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-2014 Cogent Embedded, Inc.
7 * Copyright (C) 2014 Codethink Limited
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/etherdevice.h>
28 #include <linux/delay.h>
29 #include <linux/platform_device.h>
30 #include <linux/mdio-bitbang.h>
31 #include <linux/netdevice.h>
33 #include <linux/of_device.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_net.h>
36 #include <linux/phy.h>
37 #include <linux/cache.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/slab.h>
41 #include <linux/ethtool.h>
42 #include <linux/if_vlan.h>
43 #include <linux/clk.h>
44 #include <linux/sh_eth.h>
45 #include <linux/of_mdio.h>
49 #define SH_ETH_DEF_MSG_ENABLE \
55 #define SH_ETH_OFFSET_INVALID ((u16)~0)
57 #define SH_ETH_OFFSET_DEFAULTS \
58 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
60 static const u16 sh_eth_offset_gigabit
[SH_ETH_MAX_REGISTER_OFFSET
] = {
61 SH_ETH_OFFSET_DEFAULTS
,
116 [TSU_CTRST
] = 0x0004,
117 [TSU_FWEN0
] = 0x0010,
118 [TSU_FWEN1
] = 0x0014,
120 [TSU_BSYSL0
] = 0x0020,
121 [TSU_BSYSL1
] = 0x0024,
122 [TSU_PRISL0
] = 0x0028,
123 [TSU_PRISL1
] = 0x002c,
124 [TSU_FWSL0
] = 0x0030,
125 [TSU_FWSL1
] = 0x0034,
126 [TSU_FWSLC
] = 0x0038,
127 [TSU_QTAG0
] = 0x0040,
128 [TSU_QTAG1
] = 0x0044,
130 [TSU_FWINMK
] = 0x0054,
131 [TSU_ADQT0
] = 0x0048,
132 [TSU_ADQT1
] = 0x004c,
133 [TSU_VTAG0
] = 0x0058,
134 [TSU_VTAG1
] = 0x005c,
135 [TSU_ADSBSY
] = 0x0060,
137 [TSU_POST1
] = 0x0070,
138 [TSU_POST2
] = 0x0074,
139 [TSU_POST3
] = 0x0078,
140 [TSU_POST4
] = 0x007c,
141 [TSU_ADRH0
] = 0x0100,
157 static const u16 sh_eth_offset_fast_rz
[SH_ETH_MAX_REGISTER_OFFSET
] = {
158 SH_ETH_OFFSET_DEFAULTS
,
203 [TSU_CTRST
] = 0x0004,
204 [TSU_VTAG0
] = 0x0058,
205 [TSU_ADSBSY
] = 0x0060,
207 [TSU_ADRH0
] = 0x0100,
215 static const u16 sh_eth_offset_fast_rcar
[SH_ETH_MAX_REGISTER_OFFSET
] = {
216 SH_ETH_OFFSET_DEFAULTS
,
263 static const u16 sh_eth_offset_fast_sh4
[SH_ETH_MAX_REGISTER_OFFSET
] = {
264 SH_ETH_OFFSET_DEFAULTS
,
317 static const u16 sh_eth_offset_fast_sh3_sh2
[SH_ETH_MAX_REGISTER_OFFSET
] = {
318 SH_ETH_OFFSET_DEFAULTS
,
366 [TSU_CTRST
] = 0x0004,
367 [TSU_FWEN0
] = 0x0010,
368 [TSU_FWEN1
] = 0x0014,
370 [TSU_BSYSL0
] = 0x0020,
371 [TSU_BSYSL1
] = 0x0024,
372 [TSU_PRISL0
] = 0x0028,
373 [TSU_PRISL1
] = 0x002c,
374 [TSU_FWSL0
] = 0x0030,
375 [TSU_FWSL1
] = 0x0034,
376 [TSU_FWSLC
] = 0x0038,
377 [TSU_QTAGM0
] = 0x0040,
378 [TSU_QTAGM1
] = 0x0044,
379 [TSU_ADQT0
] = 0x0048,
380 [TSU_ADQT1
] = 0x004c,
382 [TSU_FWINMK
] = 0x0054,
383 [TSU_ADSBSY
] = 0x0060,
385 [TSU_POST1
] = 0x0070,
386 [TSU_POST2
] = 0x0074,
387 [TSU_POST3
] = 0x0078,
388 [TSU_POST4
] = 0x007c,
403 [TSU_ADRH0
] = 0x0100,
406 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
);
407 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
);
409 static void sh_eth_write(struct net_device
*ndev
, u32 data
, int enum_index
)
411 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
412 u16 offset
= mdp
->reg_offset
[enum_index
];
414 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
417 iowrite32(data
, mdp
->addr
+ offset
);
420 static u32
sh_eth_read(struct net_device
*ndev
, int enum_index
)
422 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
423 u16 offset
= mdp
->reg_offset
[enum_index
];
425 if (WARN_ON(offset
== SH_ETH_OFFSET_INVALID
))
428 return ioread32(mdp
->addr
+ offset
);
431 static bool sh_eth_is_gether(struct sh_eth_private
*mdp
)
433 return mdp
->reg_offset
== sh_eth_offset_gigabit
;
436 static bool sh_eth_is_rz_fast_ether(struct sh_eth_private
*mdp
)
438 return mdp
->reg_offset
== sh_eth_offset_fast_rz
;
441 static void sh_eth_select_mii(struct net_device
*ndev
)
444 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
446 switch (mdp
->phy_interface
) {
447 case PHY_INTERFACE_MODE_GMII
:
450 case PHY_INTERFACE_MODE_MII
:
453 case PHY_INTERFACE_MODE_RMII
:
458 "PHY interface mode was not setup. Set to MII.\n");
463 sh_eth_write(ndev
, value
, RMII_MII
);
466 static void sh_eth_set_duplex(struct net_device
*ndev
)
468 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
470 if (mdp
->duplex
) /* Full */
471 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
473 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
476 /* There is CPU dependent code */
477 static void sh_eth_set_rate_r8a777x(struct net_device
*ndev
)
479 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
481 switch (mdp
->speed
) {
482 case 10: /* 10BASE */
483 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_ELB
, ECMR
);
485 case 100:/* 100BASE */
486 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_ELB
, ECMR
);
494 static struct sh_eth_cpu_data r8a777x_data
= {
495 .set_duplex
= sh_eth_set_duplex
,
496 .set_rate
= sh_eth_set_rate_r8a777x
,
498 .register_type
= SH_ETH_REG_FAST_RCAR
,
500 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
501 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
502 .eesipr_value
= 0x01ff009f,
504 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
505 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
506 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
508 .fdr_value
= 0x00000f0f,
517 static struct sh_eth_cpu_data r8a779x_data
= {
518 .set_duplex
= sh_eth_set_duplex
,
519 .set_rate
= sh_eth_set_rate_r8a777x
,
521 .register_type
= SH_ETH_REG_FAST_RCAR
,
523 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
524 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
525 .eesipr_value
= 0x01ff009f,
527 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
528 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
529 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
531 .fdr_value
= 0x00000f0f,
533 .trscer_err_mask
= DESC_I_RINT8
,
542 static void sh_eth_set_rate_sh7724(struct net_device
*ndev
)
544 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
546 switch (mdp
->speed
) {
547 case 10: /* 10BASE */
548 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_RTM
, ECMR
);
550 case 100:/* 100BASE */
551 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_RTM
, ECMR
);
559 static struct sh_eth_cpu_data sh7724_data
= {
560 .set_duplex
= sh_eth_set_duplex
,
561 .set_rate
= sh_eth_set_rate_sh7724
,
563 .register_type
= SH_ETH_REG_FAST_SH4
,
565 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
566 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
567 .eesipr_value
= 0x01ff009f,
569 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
570 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
571 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
579 .rpadir_value
= 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
582 static void sh_eth_set_rate_sh7757(struct net_device
*ndev
)
584 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
586 switch (mdp
->speed
) {
587 case 10: /* 10BASE */
588 sh_eth_write(ndev
, 0, RTRATE
);
590 case 100:/* 100BASE */
591 sh_eth_write(ndev
, 1, RTRATE
);
599 static struct sh_eth_cpu_data sh7757_data
= {
600 .set_duplex
= sh_eth_set_duplex
,
601 .set_rate
= sh_eth_set_rate_sh7757
,
603 .register_type
= SH_ETH_REG_FAST_SH4
,
605 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
607 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
608 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
609 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
612 .irq_flags
= IRQF_SHARED
,
619 .rpadir_value
= 2 << 16,
623 #define SH_GIGA_ETH_BASE 0xfee00000UL
624 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
625 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
626 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
629 u32 mahr
[2], malr
[2];
631 /* save MAHR and MALR */
632 for (i
= 0; i
< 2; i
++) {
633 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
634 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
638 iowrite32(ARSTR_ARSTR
, (void *)(SH_GIGA_ETH_BASE
+ 0x1800));
641 /* restore MAHR and MALR */
642 for (i
= 0; i
< 2; i
++) {
643 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
644 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
648 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
650 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
652 switch (mdp
->speed
) {
653 case 10: /* 10BASE */
654 sh_eth_write(ndev
, 0x00000000, GECMR
);
656 case 100:/* 100BASE */
657 sh_eth_write(ndev
, 0x00000010, GECMR
);
659 case 1000: /* 1000BASE */
660 sh_eth_write(ndev
, 0x00000020, GECMR
);
667 /* SH7757(GETHERC) */
668 static struct sh_eth_cpu_data sh7757_data_giga
= {
669 .chip_reset
= sh_eth_chip_reset_giga
,
670 .set_duplex
= sh_eth_set_duplex
,
671 .set_rate
= sh_eth_set_rate_giga
,
673 .register_type
= SH_ETH_REG_GIGABIT
,
675 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
676 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
677 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
679 .tx_check
= EESR_TC1
| EESR_FTC
,
680 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
681 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
683 .fdr_value
= 0x0000072f,
685 .irq_flags
= IRQF_SHARED
,
692 .rpadir_value
= 2 << 16,
698 static void sh_eth_chip_reset(struct net_device
*ndev
)
700 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
703 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
707 static void sh_eth_set_rate_gether(struct net_device
*ndev
)
709 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
711 switch (mdp
->speed
) {
712 case 10: /* 10BASE */
713 sh_eth_write(ndev
, GECMR_10
, GECMR
);
715 case 100:/* 100BASE */
716 sh_eth_write(ndev
, GECMR_100
, GECMR
);
718 case 1000: /* 1000BASE */
719 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
727 static struct sh_eth_cpu_data sh7734_data
= {
728 .chip_reset
= sh_eth_chip_reset
,
729 .set_duplex
= sh_eth_set_duplex
,
730 .set_rate
= sh_eth_set_rate_gether
,
732 .register_type
= SH_ETH_REG_GIGABIT
,
734 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
735 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
736 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
738 .tx_check
= EESR_TC1
| EESR_FTC
,
739 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
740 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
756 static struct sh_eth_cpu_data sh7763_data
= {
757 .chip_reset
= sh_eth_chip_reset
,
758 .set_duplex
= sh_eth_set_duplex
,
759 .set_rate
= sh_eth_set_rate_gether
,
761 .register_type
= SH_ETH_REG_GIGABIT
,
763 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
764 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
765 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
767 .tx_check
= EESR_TC1
| EESR_FTC
,
768 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
769 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
780 .irq_flags
= IRQF_SHARED
,
783 static void sh_eth_chip_reset_r8a7740(struct net_device
*ndev
)
785 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
788 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
791 sh_eth_select_mii(ndev
);
795 static struct sh_eth_cpu_data r8a7740_data
= {
796 .chip_reset
= sh_eth_chip_reset_r8a7740
,
797 .set_duplex
= sh_eth_set_duplex
,
798 .set_rate
= sh_eth_set_rate_gether
,
800 .register_type
= SH_ETH_REG_GIGABIT
,
802 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
803 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
804 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
806 .tx_check
= EESR_TC1
| EESR_FTC
,
807 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
808 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
810 .fdr_value
= 0x0000070f,
818 .rpadir_value
= 2 << 16,
827 static struct sh_eth_cpu_data r7s72100_data
= {
828 .chip_reset
= sh_eth_chip_reset
,
829 .set_duplex
= sh_eth_set_duplex
,
831 .register_type
= SH_ETH_REG_FAST_RZ
,
833 .ecsr_value
= ECSR_ICD
,
834 .ecsipr_value
= ECSIPR_ICDIP
,
835 .eesipr_value
= 0xff7f009f,
837 .tx_check
= EESR_TC1
| EESR_FTC
,
838 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
839 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
841 .fdr_value
= 0x0000070f,
849 .rpadir_value
= 2 << 16,
857 static struct sh_eth_cpu_data sh7619_data
= {
858 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
860 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
868 static struct sh_eth_cpu_data sh771x_data
= {
869 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
871 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
875 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
878 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
880 if (!cd
->ecsipr_value
)
881 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
883 if (!cd
->fcftr_value
)
884 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
|
885 DEFAULT_FIFO_F_D_RFD
;
888 cd
->fdr_value
= DEFAULT_FDR_INIT
;
891 cd
->tx_check
= DEFAULT_TX_CHECK
;
893 if (!cd
->eesr_err_check
)
894 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
896 if (!cd
->trscer_err_mask
)
897 cd
->trscer_err_mask
= DEFAULT_TRSCER_ERR_MASK
;
900 static int sh_eth_check_reset(struct net_device
*ndev
)
906 if (!(sh_eth_read(ndev
, EDMR
) & 0x3))
912 netdev_err(ndev
, "Device reset failed\n");
918 static int sh_eth_reset(struct net_device
*ndev
)
920 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
923 if (sh_eth_is_gether(mdp
) || sh_eth_is_rz_fast_ether(mdp
)) {
924 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
925 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
,
928 ret
= sh_eth_check_reset(ndev
);
933 sh_eth_write(ndev
, 0x0, TDLAR
);
934 sh_eth_write(ndev
, 0x0, TDFAR
);
935 sh_eth_write(ndev
, 0x0, TDFXR
);
936 sh_eth_write(ndev
, 0x0, TDFFR
);
937 sh_eth_write(ndev
, 0x0, RDLAR
);
938 sh_eth_write(ndev
, 0x0, RDFAR
);
939 sh_eth_write(ndev
, 0x0, RDFXR
);
940 sh_eth_write(ndev
, 0x0, RDFFR
);
942 /* Reset HW CRC register */
944 sh_eth_write(ndev
, 0x0, CSMR
);
946 /* Select MII mode */
947 if (mdp
->cd
->select_mii
)
948 sh_eth_select_mii(ndev
);
950 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_ETHER
,
953 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) & ~EDMR_SRST_ETHER
,
960 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
962 uintptr_t reserve
= (uintptr_t)skb
->data
& (SH_ETH_RX_ALIGN
- 1);
965 skb_reserve(skb
, SH_ETH_RX_ALIGN
- reserve
);
969 /* CPU <-> EDMAC endian convert */
970 static inline __u32
cpu_to_edmac(struct sh_eth_private
*mdp
, u32 x
)
972 switch (mdp
->edmac_endian
) {
973 case EDMAC_LITTLE_ENDIAN
:
974 return cpu_to_le32(x
);
975 case EDMAC_BIG_ENDIAN
:
976 return cpu_to_be32(x
);
981 static inline __u32
edmac_to_cpu(struct sh_eth_private
*mdp
, u32 x
)
983 switch (mdp
->edmac_endian
) {
984 case EDMAC_LITTLE_ENDIAN
:
985 return le32_to_cpu(x
);
986 case EDMAC_BIG_ENDIAN
:
987 return be32_to_cpu(x
);
992 /* Program the hardware MAC address from dev->dev_addr. */
993 static void update_mac_address(struct net_device
*ndev
)
996 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
997 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
999 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
1002 /* Get MAC address from SuperH MAC address register
1004 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
1005 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
1006 * When you want use this device, you must set MAC address in bootloader.
1009 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
1011 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
1012 memcpy(ndev
->dev_addr
, mac
, ETH_ALEN
);
1014 ndev
->dev_addr
[0] = (sh_eth_read(ndev
, MAHR
) >> 24);
1015 ndev
->dev_addr
[1] = (sh_eth_read(ndev
, MAHR
) >> 16) & 0xFF;
1016 ndev
->dev_addr
[2] = (sh_eth_read(ndev
, MAHR
) >> 8) & 0xFF;
1017 ndev
->dev_addr
[3] = (sh_eth_read(ndev
, MAHR
) & 0xFF);
1018 ndev
->dev_addr
[4] = (sh_eth_read(ndev
, MALR
) >> 8) & 0xFF;
1019 ndev
->dev_addr
[5] = (sh_eth_read(ndev
, MALR
) & 0xFF);
1023 static u32
sh_eth_get_edtrr_trns(struct sh_eth_private
*mdp
)
1025 if (sh_eth_is_gether(mdp
) || sh_eth_is_rz_fast_ether(mdp
))
1026 return EDTRR_TRNS_GETHER
;
1028 return EDTRR_TRNS_ETHER
;
1032 void (*set_gate
)(void *addr
);
1033 struct mdiobb_ctrl ctrl
;
1035 u32 mmd_msk
;/* MMD */
1042 static void bb_set(void *addr
, u32 msk
)
1044 iowrite32(ioread32(addr
) | msk
, addr
);
1048 static void bb_clr(void *addr
, u32 msk
)
1050 iowrite32((ioread32(addr
) & ~msk
), addr
);
1054 static int bb_read(void *addr
, u32 msk
)
1056 return (ioread32(addr
) & msk
) != 0;
1059 /* Data I/O pin control */
1060 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1062 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1064 if (bitbang
->set_gate
)
1065 bitbang
->set_gate(bitbang
->addr
);
1068 bb_set(bitbang
->addr
, bitbang
->mmd_msk
);
1070 bb_clr(bitbang
->addr
, bitbang
->mmd_msk
);
1074 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
1076 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1078 if (bitbang
->set_gate
)
1079 bitbang
->set_gate(bitbang
->addr
);
1082 bb_set(bitbang
->addr
, bitbang
->mdo_msk
);
1084 bb_clr(bitbang
->addr
, bitbang
->mdo_msk
);
1088 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
1090 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1092 if (bitbang
->set_gate
)
1093 bitbang
->set_gate(bitbang
->addr
);
1095 return bb_read(bitbang
->addr
, bitbang
->mdi_msk
);
1098 /* MDC pin control */
1099 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1101 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1103 if (bitbang
->set_gate
)
1104 bitbang
->set_gate(bitbang
->addr
);
1107 bb_set(bitbang
->addr
, bitbang
->mdc_msk
);
1109 bb_clr(bitbang
->addr
, bitbang
->mdc_msk
);
1112 /* mdio bus control struct */
1113 static struct mdiobb_ops bb_ops
= {
1114 .owner
= THIS_MODULE
,
1115 .set_mdc
= sh_mdc_ctrl
,
1116 .set_mdio_dir
= sh_mmd_ctrl
,
1117 .set_mdio_data
= sh_set_mdio
,
1118 .get_mdio_data
= sh_get_mdio
,
1121 /* free skb and descriptor buffer */
1122 static void sh_eth_ring_free(struct net_device
*ndev
)
1124 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1127 /* Free Rx skb ringbuffer */
1128 if (mdp
->rx_skbuff
) {
1129 for (i
= 0; i
< mdp
->num_rx_ring
; i
++)
1130 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1132 kfree(mdp
->rx_skbuff
);
1133 mdp
->rx_skbuff
= NULL
;
1135 /* Free Tx skb ringbuffer */
1136 if (mdp
->tx_skbuff
) {
1137 for (i
= 0; i
< mdp
->num_tx_ring
; i
++)
1138 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1140 kfree(mdp
->tx_skbuff
);
1141 mdp
->tx_skbuff
= NULL
;
1144 ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1145 dma_free_coherent(NULL
, ringsize
, mdp
->rx_ring
,
1147 mdp
->rx_ring
= NULL
;
1151 ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1152 dma_free_coherent(NULL
, ringsize
, mdp
->tx_ring
,
1154 mdp
->tx_ring
= NULL
;
1158 /* format skb and descriptor buffer */
1159 static void sh_eth_ring_format(struct net_device
*ndev
)
1161 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1163 struct sk_buff
*skb
;
1164 struct sh_eth_rxdesc
*rxdesc
= NULL
;
1165 struct sh_eth_txdesc
*txdesc
= NULL
;
1166 int rx_ringsize
= sizeof(*rxdesc
) * mdp
->num_rx_ring
;
1167 int tx_ringsize
= sizeof(*txdesc
) * mdp
->num_tx_ring
;
1168 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1169 dma_addr_t dma_addr
;
1177 memset(mdp
->rx_ring
, 0, rx_ringsize
);
1179 /* build Rx ring buffer */
1180 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1182 mdp
->rx_skbuff
[i
] = NULL
;
1183 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1186 sh_eth_set_receive_align(skb
);
1189 rxdesc
= &mdp
->rx_ring
[i
];
1190 /* The size of the buffer is a multiple of 32 bytes. */
1191 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1192 rxdesc
->len
= cpu_to_edmac(mdp
, buf_len
<< 16);
1193 dma_addr
= dma_map_single(&ndev
->dev
, skb
->data
, buf_len
,
1195 if (dma_mapping_error(&ndev
->dev
, dma_addr
)) {
1199 mdp
->rx_skbuff
[i
] = skb
;
1200 rxdesc
->addr
= cpu_to_edmac(mdp
, dma_addr
);
1201 rxdesc
->status
= cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1203 /* Rx descriptor address set */
1205 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
1206 if (sh_eth_is_gether(mdp
) ||
1207 sh_eth_is_rz_fast_ether(mdp
))
1208 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
1212 mdp
->dirty_rx
= (u32
) (i
- mdp
->num_rx_ring
);
1214 /* Mark the last entry as wrapping the ring. */
1215 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RDLE
);
1217 memset(mdp
->tx_ring
, 0, tx_ringsize
);
1219 /* build Tx ring buffer */
1220 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1221 mdp
->tx_skbuff
[i
] = NULL
;
1222 txdesc
= &mdp
->tx_ring
[i
];
1223 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1224 txdesc
->len
= cpu_to_edmac(mdp
, 0);
1226 /* Tx descriptor address set */
1227 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
1228 if (sh_eth_is_gether(mdp
) ||
1229 sh_eth_is_rz_fast_ether(mdp
))
1230 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
1234 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1237 /* Get skb and descriptor buffer */
1238 static int sh_eth_ring_init(struct net_device
*ndev
)
1240 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1241 int rx_ringsize
, tx_ringsize
;
1243 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1244 * card needs room to do 8 byte alignment, +2 so we can reserve
1245 * the first 2 bytes, and +16 gets room for the status word from the
1248 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
1249 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
1250 if (mdp
->cd
->rpadir
)
1251 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
1253 /* Allocate RX and TX skb rings */
1254 mdp
->rx_skbuff
= kcalloc(mdp
->num_rx_ring
, sizeof(*mdp
->rx_skbuff
),
1256 if (!mdp
->rx_skbuff
)
1259 mdp
->tx_skbuff
= kcalloc(mdp
->num_tx_ring
, sizeof(*mdp
->tx_skbuff
),
1261 if (!mdp
->tx_skbuff
)
1264 /* Allocate all Rx descriptors. */
1265 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1266 mdp
->rx_ring
= dma_alloc_coherent(NULL
, rx_ringsize
, &mdp
->rx_desc_dma
,
1273 /* Allocate all Tx descriptors. */
1274 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1275 mdp
->tx_ring
= dma_alloc_coherent(NULL
, tx_ringsize
, &mdp
->tx_desc_dma
,
1282 /* Free Rx and Tx skb ring buffer and DMA buffer */
1283 sh_eth_ring_free(ndev
);
1288 static int sh_eth_dev_init(struct net_device
*ndev
, bool start
)
1291 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1295 ret
= sh_eth_reset(ndev
);
1299 if (mdp
->cd
->rmiimode
)
1300 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1302 /* Descriptor format */
1303 sh_eth_ring_format(ndev
);
1304 if (mdp
->cd
->rpadir
)
1305 sh_eth_write(ndev
, mdp
->cd
->rpadir_value
, RPADIR
);
1307 /* all sh_eth int mask */
1308 sh_eth_write(ndev
, 0, EESIPR
);
1310 #if defined(__LITTLE_ENDIAN)
1311 if (mdp
->cd
->hw_swap
)
1312 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
1315 sh_eth_write(ndev
, 0, EDMR
);
1318 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
1319 sh_eth_write(ndev
, 0, TFTR
);
1321 /* Frame recv control (enable multiple-packets per rx irq) */
1322 sh_eth_write(ndev
, RMCR_RNC
, RMCR
);
1324 sh_eth_write(ndev
, mdp
->cd
->trscer_err_mask
, TRSCER
);
1327 sh_eth_write(ndev
, 0x800, BCULR
); /* Burst sycle set */
1329 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
1331 if (!mdp
->cd
->no_trimd
)
1332 sh_eth_write(ndev
, 0, TRIMD
);
1334 /* Recv frame limit set register */
1335 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
1338 sh_eth_write(ndev
, sh_eth_read(ndev
, EESR
), EESR
);
1340 mdp
->irq_enabled
= true;
1341 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1344 /* PAUSE Prohibition */
1345 val
= (sh_eth_read(ndev
, ECMR
) & ECMR_DM
) |
1346 ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) | ECMR_TE
| ECMR_RE
;
1348 sh_eth_write(ndev
, val
, ECMR
);
1350 if (mdp
->cd
->set_rate
)
1351 mdp
->cd
->set_rate(ndev
);
1353 /* E-MAC Status Register clear */
1354 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
1356 /* E-MAC Interrupt Enable register */
1358 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
1360 /* Set MAC address */
1361 update_mac_address(ndev
);
1365 sh_eth_write(ndev
, APR_AP
, APR
);
1367 sh_eth_write(ndev
, MPR_MP
, MPR
);
1368 if (mdp
->cd
->tpauser
)
1369 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
1372 /* Setting the Rx mode will start the Rx process. */
1373 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1375 netif_start_queue(ndev
);
1381 static void sh_eth_dev_exit(struct net_device
*ndev
)
1383 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1386 /* Deactivate all TX descriptors, so DMA should stop at next
1387 * packet boundary if it's currently running
1389 for (i
= 0; i
< mdp
->num_tx_ring
; i
++)
1390 mdp
->tx_ring
[i
].status
&= ~cpu_to_edmac(mdp
, TD_TACT
);
1392 /* Disable TX FIFO egress to MAC */
1393 sh_eth_rcv_snd_disable(ndev
);
1395 /* Stop RX DMA at next packet boundary */
1396 sh_eth_write(ndev
, 0, EDRRR
);
1398 /* Aside from TX DMA, we can't tell when the hardware is
1399 * really stopped, so we need to reset to make sure.
1400 * Before doing that, wait for long enough to *probably*
1401 * finish transmitting the last packet and poll stats.
1403 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1404 sh_eth_get_stats(ndev
);
1407 /* Set MAC address again */
1408 update_mac_address(ndev
);
1411 /* free Tx skb function */
1412 static int sh_eth_txfree(struct net_device
*ndev
)
1414 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1415 struct sh_eth_txdesc
*txdesc
;
1419 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
1420 entry
= mdp
->dirty_tx
% mdp
->num_tx_ring
;
1421 txdesc
= &mdp
->tx_ring
[entry
];
1422 if (txdesc
->status
& cpu_to_edmac(mdp
, TD_TACT
))
1424 /* TACT bit must be checked before all the following reads */
1426 netif_info(mdp
, tx_done
, ndev
,
1427 "tx entry %d status 0x%08x\n",
1428 entry
, edmac_to_cpu(mdp
, txdesc
->status
));
1429 /* Free the original skb. */
1430 if (mdp
->tx_skbuff
[entry
]) {
1431 dma_unmap_single(&ndev
->dev
,
1432 edmac_to_cpu(mdp
, txdesc
->addr
),
1433 edmac_to_cpu(mdp
, txdesc
->len
) >> 16,
1435 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
1436 mdp
->tx_skbuff
[entry
] = NULL
;
1439 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1440 if (entry
>= mdp
->num_tx_ring
- 1)
1441 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1443 ndev
->stats
.tx_packets
++;
1444 ndev
->stats
.tx_bytes
+= edmac_to_cpu(mdp
, txdesc
->len
) >> 16;
1449 /* Packet receive function */
1450 static int sh_eth_rx(struct net_device
*ndev
, u32 intr_status
, int *quota
)
1452 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1453 struct sh_eth_rxdesc
*rxdesc
;
1455 int entry
= mdp
->cur_rx
% mdp
->num_rx_ring
;
1456 int boguscnt
= (mdp
->dirty_rx
+ mdp
->num_rx_ring
) - mdp
->cur_rx
;
1458 struct sk_buff
*skb
;
1461 int skbuff_size
= mdp
->rx_buf_sz
+ SH_ETH_RX_ALIGN
+ 32 - 1;
1462 dma_addr_t dma_addr
;
1465 boguscnt
= min(boguscnt
, *quota
);
1467 rxdesc
= &mdp
->rx_ring
[entry
];
1468 while (!(rxdesc
->status
& cpu_to_edmac(mdp
, RD_RACT
))) {
1469 /* RACT bit must be checked before all the following reads */
1471 desc_status
= edmac_to_cpu(mdp
, rxdesc
->status
);
1472 pkt_len
= edmac_to_cpu(mdp
, rxdesc
->len
) & RD_RFL
;
1477 netif_info(mdp
, rx_status
, ndev
,
1478 "rx entry %d status 0x%08x len %d\n",
1479 entry
, desc_status
, pkt_len
);
1481 if (!(desc_status
& RDFEND
))
1482 ndev
->stats
.rx_length_errors
++;
1484 /* In case of almost all GETHER/ETHERs, the Receive Frame State
1485 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1486 * bit 0. However, in case of the R8A7740 and R7S72100
1487 * the RFS bits are from bit 25 to bit 16. So, the
1488 * driver needs right shifting by 16.
1490 if (mdp
->cd
->shift_rd0
)
1493 skb
= mdp
->rx_skbuff
[entry
];
1494 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
1495 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
1496 ndev
->stats
.rx_errors
++;
1497 if (desc_status
& RD_RFS1
)
1498 ndev
->stats
.rx_crc_errors
++;
1499 if (desc_status
& RD_RFS2
)
1500 ndev
->stats
.rx_frame_errors
++;
1501 if (desc_status
& RD_RFS3
)
1502 ndev
->stats
.rx_length_errors
++;
1503 if (desc_status
& RD_RFS4
)
1504 ndev
->stats
.rx_length_errors
++;
1505 if (desc_status
& RD_RFS6
)
1506 ndev
->stats
.rx_missed_errors
++;
1507 if (desc_status
& RD_RFS10
)
1508 ndev
->stats
.rx_over_errors
++;
1510 dma_addr
= edmac_to_cpu(mdp
, rxdesc
->addr
);
1511 if (!mdp
->cd
->hw_swap
)
1513 phys_to_virt(ALIGN(dma_addr
, 4)),
1515 mdp
->rx_skbuff
[entry
] = NULL
;
1516 if (mdp
->cd
->rpadir
)
1517 skb_reserve(skb
, NET_IP_ALIGN
);
1518 dma_unmap_single(&ndev
->dev
, dma_addr
,
1519 ALIGN(mdp
->rx_buf_sz
, 32),
1521 skb_put(skb
, pkt_len
);
1522 skb
->protocol
= eth_type_trans(skb
, ndev
);
1523 netif_receive_skb(skb
);
1524 ndev
->stats
.rx_packets
++;
1525 ndev
->stats
.rx_bytes
+= pkt_len
;
1526 if (desc_status
& RD_RFS8
)
1527 ndev
->stats
.multicast
++;
1529 entry
= (++mdp
->cur_rx
) % mdp
->num_rx_ring
;
1530 rxdesc
= &mdp
->rx_ring
[entry
];
1533 /* Refill the Rx ring buffers. */
1534 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
1535 entry
= mdp
->dirty_rx
% mdp
->num_rx_ring
;
1536 rxdesc
= &mdp
->rx_ring
[entry
];
1537 /* The size of the buffer is 32 byte boundary. */
1538 buf_len
= ALIGN(mdp
->rx_buf_sz
, 32);
1539 rxdesc
->len
= cpu_to_edmac(mdp
, buf_len
<< 16);
1541 if (mdp
->rx_skbuff
[entry
] == NULL
) {
1542 skb
= netdev_alloc_skb(ndev
, skbuff_size
);
1544 break; /* Better luck next round. */
1545 sh_eth_set_receive_align(skb
);
1546 dma_addr
= dma_map_single(&ndev
->dev
, skb
->data
,
1547 buf_len
, DMA_FROM_DEVICE
);
1548 if (dma_mapping_error(&ndev
->dev
, dma_addr
)) {
1552 mdp
->rx_skbuff
[entry
] = skb
;
1554 skb_checksum_none_assert(skb
);
1555 rxdesc
->addr
= cpu_to_edmac(mdp
, dma_addr
);
1557 dma_wmb(); /* RACT bit must be set after all the above writes */
1558 if (entry
>= mdp
->num_rx_ring
- 1)
1560 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
| RD_RDLE
);
1563 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1566 /* Restart Rx engine if stopped. */
1567 /* If we don't need to check status, don't. -KDU */
1568 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
)) {
1569 /* fix the values for the next receiving if RDE is set */
1570 if (intr_status
& EESR_RDE
&&
1571 mdp
->reg_offset
[RDFAR
] != SH_ETH_OFFSET_INVALID
) {
1572 u32 count
= (sh_eth_read(ndev
, RDFAR
) -
1573 sh_eth_read(ndev
, RDLAR
)) >> 4;
1575 mdp
->cur_rx
= count
;
1576 mdp
->dirty_rx
= count
;
1578 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1581 *quota
-= limit
- boguscnt
- 1;
1586 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
1588 /* disable tx and rx */
1589 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) &
1590 ~(ECMR_RE
| ECMR_TE
), ECMR
);
1593 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
1595 /* enable tx and rx */
1596 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) |
1597 (ECMR_RE
| ECMR_TE
), ECMR
);
1600 /* error control function */
1601 static void sh_eth_error(struct net_device
*ndev
, u32 intr_status
)
1603 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1608 if (intr_status
& EESR_ECI
) {
1609 felic_stat
= sh_eth_read(ndev
, ECSR
);
1610 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1611 if (felic_stat
& ECSR_ICD
)
1612 ndev
->stats
.tx_carrier_errors
++;
1613 if (felic_stat
& ECSR_LCHNG
) {
1615 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
) {
1618 link_stat
= (sh_eth_read(ndev
, PSR
));
1619 if (mdp
->ether_link_active_low
)
1620 link_stat
= ~link_stat
;
1622 if (!(link_stat
& PHY_ST_LINK
)) {
1623 sh_eth_rcv_snd_disable(ndev
);
1626 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) &
1627 ~DMAC_M_ECI
, EESIPR
);
1629 sh_eth_write(ndev
, sh_eth_read(ndev
, ECSR
),
1631 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) |
1632 DMAC_M_ECI
, EESIPR
);
1633 /* enable tx and rx */
1634 sh_eth_rcv_snd_enable(ndev
);
1640 if (intr_status
& EESR_TWB
) {
1641 /* Unused write back interrupt */
1642 if (intr_status
& EESR_TABT
) { /* Transmit Abort int */
1643 ndev
->stats
.tx_aborted_errors
++;
1644 netif_err(mdp
, tx_err
, ndev
, "Transmit Abort\n");
1648 if (intr_status
& EESR_RABT
) {
1649 /* Receive Abort int */
1650 if (intr_status
& EESR_RFRMER
) {
1651 /* Receive Frame Overflow int */
1652 ndev
->stats
.rx_frame_errors
++;
1656 if (intr_status
& EESR_TDE
) {
1657 /* Transmit Descriptor Empty int */
1658 ndev
->stats
.tx_fifo_errors
++;
1659 netif_err(mdp
, tx_err
, ndev
, "Transmit Descriptor Empty\n");
1662 if (intr_status
& EESR_TFE
) {
1663 /* FIFO under flow */
1664 ndev
->stats
.tx_fifo_errors
++;
1665 netif_err(mdp
, tx_err
, ndev
, "Transmit FIFO Under flow\n");
1668 if (intr_status
& EESR_RDE
) {
1669 /* Receive Descriptor Empty int */
1670 ndev
->stats
.rx_over_errors
++;
1673 if (intr_status
& EESR_RFE
) {
1674 /* Receive FIFO Overflow int */
1675 ndev
->stats
.rx_fifo_errors
++;
1678 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1680 ndev
->stats
.tx_fifo_errors
++;
1681 netif_err(mdp
, tx_err
, ndev
, "Address Error\n");
1684 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1685 if (mdp
->cd
->no_ade
)
1687 if (intr_status
& mask
) {
1689 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1692 netdev_err(ndev
, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1693 intr_status
, mdp
->cur_tx
, mdp
->dirty_tx
,
1694 (u32
)ndev
->state
, edtrr
);
1695 /* dirty buffer free */
1696 sh_eth_txfree(ndev
);
1699 if (edtrr
^ sh_eth_get_edtrr_trns(mdp
)) {
1701 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
1704 netif_wake_queue(ndev
);
1708 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1710 struct net_device
*ndev
= netdev
;
1711 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1712 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1713 irqreturn_t ret
= IRQ_NONE
;
1714 u32 intr_status
, intr_enable
;
1716 spin_lock(&mdp
->lock
);
1718 /* Get interrupt status */
1719 intr_status
= sh_eth_read(ndev
, EESR
);
1720 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1721 * enabled since it's the one that comes thru regardless of the mask,
1722 * and we need to fully handle it in sh_eth_error() in order to quench
1723 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1725 intr_enable
= sh_eth_read(ndev
, EESIPR
);
1726 intr_status
&= intr_enable
| DMAC_M_ECI
;
1727 if (intr_status
& (EESR_RX_CHECK
| cd
->tx_check
| cd
->eesr_err_check
))
1732 if (!likely(mdp
->irq_enabled
)) {
1733 sh_eth_write(ndev
, 0, EESIPR
);
1737 if (intr_status
& EESR_RX_CHECK
) {
1738 if (napi_schedule_prep(&mdp
->napi
)) {
1739 /* Mask Rx interrupts */
1740 sh_eth_write(ndev
, intr_enable
& ~EESR_RX_CHECK
,
1742 __napi_schedule(&mdp
->napi
);
1745 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
1746 intr_status
, intr_enable
);
1751 if (intr_status
& cd
->tx_check
) {
1752 /* Clear Tx interrupts */
1753 sh_eth_write(ndev
, intr_status
& cd
->tx_check
, EESR
);
1755 sh_eth_txfree(ndev
);
1756 netif_wake_queue(ndev
);
1759 if (intr_status
& cd
->eesr_err_check
) {
1760 /* Clear error interrupts */
1761 sh_eth_write(ndev
, intr_status
& cd
->eesr_err_check
, EESR
);
1763 sh_eth_error(ndev
, intr_status
);
1767 spin_unlock(&mdp
->lock
);
1772 static int sh_eth_poll(struct napi_struct
*napi
, int budget
)
1774 struct sh_eth_private
*mdp
= container_of(napi
, struct sh_eth_private
,
1776 struct net_device
*ndev
= napi
->dev
;
1781 intr_status
= sh_eth_read(ndev
, EESR
);
1782 if (!(intr_status
& EESR_RX_CHECK
))
1784 /* Clear Rx interrupts */
1785 sh_eth_write(ndev
, intr_status
& EESR_RX_CHECK
, EESR
);
1787 if (sh_eth_rx(ndev
, intr_status
, "a
))
1791 napi_complete(napi
);
1793 /* Reenable Rx interrupts */
1794 if (mdp
->irq_enabled
)
1795 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1797 return budget
- quota
;
1800 /* PHY state control function */
1801 static void sh_eth_adjust_link(struct net_device
*ndev
)
1803 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1804 struct phy_device
*phydev
= mdp
->phydev
;
1808 if (phydev
->duplex
!= mdp
->duplex
) {
1810 mdp
->duplex
= phydev
->duplex
;
1811 if (mdp
->cd
->set_duplex
)
1812 mdp
->cd
->set_duplex(ndev
);
1815 if (phydev
->speed
!= mdp
->speed
) {
1817 mdp
->speed
= phydev
->speed
;
1818 if (mdp
->cd
->set_rate
)
1819 mdp
->cd
->set_rate(ndev
);
1823 sh_eth_read(ndev
, ECMR
) & ~ECMR_TXF
,
1826 mdp
->link
= phydev
->link
;
1827 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1828 sh_eth_rcv_snd_enable(ndev
);
1830 } else if (mdp
->link
) {
1835 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1836 sh_eth_rcv_snd_disable(ndev
);
1839 if (new_state
&& netif_msg_link(mdp
))
1840 phy_print_status(phydev
);
1843 /* PHY init function */
1844 static int sh_eth_phy_init(struct net_device
*ndev
)
1846 struct device_node
*np
= ndev
->dev
.parent
->of_node
;
1847 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1848 struct phy_device
*phydev
= NULL
;
1854 /* Try connect to PHY */
1856 struct device_node
*pn
;
1858 pn
= of_parse_phandle(np
, "phy-handle", 0);
1859 phydev
= of_phy_connect(ndev
, pn
,
1860 sh_eth_adjust_link
, 0,
1861 mdp
->phy_interface
);
1864 phydev
= ERR_PTR(-ENOENT
);
1866 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1868 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
1869 mdp
->mii_bus
->id
, mdp
->phy_id
);
1871 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
1872 mdp
->phy_interface
);
1875 if (IS_ERR(phydev
)) {
1876 netdev_err(ndev
, "failed to connect PHY\n");
1877 return PTR_ERR(phydev
);
1880 netdev_info(ndev
, "attached PHY %d (IRQ %d) to driver %s\n",
1881 phydev
->addr
, phydev
->irq
, phydev
->drv
->name
);
1883 mdp
->phydev
= phydev
;
1888 /* PHY control start function */
1889 static int sh_eth_phy_start(struct net_device
*ndev
)
1891 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1894 ret
= sh_eth_phy_init(ndev
);
1898 phy_start(mdp
->phydev
);
1903 static int sh_eth_get_settings(struct net_device
*ndev
,
1904 struct ethtool_cmd
*ecmd
)
1906 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1907 unsigned long flags
;
1913 spin_lock_irqsave(&mdp
->lock
, flags
);
1914 ret
= phy_ethtool_gset(mdp
->phydev
, ecmd
);
1915 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1920 static int sh_eth_set_settings(struct net_device
*ndev
,
1921 struct ethtool_cmd
*ecmd
)
1923 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1924 unsigned long flags
;
1930 spin_lock_irqsave(&mdp
->lock
, flags
);
1932 /* disable tx and rx */
1933 sh_eth_rcv_snd_disable(ndev
);
1935 ret
= phy_ethtool_sset(mdp
->phydev
, ecmd
);
1939 if (ecmd
->duplex
== DUPLEX_FULL
)
1944 if (mdp
->cd
->set_duplex
)
1945 mdp
->cd
->set_duplex(ndev
);
1950 /* enable tx and rx */
1951 sh_eth_rcv_snd_enable(ndev
);
1953 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1958 /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1959 * version must be bumped as well. Just adding registers up to that
1960 * limit is fine, as long as the existing register indices don't
1963 #define SH_ETH_REG_DUMP_VERSION 1
1964 #define SH_ETH_REG_DUMP_MAX_REGS 256
1966 static size_t __sh_eth_get_regs(struct net_device
*ndev
, u32
*buf
)
1968 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1969 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1973 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET
> SH_ETH_REG_DUMP_MAX_REGS
);
1975 /* Dump starts with a bitmap that tells ethtool which
1976 * registers are defined for this chip.
1978 len
= DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS
, 32);
1986 /* Add a register to the dump, if it has a defined offset.
1987 * This automatically skips most undefined registers, but for
1988 * some it is also necessary to check a capability flag in
1989 * struct sh_eth_cpu_data.
1991 #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1992 #define add_reg_from(reg, read_expr) do { \
1993 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1995 mark_reg_valid(reg); \
1996 *buf++ = read_expr; \
2001 #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
2002 #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
2074 add_tsu_reg(TSU_CTRST
);
2075 add_tsu_reg(TSU_FWEN0
);
2076 add_tsu_reg(TSU_FWEN1
);
2077 add_tsu_reg(TSU_FCM
);
2078 add_tsu_reg(TSU_BSYSL0
);
2079 add_tsu_reg(TSU_BSYSL1
);
2080 add_tsu_reg(TSU_PRISL0
);
2081 add_tsu_reg(TSU_PRISL1
);
2082 add_tsu_reg(TSU_FWSL0
);
2083 add_tsu_reg(TSU_FWSL1
);
2084 add_tsu_reg(TSU_FWSLC
);
2085 add_tsu_reg(TSU_QTAG0
);
2086 add_tsu_reg(TSU_QTAG1
);
2087 add_tsu_reg(TSU_QTAGM0
);
2088 add_tsu_reg(TSU_QTAGM1
);
2089 add_tsu_reg(TSU_FWSR
);
2090 add_tsu_reg(TSU_FWINMK
);
2091 add_tsu_reg(TSU_ADQT0
);
2092 add_tsu_reg(TSU_ADQT1
);
2093 add_tsu_reg(TSU_VTAG0
);
2094 add_tsu_reg(TSU_VTAG1
);
2095 add_tsu_reg(TSU_ADSBSY
);
2096 add_tsu_reg(TSU_TEN
);
2097 add_tsu_reg(TSU_POST1
);
2098 add_tsu_reg(TSU_POST2
);
2099 add_tsu_reg(TSU_POST3
);
2100 add_tsu_reg(TSU_POST4
);
2101 if (mdp
->reg_offset
[TSU_ADRH0
] != SH_ETH_OFFSET_INVALID
) {
2102 /* This is the start of a table, not just a single
2108 mark_reg_valid(TSU_ADRH0
);
2109 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
* 2; i
++)
2112 mdp
->reg_offset
[TSU_ADRH0
] +
2115 len
+= SH_ETH_TSU_CAM_ENTRIES
* 2;
2119 #undef mark_reg_valid
2127 static int sh_eth_get_regs_len(struct net_device
*ndev
)
2129 return __sh_eth_get_regs(ndev
, NULL
);
2132 static void sh_eth_get_regs(struct net_device
*ndev
, struct ethtool_regs
*regs
,
2135 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2137 regs
->version
= SH_ETH_REG_DUMP_VERSION
;
2139 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2140 __sh_eth_get_regs(ndev
, buf
);
2141 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2144 static int sh_eth_nway_reset(struct net_device
*ndev
)
2146 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2147 unsigned long flags
;
2153 spin_lock_irqsave(&mdp
->lock
, flags
);
2154 ret
= phy_start_aneg(mdp
->phydev
);
2155 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2160 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
2162 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2163 return mdp
->msg_enable
;
2166 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
2168 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2169 mdp
->msg_enable
= value
;
2172 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
2173 "rx_current", "tx_current",
2174 "rx_dirty", "tx_dirty",
2176 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2178 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
2182 return SH_ETH_STATS_LEN
;
2188 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
2189 struct ethtool_stats
*stats
, u64
*data
)
2191 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2194 /* device-specific stats */
2195 data
[i
++] = mdp
->cur_rx
;
2196 data
[i
++] = mdp
->cur_tx
;
2197 data
[i
++] = mdp
->dirty_rx
;
2198 data
[i
++] = mdp
->dirty_tx
;
2201 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
2203 switch (stringset
) {
2205 memcpy(data
, *sh_eth_gstrings_stats
,
2206 sizeof(sh_eth_gstrings_stats
));
2211 static void sh_eth_get_ringparam(struct net_device
*ndev
,
2212 struct ethtool_ringparam
*ring
)
2214 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2216 ring
->rx_max_pending
= RX_RING_MAX
;
2217 ring
->tx_max_pending
= TX_RING_MAX
;
2218 ring
->rx_pending
= mdp
->num_rx_ring
;
2219 ring
->tx_pending
= mdp
->num_tx_ring
;
2222 static int sh_eth_set_ringparam(struct net_device
*ndev
,
2223 struct ethtool_ringparam
*ring
)
2225 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2228 if (ring
->tx_pending
> TX_RING_MAX
||
2229 ring
->rx_pending
> RX_RING_MAX
||
2230 ring
->tx_pending
< TX_RING_MIN
||
2231 ring
->rx_pending
< RX_RING_MIN
)
2233 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
2236 if (netif_running(ndev
)) {
2237 netif_device_detach(ndev
);
2238 netif_tx_disable(ndev
);
2240 /* Serialise with the interrupt handler and NAPI, then
2241 * disable interrupts. We have to clear the
2242 * irq_enabled flag first to ensure that interrupts
2243 * won't be re-enabled.
2245 mdp
->irq_enabled
= false;
2246 synchronize_irq(ndev
->irq
);
2247 napi_synchronize(&mdp
->napi
);
2248 sh_eth_write(ndev
, 0x0000, EESIPR
);
2250 sh_eth_dev_exit(ndev
);
2252 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
2253 sh_eth_ring_free(ndev
);
2256 /* Set new parameters */
2257 mdp
->num_rx_ring
= ring
->rx_pending
;
2258 mdp
->num_tx_ring
= ring
->tx_pending
;
2260 if (netif_running(ndev
)) {
2261 ret
= sh_eth_ring_init(ndev
);
2263 netdev_err(ndev
, "%s: sh_eth_ring_init failed.\n",
2267 ret
= sh_eth_dev_init(ndev
, false);
2269 netdev_err(ndev
, "%s: sh_eth_dev_init failed.\n",
2274 mdp
->irq_enabled
= true;
2275 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
2276 /* Setting the Rx mode will start the Rx process. */
2277 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
2278 netif_device_attach(ndev
);
2284 static const struct ethtool_ops sh_eth_ethtool_ops
= {
2285 .get_settings
= sh_eth_get_settings
,
2286 .set_settings
= sh_eth_set_settings
,
2287 .get_regs_len
= sh_eth_get_regs_len
,
2288 .get_regs
= sh_eth_get_regs
,
2289 .nway_reset
= sh_eth_nway_reset
,
2290 .get_msglevel
= sh_eth_get_msglevel
,
2291 .set_msglevel
= sh_eth_set_msglevel
,
2292 .get_link
= ethtool_op_get_link
,
2293 .get_strings
= sh_eth_get_strings
,
2294 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
2295 .get_sset_count
= sh_eth_get_sset_count
,
2296 .get_ringparam
= sh_eth_get_ringparam
,
2297 .set_ringparam
= sh_eth_set_ringparam
,
2300 /* network device open function */
2301 static int sh_eth_open(struct net_device
*ndev
)
2304 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2306 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2308 napi_enable(&mdp
->napi
);
2310 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
2311 mdp
->cd
->irq_flags
, ndev
->name
, ndev
);
2313 netdev_err(ndev
, "Can not assign IRQ number\n");
2317 /* Descriptor set */
2318 ret
= sh_eth_ring_init(ndev
);
2323 ret
= sh_eth_dev_init(ndev
, true);
2327 /* PHY control start*/
2328 ret
= sh_eth_phy_start(ndev
);
2337 free_irq(ndev
->irq
, ndev
);
2339 napi_disable(&mdp
->napi
);
2340 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2344 /* Timeout function */
2345 static void sh_eth_tx_timeout(struct net_device
*ndev
)
2347 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2348 struct sh_eth_rxdesc
*rxdesc
;
2351 netif_stop_queue(ndev
);
2353 netif_err(mdp
, timer
, ndev
,
2354 "transmit timed out, status %8.8x, resetting...\n",
2355 sh_eth_read(ndev
, EESR
));
2357 /* tx_errors count up */
2358 ndev
->stats
.tx_errors
++;
2360 /* Free all the skbuffs in the Rx queue. */
2361 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
2362 rxdesc
= &mdp
->rx_ring
[i
];
2363 rxdesc
->status
= cpu_to_edmac(mdp
, 0);
2364 rxdesc
->addr
= cpu_to_edmac(mdp
, 0xBADF00D0);
2365 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
2366 mdp
->rx_skbuff
[i
] = NULL
;
2368 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
2369 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
2370 mdp
->tx_skbuff
[i
] = NULL
;
2374 sh_eth_dev_init(ndev
, true);
2377 /* Packet transmit function */
2378 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
2380 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2381 struct sh_eth_txdesc
*txdesc
;
2382 dma_addr_t dma_addr
;
2384 unsigned long flags
;
2386 spin_lock_irqsave(&mdp
->lock
, flags
);
2387 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (mdp
->num_tx_ring
- 4)) {
2388 if (!sh_eth_txfree(ndev
)) {
2389 netif_warn(mdp
, tx_queued
, ndev
, "TxFD exhausted.\n");
2390 netif_stop_queue(ndev
);
2391 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2392 return NETDEV_TX_BUSY
;
2395 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2397 if (skb_put_padto(skb
, ETH_ZLEN
))
2398 return NETDEV_TX_OK
;
2400 entry
= mdp
->cur_tx
% mdp
->num_tx_ring
;
2401 mdp
->tx_skbuff
[entry
] = skb
;
2402 txdesc
= &mdp
->tx_ring
[entry
];
2404 if (!mdp
->cd
->hw_swap
)
2405 sh_eth_soft_swap(PTR_ALIGN(skb
->data
, 4), skb
->len
+ 2);
2406 dma_addr
= dma_map_single(&ndev
->dev
, skb
->data
, skb
->len
,
2408 if (dma_mapping_error(&ndev
->dev
, dma_addr
)) {
2410 return NETDEV_TX_OK
;
2412 txdesc
->addr
= cpu_to_edmac(mdp
, dma_addr
);
2413 txdesc
->len
= cpu_to_edmac(mdp
, skb
->len
<< 16);
2415 dma_wmb(); /* TACT bit must be set after all the above writes */
2416 if (entry
>= mdp
->num_tx_ring
- 1)
2417 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
| TD_TDLE
);
2419 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
);
2423 if (!(sh_eth_read(ndev
, EDTRR
) & sh_eth_get_edtrr_trns(mdp
)))
2424 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
2426 return NETDEV_TX_OK
;
2429 /* The statistics registers have write-clear behaviour, which means we
2430 * will lose any increment between the read and write. We mitigate
2431 * this by only clearing when we read a non-zero value, so we will
2432 * never falsely report a total of zero.
2435 sh_eth_update_stat(struct net_device
*ndev
, unsigned long *stat
, int reg
)
2437 u32 delta
= sh_eth_read(ndev
, reg
);
2441 sh_eth_write(ndev
, 0, reg
);
2445 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
2447 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2449 if (sh_eth_is_rz_fast_ether(mdp
))
2450 return &ndev
->stats
;
2452 if (!mdp
->is_opened
)
2453 return &ndev
->stats
;
2455 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_dropped
, TROCR
);
2456 sh_eth_update_stat(ndev
, &ndev
->stats
.collisions
, CDCR
);
2457 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
, LCCR
);
2459 if (sh_eth_is_gether(mdp
)) {
2460 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2462 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2465 sh_eth_update_stat(ndev
, &ndev
->stats
.tx_carrier_errors
,
2469 return &ndev
->stats
;
2472 /* device close function */
2473 static int sh_eth_close(struct net_device
*ndev
)
2475 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2477 netif_stop_queue(ndev
);
2479 /* Serialise with the interrupt handler and NAPI, then disable
2480 * interrupts. We have to clear the irq_enabled flag first to
2481 * ensure that interrupts won't be re-enabled.
2483 mdp
->irq_enabled
= false;
2484 synchronize_irq(ndev
->irq
);
2485 napi_disable(&mdp
->napi
);
2486 sh_eth_write(ndev
, 0x0000, EESIPR
);
2488 sh_eth_dev_exit(ndev
);
2490 /* PHY Disconnect */
2492 phy_stop(mdp
->phydev
);
2493 phy_disconnect(mdp
->phydev
);
2497 free_irq(ndev
->irq
, ndev
);
2499 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
2500 sh_eth_ring_free(ndev
);
2502 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2509 /* ioctl to device function */
2510 static int sh_eth_do_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
2512 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2513 struct phy_device
*phydev
= mdp
->phydev
;
2515 if (!netif_running(ndev
))
2521 return phy_mii_ioctl(phydev
, rq
, cmd
);
2524 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2525 static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private
*mdp
,
2528 return sh_eth_tsu_get_offset(mdp
, TSU_POST1
) + (entry
/ 8 * 4);
2531 static u32
sh_eth_tsu_get_post_mask(int entry
)
2533 return 0x0f << (28 - ((entry
% 8) * 4));
2536 static u32
sh_eth_tsu_get_post_bit(struct sh_eth_private
*mdp
, int entry
)
2538 return (0x08 >> (mdp
->port
<< 1)) << (28 - ((entry
% 8) * 4));
2541 static void sh_eth_tsu_enable_cam_entry_post(struct net_device
*ndev
,
2544 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2548 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2549 tmp
= ioread32(reg_offset
);
2550 iowrite32(tmp
| sh_eth_tsu_get_post_bit(mdp
, entry
), reg_offset
);
2553 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device
*ndev
,
2556 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2557 u32 post_mask
, ref_mask
, tmp
;
2560 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2561 post_mask
= sh_eth_tsu_get_post_mask(entry
);
2562 ref_mask
= sh_eth_tsu_get_post_bit(mdp
, entry
) & ~post_mask
;
2564 tmp
= ioread32(reg_offset
);
2565 iowrite32(tmp
& ~post_mask
, reg_offset
);
2567 /* If other port enables, the function returns "true" */
2568 return tmp
& ref_mask
;
2571 static int sh_eth_tsu_busy(struct net_device
*ndev
)
2573 int timeout
= SH_ETH_TSU_TIMEOUT_MS
* 100;
2574 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2576 while ((sh_eth_tsu_read(mdp
, TSU_ADSBSY
) & TSU_ADSBSY_0
)) {
2580 netdev_err(ndev
, "%s: timeout\n", __func__
);
2588 static int sh_eth_tsu_write_entry(struct net_device
*ndev
, void *reg
,
2593 val
= addr
[0] << 24 | addr
[1] << 16 | addr
[2] << 8 | addr
[3];
2594 iowrite32(val
, reg
);
2595 if (sh_eth_tsu_busy(ndev
) < 0)
2598 val
= addr
[4] << 8 | addr
[5];
2599 iowrite32(val
, reg
+ 4);
2600 if (sh_eth_tsu_busy(ndev
) < 0)
2606 static void sh_eth_tsu_read_entry(void *reg
, u8
*addr
)
2610 val
= ioread32(reg
);
2611 addr
[0] = (val
>> 24) & 0xff;
2612 addr
[1] = (val
>> 16) & 0xff;
2613 addr
[2] = (val
>> 8) & 0xff;
2614 addr
[3] = val
& 0xff;
2615 val
= ioread32(reg
+ 4);
2616 addr
[4] = (val
>> 8) & 0xff;
2617 addr
[5] = val
& 0xff;
2621 static int sh_eth_tsu_find_entry(struct net_device
*ndev
, const u8
*addr
)
2623 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2624 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2626 u8 c_addr
[ETH_ALEN
];
2628 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2629 sh_eth_tsu_read_entry(reg_offset
, c_addr
);
2630 if (ether_addr_equal(addr
, c_addr
))
2637 static int sh_eth_tsu_find_empty(struct net_device
*ndev
)
2642 memset(blank
, 0, sizeof(blank
));
2643 entry
= sh_eth_tsu_find_entry(ndev
, blank
);
2644 return (entry
< 0) ? -ENOMEM
: entry
;
2647 static int sh_eth_tsu_disable_cam_entry_table(struct net_device
*ndev
,
2650 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2651 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2655 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) &
2656 ~(1 << (31 - entry
)), TSU_TEN
);
2658 memset(blank
, 0, sizeof(blank
));
2659 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ entry
* 8, blank
);
2665 static int sh_eth_tsu_add_entry(struct net_device
*ndev
, const u8
*addr
)
2667 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2668 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2674 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2676 /* No entry found, create one */
2677 i
= sh_eth_tsu_find_empty(ndev
);
2680 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ i
* 8, addr
);
2684 /* Enable the entry */
2685 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) |
2686 (1 << (31 - i
)), TSU_TEN
);
2689 /* Entry found or created, enable POST */
2690 sh_eth_tsu_enable_cam_entry_post(ndev
, i
);
2695 static int sh_eth_tsu_del_entry(struct net_device
*ndev
, const u8
*addr
)
2697 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2703 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2706 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2709 /* Disable the entry if both ports was disabled */
2710 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2718 static int sh_eth_tsu_purge_all(struct net_device
*ndev
)
2720 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2726 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++) {
2727 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2730 /* Disable the entry if both ports was disabled */
2731 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2739 static void sh_eth_tsu_purge_mcast(struct net_device
*ndev
)
2741 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2743 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2749 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2750 sh_eth_tsu_read_entry(reg_offset
, addr
);
2751 if (is_multicast_ether_addr(addr
))
2752 sh_eth_tsu_del_entry(ndev
, addr
);
2756 /* Update promiscuous flag and multicast filter */
2757 static void sh_eth_set_rx_mode(struct net_device
*ndev
)
2759 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2762 unsigned long flags
;
2764 spin_lock_irqsave(&mdp
->lock
, flags
);
2765 /* Initial condition is MCT = 1, PRM = 0.
2766 * Depending on ndev->flags, set PRM or clear MCT
2768 ecmr_bits
= sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
;
2770 ecmr_bits
|= ECMR_MCT
;
2772 if (!(ndev
->flags
& IFF_MULTICAST
)) {
2773 sh_eth_tsu_purge_mcast(ndev
);
2776 if (ndev
->flags
& IFF_ALLMULTI
) {
2777 sh_eth_tsu_purge_mcast(ndev
);
2778 ecmr_bits
&= ~ECMR_MCT
;
2782 if (ndev
->flags
& IFF_PROMISC
) {
2783 sh_eth_tsu_purge_all(ndev
);
2784 ecmr_bits
= (ecmr_bits
& ~ECMR_MCT
) | ECMR_PRM
;
2785 } else if (mdp
->cd
->tsu
) {
2786 struct netdev_hw_addr
*ha
;
2787 netdev_for_each_mc_addr(ha
, ndev
) {
2788 if (mcast_all
&& is_multicast_ether_addr(ha
->addr
))
2791 if (sh_eth_tsu_add_entry(ndev
, ha
->addr
) < 0) {
2793 sh_eth_tsu_purge_mcast(ndev
);
2794 ecmr_bits
&= ~ECMR_MCT
;
2801 /* update the ethernet mode */
2802 sh_eth_write(ndev
, ecmr_bits
, ECMR
);
2804 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2807 static int sh_eth_get_vtag_index(struct sh_eth_private
*mdp
)
2815 static int sh_eth_vlan_rx_add_vid(struct net_device
*ndev
,
2816 __be16 proto
, u16 vid
)
2818 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2819 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2821 if (unlikely(!mdp
->cd
->tsu
))
2824 /* No filtering if vid = 0 */
2828 mdp
->vlan_num_ids
++;
2830 /* The controller has one VLAN tag HW filter. So, if the filter is
2831 * already enabled, the driver disables it and the filte
2833 if (mdp
->vlan_num_ids
> 1) {
2834 /* disable VLAN filter */
2835 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2839 sh_eth_tsu_write(mdp
, TSU_VTAG_ENABLE
| (vid
& TSU_VTAG_VID_MASK
),
2845 static int sh_eth_vlan_rx_kill_vid(struct net_device
*ndev
,
2846 __be16 proto
, u16 vid
)
2848 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2849 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2851 if (unlikely(!mdp
->cd
->tsu
))
2854 /* No filtering if vid = 0 */
2858 mdp
->vlan_num_ids
--;
2859 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2864 /* SuperH's TSU register init function */
2865 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
2867 if (sh_eth_is_rz_fast_ether(mdp
)) {
2868 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2872 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
2873 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
2874 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
2875 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
2876 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
2877 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
2878 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
2879 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
2880 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
2881 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
2882 if (sh_eth_is_gether(mdp
)) {
2883 sh_eth_tsu_write(mdp
, 0, TSU_QTAG0
); /* Disable QTAG(0->1) */
2884 sh_eth_tsu_write(mdp
, 0, TSU_QTAG1
); /* Disable QTAG(1->0) */
2886 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
2887 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
2889 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
2890 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
2891 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2892 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
2893 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
2894 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
2895 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
2898 /* MDIO bus release function */
2899 static int sh_mdio_release(struct sh_eth_private
*mdp
)
2901 /* unregister mdio bus */
2902 mdiobus_unregister(mdp
->mii_bus
);
2904 /* free bitbang info */
2905 free_mdio_bitbang(mdp
->mii_bus
);
2910 /* MDIO bus init function */
2911 static int sh_mdio_init(struct sh_eth_private
*mdp
,
2912 struct sh_eth_plat_data
*pd
)
2915 struct bb_info
*bitbang
;
2916 struct platform_device
*pdev
= mdp
->pdev
;
2917 struct device
*dev
= &mdp
->pdev
->dev
;
2919 /* create bit control struct for PHY */
2920 bitbang
= devm_kzalloc(dev
, sizeof(struct bb_info
), GFP_KERNEL
);
2925 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
2926 bitbang
->set_gate
= pd
->set_mdio_gate
;
2927 bitbang
->mdi_msk
= PIR_MDI
;
2928 bitbang
->mdo_msk
= PIR_MDO
;
2929 bitbang
->mmd_msk
= PIR_MMD
;
2930 bitbang
->mdc_msk
= PIR_MDC
;
2931 bitbang
->ctrl
.ops
= &bb_ops
;
2933 /* MII controller setting */
2934 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
2938 /* Hook up MII support for ethtool */
2939 mdp
->mii_bus
->name
= "sh_mii";
2940 mdp
->mii_bus
->parent
= dev
;
2941 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
2942 pdev
->name
, pdev
->id
);
2945 mdp
->mii_bus
->irq
= devm_kmalloc_array(dev
, PHY_MAX_ADDR
, sizeof(int),
2947 if (!mdp
->mii_bus
->irq
) {
2952 /* register MDIO bus */
2954 ret
= of_mdiobus_register(mdp
->mii_bus
, dev
->of_node
);
2956 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
2957 mdp
->mii_bus
->irq
[i
] = PHY_POLL
;
2958 if (pd
->phy_irq
> 0)
2959 mdp
->mii_bus
->irq
[pd
->phy
] = pd
->phy_irq
;
2961 ret
= mdiobus_register(mdp
->mii_bus
);
2970 free_mdio_bitbang(mdp
->mii_bus
);
2974 static const u16
*sh_eth_get_register_offset(int register_type
)
2976 const u16
*reg_offset
= NULL
;
2978 switch (register_type
) {
2979 case SH_ETH_REG_GIGABIT
:
2980 reg_offset
= sh_eth_offset_gigabit
;
2982 case SH_ETH_REG_FAST_RZ
:
2983 reg_offset
= sh_eth_offset_fast_rz
;
2985 case SH_ETH_REG_FAST_RCAR
:
2986 reg_offset
= sh_eth_offset_fast_rcar
;
2988 case SH_ETH_REG_FAST_SH4
:
2989 reg_offset
= sh_eth_offset_fast_sh4
;
2991 case SH_ETH_REG_FAST_SH3_SH2
:
2992 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
3001 static const struct net_device_ops sh_eth_netdev_ops
= {
3002 .ndo_open
= sh_eth_open
,
3003 .ndo_stop
= sh_eth_close
,
3004 .ndo_start_xmit
= sh_eth_start_xmit
,
3005 .ndo_get_stats
= sh_eth_get_stats
,
3006 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
3007 .ndo_tx_timeout
= sh_eth_tx_timeout
,
3008 .ndo_do_ioctl
= sh_eth_do_ioctl
,
3009 .ndo_validate_addr
= eth_validate_addr
,
3010 .ndo_set_mac_address
= eth_mac_addr
,
3011 .ndo_change_mtu
= eth_change_mtu
,
3014 static const struct net_device_ops sh_eth_netdev_ops_tsu
= {
3015 .ndo_open
= sh_eth_open
,
3016 .ndo_stop
= sh_eth_close
,
3017 .ndo_start_xmit
= sh_eth_start_xmit
,
3018 .ndo_get_stats
= sh_eth_get_stats
,
3019 .ndo_set_rx_mode
= sh_eth_set_rx_mode
,
3020 .ndo_vlan_rx_add_vid
= sh_eth_vlan_rx_add_vid
,
3021 .ndo_vlan_rx_kill_vid
= sh_eth_vlan_rx_kill_vid
,
3022 .ndo_tx_timeout
= sh_eth_tx_timeout
,
3023 .ndo_do_ioctl
= sh_eth_do_ioctl
,
3024 .ndo_validate_addr
= eth_validate_addr
,
3025 .ndo_set_mac_address
= eth_mac_addr
,
3026 .ndo_change_mtu
= eth_change_mtu
,
3030 static struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
3032 struct device_node
*np
= dev
->of_node
;
3033 struct sh_eth_plat_data
*pdata
;
3034 const char *mac_addr
;
3036 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
3040 pdata
->phy_interface
= of_get_phy_mode(np
);
3042 mac_addr
= of_get_mac_address(np
);
3044 memcpy(pdata
->mac_addr
, mac_addr
, ETH_ALEN
);
3046 pdata
->no_ether_link
=
3047 of_property_read_bool(np
, "renesas,no-ether-link");
3048 pdata
->ether_link_active_low
=
3049 of_property_read_bool(np
, "renesas,ether-link-active-low");
3054 static const struct of_device_id sh_eth_match_table
[] = {
3055 { .compatible
= "renesas,gether-r8a7740", .data
= &r8a7740_data
},
3056 { .compatible
= "renesas,ether-r8a7778", .data
= &r8a777x_data
},
3057 { .compatible
= "renesas,ether-r8a7779", .data
= &r8a777x_data
},
3058 { .compatible
= "renesas,ether-r8a7790", .data
= &r8a779x_data
},
3059 { .compatible
= "renesas,ether-r8a7791", .data
= &r8a779x_data
},
3060 { .compatible
= "renesas,ether-r8a7793", .data
= &r8a779x_data
},
3061 { .compatible
= "renesas,ether-r8a7794", .data
= &r8a779x_data
},
3062 { .compatible
= "renesas,ether-r7s72100", .data
= &r7s72100_data
},
3065 MODULE_DEVICE_TABLE(of
, sh_eth_match_table
);
3067 static inline struct sh_eth_plat_data
*sh_eth_parse_dt(struct device
*dev
)
3073 static int sh_eth_drv_probe(struct platform_device
*pdev
)
3076 struct resource
*res
;
3077 struct net_device
*ndev
= NULL
;
3078 struct sh_eth_private
*mdp
= NULL
;
3079 struct sh_eth_plat_data
*pd
= dev_get_platdata(&pdev
->dev
);
3080 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
3083 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
3085 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
3089 pm_runtime_enable(&pdev
->dev
);
3090 pm_runtime_get_sync(&pdev
->dev
);
3097 ret
= platform_get_irq(pdev
, 0);
3102 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
3104 mdp
= netdev_priv(ndev
);
3105 mdp
->num_tx_ring
= TX_RING_SIZE
;
3106 mdp
->num_rx_ring
= RX_RING_SIZE
;
3107 mdp
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
3108 if (IS_ERR(mdp
->addr
)) {
3109 ret
= PTR_ERR(mdp
->addr
);
3113 ndev
->base_addr
= res
->start
;
3115 spin_lock_init(&mdp
->lock
);
3118 if (pdev
->dev
.of_node
)
3119 pd
= sh_eth_parse_dt(&pdev
->dev
);
3121 dev_err(&pdev
->dev
, "no platform data\n");
3127 mdp
->phy_id
= pd
->phy
;
3128 mdp
->phy_interface
= pd
->phy_interface
;
3130 mdp
->edmac_endian
= pd
->edmac_endian
;
3131 mdp
->no_ether_link
= pd
->no_ether_link
;
3132 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
3136 mdp
->cd
= (struct sh_eth_cpu_data
*)id
->driver_data
;
3138 const struct of_device_id
*match
;
3140 match
= of_match_device(of_match_ptr(sh_eth_match_table
),
3142 mdp
->cd
= (struct sh_eth_cpu_data
*)match
->data
;
3144 mdp
->reg_offset
= sh_eth_get_register_offset(mdp
->cd
->register_type
);
3145 if (!mdp
->reg_offset
) {
3146 dev_err(&pdev
->dev
, "Unknown register type (%d)\n",
3147 mdp
->cd
->register_type
);
3151 sh_eth_set_default_cpu_data(mdp
->cd
);
3155 ndev
->netdev_ops
= &sh_eth_netdev_ops_tsu
;
3157 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
3158 ndev
->ethtool_ops
= &sh_eth_ethtool_ops
;
3159 ndev
->watchdog_timeo
= TX_TIMEOUT
;
3161 /* debug message level */
3162 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
3164 /* read and set MAC address */
3165 read_mac_address(ndev
, pd
->mac_addr
);
3166 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
3167 dev_warn(&pdev
->dev
,
3168 "no valid MAC address supplied, using a random one.\n");
3169 eth_hw_addr_random(ndev
);
3172 /* ioremap the TSU registers */
3174 struct resource
*rtsu
;
3175 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
3176 mdp
->tsu_addr
= devm_ioremap_resource(&pdev
->dev
, rtsu
);
3177 if (IS_ERR(mdp
->tsu_addr
)) {
3178 ret
= PTR_ERR(mdp
->tsu_addr
);
3181 mdp
->port
= devno
% 2;
3182 ndev
->features
= NETIF_F_HW_VLAN_CTAG_FILTER
;
3185 /* initialize first or needed device */
3186 if (!devno
|| pd
->needs_init
) {
3187 if (mdp
->cd
->chip_reset
)
3188 mdp
->cd
->chip_reset(ndev
);
3191 /* TSU init (Init only)*/
3192 sh_eth_tsu_init(mdp
);
3196 if (mdp
->cd
->rmiimode
)
3197 sh_eth_write(ndev
, 0x1, RMIIMODE
);
3200 ret
= sh_mdio_init(mdp
, pd
);
3202 dev_err(&ndev
->dev
, "failed to initialise MDIO\n");
3206 netif_napi_add(ndev
, &mdp
->napi
, sh_eth_poll
, 64);
3208 /* network device register */
3209 ret
= register_netdev(ndev
);
3213 /* print device information */
3214 netdev_info(ndev
, "Base address at 0x%x, %pM, IRQ %d.\n",
3215 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
3217 pm_runtime_put(&pdev
->dev
);
3218 platform_set_drvdata(pdev
, ndev
);
3223 netif_napi_del(&mdp
->napi
);
3224 sh_mdio_release(mdp
);
3231 pm_runtime_put(&pdev
->dev
);
3232 pm_runtime_disable(&pdev
->dev
);
3236 static int sh_eth_drv_remove(struct platform_device
*pdev
)
3238 struct net_device
*ndev
= platform_get_drvdata(pdev
);
3239 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
3241 unregister_netdev(ndev
);
3242 netif_napi_del(&mdp
->napi
);
3243 sh_mdio_release(mdp
);
3244 pm_runtime_disable(&pdev
->dev
);
3251 #ifdef CONFIG_PM_SLEEP
3252 static int sh_eth_suspend(struct device
*dev
)
3254 struct net_device
*ndev
= dev_get_drvdata(dev
);
3257 if (netif_running(ndev
)) {
3258 netif_device_detach(ndev
);
3259 ret
= sh_eth_close(ndev
);
3265 static int sh_eth_resume(struct device
*dev
)
3267 struct net_device
*ndev
= dev_get_drvdata(dev
);
3270 if (netif_running(ndev
)) {
3271 ret
= sh_eth_open(ndev
);
3274 netif_device_attach(ndev
);
3281 static int sh_eth_runtime_nop(struct device
*dev
)
3283 /* Runtime PM callback shared between ->runtime_suspend()
3284 * and ->runtime_resume(). Simply returns success.
3286 * This driver re-initializes all registers after
3287 * pm_runtime_get_sync() anyway so there is no need
3288 * to save and restore registers here.
3293 static const struct dev_pm_ops sh_eth_dev_pm_ops
= {
3294 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend
, sh_eth_resume
)
3295 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop
, sh_eth_runtime_nop
, NULL
)
3297 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3299 #define SH_ETH_PM_OPS NULL
3302 static struct platform_device_id sh_eth_id_table
[] = {
3303 { "sh7619-ether", (kernel_ulong_t
)&sh7619_data
},
3304 { "sh771x-ether", (kernel_ulong_t
)&sh771x_data
},
3305 { "sh7724-ether", (kernel_ulong_t
)&sh7724_data
},
3306 { "sh7734-gether", (kernel_ulong_t
)&sh7734_data
},
3307 { "sh7757-ether", (kernel_ulong_t
)&sh7757_data
},
3308 { "sh7757-gether", (kernel_ulong_t
)&sh7757_data_giga
},
3309 { "sh7763-gether", (kernel_ulong_t
)&sh7763_data
},
3310 { "r7s72100-ether", (kernel_ulong_t
)&r7s72100_data
},
3311 { "r8a7740-gether", (kernel_ulong_t
)&r8a7740_data
},
3312 { "r8a777x-ether", (kernel_ulong_t
)&r8a777x_data
},
3313 { "r8a7790-ether", (kernel_ulong_t
)&r8a779x_data
},
3314 { "r8a7791-ether", (kernel_ulong_t
)&r8a779x_data
},
3315 { "r8a7793-ether", (kernel_ulong_t
)&r8a779x_data
},
3316 { "r8a7794-ether", (kernel_ulong_t
)&r8a779x_data
},
3319 MODULE_DEVICE_TABLE(platform
, sh_eth_id_table
);
3321 static struct platform_driver sh_eth_driver
= {
3322 .probe
= sh_eth_drv_probe
,
3323 .remove
= sh_eth_drv_remove
,
3324 .id_table
= sh_eth_id_table
,
3327 .pm
= SH_ETH_PM_OPS
,
3328 .of_match_table
= of_match_ptr(sh_eth_match_table
),
3332 module_platform_driver(sh_eth_driver
);
3334 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3335 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3336 MODULE_LICENSE("GPL v2");