2 * SuperH Ethernet device driver
4 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
5 * Copyright (C) 2008-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 * The full GNU General Public License is included in this distribution in
21 * the file called "COPYING".
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/etherdevice.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/mdio-bitbang.h>
34 #include <linux/netdevice.h>
35 #include <linux/phy.h>
36 #include <linux/cache.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/slab.h>
40 #include <linux/ethtool.h>
41 #include <linux/if_vlan.h>
42 #include <linux/clk.h>
43 #include <linux/sh_eth.h>
47 #define SH_ETH_DEF_MSG_ENABLE \
53 static const u16 sh_eth_offset_gigabit
[SH_ETH_MAX_REGISTER_OFFSET
] = {
107 [TSU_CTRST
] = 0x0004,
108 [TSU_FWEN0
] = 0x0010,
109 [TSU_FWEN1
] = 0x0014,
111 [TSU_BSYSL0
] = 0x0020,
112 [TSU_BSYSL1
] = 0x0024,
113 [TSU_PRISL0
] = 0x0028,
114 [TSU_PRISL1
] = 0x002c,
115 [TSU_FWSL0
] = 0x0030,
116 [TSU_FWSL1
] = 0x0034,
117 [TSU_FWSLC
] = 0x0038,
118 [TSU_QTAG0
] = 0x0040,
119 [TSU_QTAG1
] = 0x0044,
121 [TSU_FWINMK
] = 0x0054,
122 [TSU_ADQT0
] = 0x0048,
123 [TSU_ADQT1
] = 0x004c,
124 [TSU_VTAG0
] = 0x0058,
125 [TSU_VTAG1
] = 0x005c,
126 [TSU_ADSBSY
] = 0x0060,
128 [TSU_POST1
] = 0x0070,
129 [TSU_POST2
] = 0x0074,
130 [TSU_POST3
] = 0x0078,
131 [TSU_POST4
] = 0x007c,
132 [TSU_ADRH0
] = 0x0100,
133 [TSU_ADRL0
] = 0x0104,
134 [TSU_ADRH31
] = 0x01f8,
135 [TSU_ADRL31
] = 0x01fc,
151 static const u16 sh_eth_offset_fast_rcar
[SH_ETH_MAX_REGISTER_OFFSET
] = {
197 static const u16 sh_eth_offset_fast_sh4
[SH_ETH_MAX_REGISTER_OFFSET
] = {
249 static const u16 sh_eth_offset_fast_sh3_sh2
[SH_ETH_MAX_REGISTER_OFFSET
] = {
275 [TSU_CTRST
] = 0x0004,
276 [TSU_FWEN0
] = 0x0010,
277 [TSU_FWEN1
] = 0x0014,
279 [TSU_BSYSL0
] = 0x0020,
280 [TSU_BSYSL1
] = 0x0024,
281 [TSU_PRISL0
] = 0x0028,
282 [TSU_PRISL1
] = 0x002c,
283 [TSU_FWSL0
] = 0x0030,
284 [TSU_FWSL1
] = 0x0034,
285 [TSU_FWSLC
] = 0x0038,
286 [TSU_QTAGM0
] = 0x0040,
287 [TSU_QTAGM1
] = 0x0044,
288 [TSU_ADQT0
] = 0x0048,
289 [TSU_ADQT1
] = 0x004c,
291 [TSU_FWINMK
] = 0x0054,
292 [TSU_ADSBSY
] = 0x0060,
294 [TSU_POST1
] = 0x0070,
295 [TSU_POST2
] = 0x0074,
296 [TSU_POST3
] = 0x0078,
297 [TSU_POST4
] = 0x007c,
312 [TSU_ADRH0
] = 0x0100,
313 [TSU_ADRL0
] = 0x0104,
314 [TSU_ADRL31
] = 0x01fc,
317 static int sh_eth_is_gether(struct sh_eth_private
*mdp
)
319 if (mdp
->reg_offset
== sh_eth_offset_gigabit
)
325 static void sh_eth_select_mii(struct net_device
*ndev
)
328 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
330 switch (mdp
->phy_interface
) {
331 case PHY_INTERFACE_MODE_GMII
:
334 case PHY_INTERFACE_MODE_MII
:
337 case PHY_INTERFACE_MODE_RMII
:
341 pr_warn("PHY interface mode was not setup. Set to MII.\n");
346 sh_eth_write(ndev
, value
, RMII_MII
);
349 static void sh_eth_set_duplex(struct net_device
*ndev
)
351 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
353 if (mdp
->duplex
) /* Full */
354 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
356 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
359 /* There is CPU dependent code */
360 static void sh_eth_set_rate_r8a777x(struct net_device
*ndev
)
362 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
364 switch (mdp
->speed
) {
365 case 10: /* 10BASE */
366 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_ELB
, ECMR
);
368 case 100:/* 100BASE */
369 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_ELB
, ECMR
);
377 static struct sh_eth_cpu_data r8a777x_data
= {
378 .set_duplex
= sh_eth_set_duplex
,
379 .set_rate
= sh_eth_set_rate_r8a777x
,
381 .register_type
= SH_ETH_REG_FAST_RCAR
,
383 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
384 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
385 .eesipr_value
= 0x01ff009f,
387 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
388 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
389 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
399 static struct sh_eth_cpu_data r8a7790_data
= {
400 .set_duplex
= sh_eth_set_duplex
,
401 .set_rate
= sh_eth_set_rate_r8a777x
,
403 .register_type
= SH_ETH_REG_FAST_RCAR
,
405 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
406 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
407 .eesipr_value
= 0x01ff009f,
409 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
410 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
411 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
422 static void sh_eth_set_rate_sh7724(struct net_device
*ndev
)
424 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
426 switch (mdp
->speed
) {
427 case 10: /* 10BASE */
428 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_RTM
, ECMR
);
430 case 100:/* 100BASE */
431 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_RTM
, ECMR
);
439 static struct sh_eth_cpu_data sh7724_data
= {
440 .set_duplex
= sh_eth_set_duplex
,
441 .set_rate
= sh_eth_set_rate_sh7724
,
443 .register_type
= SH_ETH_REG_FAST_SH4
,
445 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
446 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
447 .eesipr_value
= 0x01ff009f,
449 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
450 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
451 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
459 .rpadir_value
= 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
462 static void sh_eth_set_rate_sh7757(struct net_device
*ndev
)
464 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
466 switch (mdp
->speed
) {
467 case 10: /* 10BASE */
468 sh_eth_write(ndev
, 0, RTRATE
);
470 case 100:/* 100BASE */
471 sh_eth_write(ndev
, 1, RTRATE
);
479 static struct sh_eth_cpu_data sh7757_data
= {
480 .set_duplex
= sh_eth_set_duplex
,
481 .set_rate
= sh_eth_set_rate_sh7757
,
483 .register_type
= SH_ETH_REG_FAST_SH4
,
485 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
486 .rmcr_value
= 0x00000001,
488 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
489 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
490 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
493 .irq_flags
= IRQF_SHARED
,
500 .rpadir_value
= 2 << 16,
503 #define SH_GIGA_ETH_BASE 0xfee00000UL
504 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
505 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
506 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
509 unsigned long mahr
[2], malr
[2];
511 /* save MAHR and MALR */
512 for (i
= 0; i
< 2; i
++) {
513 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
514 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
518 iowrite32(ARSTR_ARSTR
, (void *)(SH_GIGA_ETH_BASE
+ 0x1800));
521 /* restore MAHR and MALR */
522 for (i
= 0; i
< 2; i
++) {
523 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
524 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
528 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
530 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
532 switch (mdp
->speed
) {
533 case 10: /* 10BASE */
534 sh_eth_write(ndev
, 0x00000000, GECMR
);
536 case 100:/* 100BASE */
537 sh_eth_write(ndev
, 0x00000010, GECMR
);
539 case 1000: /* 1000BASE */
540 sh_eth_write(ndev
, 0x00000020, GECMR
);
547 /* SH7757(GETHERC) */
548 static struct sh_eth_cpu_data sh7757_data_giga
= {
549 .chip_reset
= sh_eth_chip_reset_giga
,
550 .set_duplex
= sh_eth_set_duplex
,
551 .set_rate
= sh_eth_set_rate_giga
,
553 .register_type
= SH_ETH_REG_GIGABIT
,
555 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
556 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
557 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
559 .tx_check
= EESR_TC1
| EESR_FTC
,
560 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
561 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
563 .fdr_value
= 0x0000072f,
564 .rmcr_value
= 0x00000001,
566 .irq_flags
= IRQF_SHARED
,
573 .rpadir_value
= 2 << 16,
579 static void sh_eth_chip_reset(struct net_device
*ndev
)
581 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
584 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
588 static void sh_eth_set_rate_gether(struct net_device
*ndev
)
590 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
592 switch (mdp
->speed
) {
593 case 10: /* 10BASE */
594 sh_eth_write(ndev
, GECMR_10
, GECMR
);
596 case 100:/* 100BASE */
597 sh_eth_write(ndev
, GECMR_100
, GECMR
);
599 case 1000: /* 1000BASE */
600 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
608 static struct sh_eth_cpu_data sh7734_data
= {
609 .chip_reset
= sh_eth_chip_reset
,
610 .set_duplex
= sh_eth_set_duplex
,
611 .set_rate
= sh_eth_set_rate_gether
,
613 .register_type
= SH_ETH_REG_GIGABIT
,
615 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
616 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
617 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
619 .tx_check
= EESR_TC1
| EESR_FTC
,
620 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
621 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
637 static struct sh_eth_cpu_data sh7763_data
= {
638 .chip_reset
= sh_eth_chip_reset
,
639 .set_duplex
= sh_eth_set_duplex
,
640 .set_rate
= sh_eth_set_rate_gether
,
642 .register_type
= SH_ETH_REG_GIGABIT
,
644 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
645 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
646 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
648 .tx_check
= EESR_TC1
| EESR_FTC
,
649 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
| \
650 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
| \
661 .irq_flags
= IRQF_SHARED
,
664 static void sh_eth_chip_reset_r8a7740(struct net_device
*ndev
)
666 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
669 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
672 sh_eth_select_mii(ndev
);
676 static struct sh_eth_cpu_data r8a7740_data
= {
677 .chip_reset
= sh_eth_chip_reset_r8a7740
,
678 .set_duplex
= sh_eth_set_duplex
,
679 .set_rate
= sh_eth_set_rate_gether
,
681 .register_type
= SH_ETH_REG_GIGABIT
,
683 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
684 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
685 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
687 .tx_check
= EESR_TC1
| EESR_FTC
,
688 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
689 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
691 .fdr_value
= 0x0000070f,
692 .rmcr_value
= 0x00000001,
700 .rpadir_value
= 2 << 16,
708 static struct sh_eth_cpu_data sh7619_data
= {
709 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
711 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
719 static struct sh_eth_cpu_data sh771x_data
= {
720 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
722 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
726 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
729 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
731 if (!cd
->ecsipr_value
)
732 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
734 if (!cd
->fcftr_value
)
735 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
| \
736 DEFAULT_FIFO_F_D_RFD
;
739 cd
->fdr_value
= DEFAULT_FDR_INIT
;
742 cd
->rmcr_value
= DEFAULT_RMCR_VALUE
;
745 cd
->tx_check
= DEFAULT_TX_CHECK
;
747 if (!cd
->eesr_err_check
)
748 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
751 static int sh_eth_check_reset(struct net_device
*ndev
)
757 if (!(sh_eth_read(ndev
, EDMR
) & 0x3))
763 pr_err("Device reset failed\n");
769 static int sh_eth_reset(struct net_device
*ndev
)
771 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
774 if (sh_eth_is_gether(mdp
)) {
775 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
776 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
,
779 ret
= sh_eth_check_reset(ndev
);
784 sh_eth_write(ndev
, 0x0, TDLAR
);
785 sh_eth_write(ndev
, 0x0, TDFAR
);
786 sh_eth_write(ndev
, 0x0, TDFXR
);
787 sh_eth_write(ndev
, 0x0, TDFFR
);
788 sh_eth_write(ndev
, 0x0, RDLAR
);
789 sh_eth_write(ndev
, 0x0, RDFAR
);
790 sh_eth_write(ndev
, 0x0, RDFXR
);
791 sh_eth_write(ndev
, 0x0, RDFFR
);
793 /* Reset HW CRC register */
795 sh_eth_write(ndev
, 0x0, CSMR
);
797 /* Select MII mode */
798 if (mdp
->cd
->select_mii
)
799 sh_eth_select_mii(ndev
);
801 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_ETHER
,
804 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) & ~EDMR_SRST_ETHER
,
812 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
813 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
817 reserve
= SH4_SKB_RX_ALIGN
- ((u32
)skb
->data
& (SH4_SKB_RX_ALIGN
- 1));
819 skb_reserve(skb
, reserve
);
822 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
824 skb_reserve(skb
, SH2_SH3_SKB_RX_ALIGN
);
829 /* CPU <-> EDMAC endian convert */
830 static inline __u32
cpu_to_edmac(struct sh_eth_private
*mdp
, u32 x
)
832 switch (mdp
->edmac_endian
) {
833 case EDMAC_LITTLE_ENDIAN
:
834 return cpu_to_le32(x
);
835 case EDMAC_BIG_ENDIAN
:
836 return cpu_to_be32(x
);
841 static inline __u32
edmac_to_cpu(struct sh_eth_private
*mdp
, u32 x
)
843 switch (mdp
->edmac_endian
) {
844 case EDMAC_LITTLE_ENDIAN
:
845 return le32_to_cpu(x
);
846 case EDMAC_BIG_ENDIAN
:
847 return be32_to_cpu(x
);
853 * Program the hardware MAC address from dev->dev_addr.
855 static void update_mac_address(struct net_device
*ndev
)
858 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
859 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
861 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
865 * Get MAC address from SuperH MAC address register
867 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
868 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
869 * When you want use this device, you must set MAC address in bootloader.
872 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
874 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
875 memcpy(ndev
->dev_addr
, mac
, 6);
877 ndev
->dev_addr
[0] = (sh_eth_read(ndev
, MAHR
) >> 24);
878 ndev
->dev_addr
[1] = (sh_eth_read(ndev
, MAHR
) >> 16) & 0xFF;
879 ndev
->dev_addr
[2] = (sh_eth_read(ndev
, MAHR
) >> 8) & 0xFF;
880 ndev
->dev_addr
[3] = (sh_eth_read(ndev
, MAHR
) & 0xFF);
881 ndev
->dev_addr
[4] = (sh_eth_read(ndev
, MALR
) >> 8) & 0xFF;
882 ndev
->dev_addr
[5] = (sh_eth_read(ndev
, MALR
) & 0xFF);
886 static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private
*mdp
)
888 if (sh_eth_is_gether(mdp
))
889 return EDTRR_TRNS_GETHER
;
891 return EDTRR_TRNS_ETHER
;
895 void (*set_gate
)(void *addr
);
896 struct mdiobb_ctrl ctrl
;
898 u32 mmd_msk
;/* MMD */
905 static void bb_set(void *addr
, u32 msk
)
907 iowrite32(ioread32(addr
) | msk
, addr
);
911 static void bb_clr(void *addr
, u32 msk
)
913 iowrite32((ioread32(addr
) & ~msk
), addr
);
917 static int bb_read(void *addr
, u32 msk
)
919 return (ioread32(addr
) & msk
) != 0;
922 /* Data I/O pin control */
923 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
925 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
927 if (bitbang
->set_gate
)
928 bitbang
->set_gate(bitbang
->addr
);
931 bb_set(bitbang
->addr
, bitbang
->mmd_msk
);
933 bb_clr(bitbang
->addr
, bitbang
->mmd_msk
);
937 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
939 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
941 if (bitbang
->set_gate
)
942 bitbang
->set_gate(bitbang
->addr
);
945 bb_set(bitbang
->addr
, bitbang
->mdo_msk
);
947 bb_clr(bitbang
->addr
, bitbang
->mdo_msk
);
951 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
953 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
955 if (bitbang
->set_gate
)
956 bitbang
->set_gate(bitbang
->addr
);
958 return bb_read(bitbang
->addr
, bitbang
->mdi_msk
);
961 /* MDC pin control */
962 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
964 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
966 if (bitbang
->set_gate
)
967 bitbang
->set_gate(bitbang
->addr
);
970 bb_set(bitbang
->addr
, bitbang
->mdc_msk
);
972 bb_clr(bitbang
->addr
, bitbang
->mdc_msk
);
975 /* mdio bus control struct */
976 static struct mdiobb_ops bb_ops
= {
977 .owner
= THIS_MODULE
,
978 .set_mdc
= sh_mdc_ctrl
,
979 .set_mdio_dir
= sh_mmd_ctrl
,
980 .set_mdio_data
= sh_set_mdio
,
981 .get_mdio_data
= sh_get_mdio
,
984 /* free skb and descriptor buffer */
985 static void sh_eth_ring_free(struct net_device
*ndev
)
987 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
990 /* Free Rx skb ringbuffer */
991 if (mdp
->rx_skbuff
) {
992 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
993 if (mdp
->rx_skbuff
[i
])
994 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
997 kfree(mdp
->rx_skbuff
);
998 mdp
->rx_skbuff
= NULL
;
1000 /* Free Tx skb ringbuffer */
1001 if (mdp
->tx_skbuff
) {
1002 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1003 if (mdp
->tx_skbuff
[i
])
1004 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1007 kfree(mdp
->tx_skbuff
);
1008 mdp
->tx_skbuff
= NULL
;
1011 /* format skb and descriptor buffer */
1012 static void sh_eth_ring_format(struct net_device
*ndev
)
1014 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1016 struct sk_buff
*skb
;
1017 struct sh_eth_rxdesc
*rxdesc
= NULL
;
1018 struct sh_eth_txdesc
*txdesc
= NULL
;
1019 int rx_ringsize
= sizeof(*rxdesc
) * mdp
->num_rx_ring
;
1020 int tx_ringsize
= sizeof(*txdesc
) * mdp
->num_tx_ring
;
1022 mdp
->cur_rx
= mdp
->cur_tx
= 0;
1023 mdp
->dirty_rx
= mdp
->dirty_tx
= 0;
1025 memset(mdp
->rx_ring
, 0, rx_ringsize
);
1027 /* build Rx ring buffer */
1028 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1030 mdp
->rx_skbuff
[i
] = NULL
;
1031 skb
= netdev_alloc_skb(ndev
, mdp
->rx_buf_sz
);
1032 mdp
->rx_skbuff
[i
] = skb
;
1035 dma_map_single(&ndev
->dev
, skb
->data
, mdp
->rx_buf_sz
,
1037 sh_eth_set_receive_align(skb
);
1040 rxdesc
= &mdp
->rx_ring
[i
];
1041 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
1042 rxdesc
->status
= cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1044 /* The size of the buffer is 16 byte boundary. */
1045 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
1046 /* Rx descriptor address set */
1048 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
1049 if (sh_eth_is_gether(mdp
))
1050 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
1054 mdp
->dirty_rx
= (u32
) (i
- mdp
->num_rx_ring
);
1056 /* Mark the last entry as wrapping the ring. */
1057 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RDEL
);
1059 memset(mdp
->tx_ring
, 0, tx_ringsize
);
1061 /* build Tx ring buffer */
1062 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1063 mdp
->tx_skbuff
[i
] = NULL
;
1064 txdesc
= &mdp
->tx_ring
[i
];
1065 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1066 txdesc
->buffer_length
= 0;
1068 /* Tx descriptor address set */
1069 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
1070 if (sh_eth_is_gether(mdp
))
1071 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
1075 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1078 /* Get skb and descriptor buffer */
1079 static int sh_eth_ring_init(struct net_device
*ndev
)
1081 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1082 int rx_ringsize
, tx_ringsize
, ret
= 0;
1085 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1086 * card needs room to do 8 byte alignment, +2 so we can reserve
1087 * the first 2 bytes, and +16 gets room for the status word from the
1090 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
1091 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
1092 if (mdp
->cd
->rpadir
)
1093 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
1095 /* Allocate RX and TX skb rings */
1096 mdp
->rx_skbuff
= kmalloc_array(mdp
->num_rx_ring
,
1097 sizeof(*mdp
->rx_skbuff
), GFP_KERNEL
);
1098 if (!mdp
->rx_skbuff
) {
1103 mdp
->tx_skbuff
= kmalloc_array(mdp
->num_tx_ring
,
1104 sizeof(*mdp
->tx_skbuff
), GFP_KERNEL
);
1105 if (!mdp
->tx_skbuff
) {
1110 /* Allocate all Rx descriptors. */
1111 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1112 mdp
->rx_ring
= dma_alloc_coherent(NULL
, rx_ringsize
, &mdp
->rx_desc_dma
,
1114 if (!mdp
->rx_ring
) {
1116 goto desc_ring_free
;
1121 /* Allocate all Tx descriptors. */
1122 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1123 mdp
->tx_ring
= dma_alloc_coherent(NULL
, tx_ringsize
, &mdp
->tx_desc_dma
,
1125 if (!mdp
->tx_ring
) {
1127 goto desc_ring_free
;
1132 /* free DMA buffer */
1133 dma_free_coherent(NULL
, rx_ringsize
, mdp
->rx_ring
, mdp
->rx_desc_dma
);
1136 /* Free Rx and Tx skb ring buffer */
1137 sh_eth_ring_free(ndev
);
1138 mdp
->tx_ring
= NULL
;
1139 mdp
->rx_ring
= NULL
;
1144 static void sh_eth_free_dma_buffer(struct sh_eth_private
*mdp
)
1149 ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1150 dma_free_coherent(NULL
, ringsize
, mdp
->rx_ring
,
1152 mdp
->rx_ring
= NULL
;
1156 ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1157 dma_free_coherent(NULL
, ringsize
, mdp
->tx_ring
,
1159 mdp
->tx_ring
= NULL
;
1163 static int sh_eth_dev_init(struct net_device
*ndev
, bool start
)
1166 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1170 ret
= sh_eth_reset(ndev
);
1174 if (mdp
->cd
->rmiimode
)
1175 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1177 /* Descriptor format */
1178 sh_eth_ring_format(ndev
);
1179 if (mdp
->cd
->rpadir
)
1180 sh_eth_write(ndev
, mdp
->cd
->rpadir_value
, RPADIR
);
1182 /* all sh_eth int mask */
1183 sh_eth_write(ndev
, 0, EESIPR
);
1185 #if defined(__LITTLE_ENDIAN)
1186 if (mdp
->cd
->hw_swap
)
1187 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
1190 sh_eth_write(ndev
, 0, EDMR
);
1193 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
1194 sh_eth_write(ndev
, 0, TFTR
);
1196 /* Frame recv control */
1197 sh_eth_write(ndev
, mdp
->cd
->rmcr_value
, RMCR
);
1199 sh_eth_write(ndev
, DESC_I_RINT8
| DESC_I_RINT5
| DESC_I_TINT2
, TRSCER
);
1202 sh_eth_write(ndev
, 0x800, BCULR
); /* Burst sycle set */
1204 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
1206 if (!mdp
->cd
->no_trimd
)
1207 sh_eth_write(ndev
, 0, TRIMD
);
1209 /* Recv frame limit set register */
1210 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
1213 sh_eth_write(ndev
, sh_eth_read(ndev
, EESR
), EESR
);
1215 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1217 /* PAUSE Prohibition */
1218 val
= (sh_eth_read(ndev
, ECMR
) & ECMR_DM
) |
1219 ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) | ECMR_TE
| ECMR_RE
;
1221 sh_eth_write(ndev
, val
, ECMR
);
1223 if (mdp
->cd
->set_rate
)
1224 mdp
->cd
->set_rate(ndev
);
1226 /* E-MAC Status Register clear */
1227 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
1229 /* E-MAC Interrupt Enable register */
1231 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
1233 /* Set MAC address */
1234 update_mac_address(ndev
);
1238 sh_eth_write(ndev
, APR_AP
, APR
);
1240 sh_eth_write(ndev
, MPR_MP
, MPR
);
1241 if (mdp
->cd
->tpauser
)
1242 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
1245 /* Setting the Rx mode will start the Rx process. */
1246 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1248 netif_start_queue(ndev
);
1255 /* free Tx skb function */
1256 static int sh_eth_txfree(struct net_device
*ndev
)
1258 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1259 struct sh_eth_txdesc
*txdesc
;
1263 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
1264 entry
= mdp
->dirty_tx
% mdp
->num_tx_ring
;
1265 txdesc
= &mdp
->tx_ring
[entry
];
1266 if (txdesc
->status
& cpu_to_edmac(mdp
, TD_TACT
))
1268 /* Free the original skb. */
1269 if (mdp
->tx_skbuff
[entry
]) {
1270 dma_unmap_single(&ndev
->dev
, txdesc
->addr
,
1271 txdesc
->buffer_length
, DMA_TO_DEVICE
);
1272 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
1273 mdp
->tx_skbuff
[entry
] = NULL
;
1276 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1277 if (entry
>= mdp
->num_tx_ring
- 1)
1278 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1280 ndev
->stats
.tx_packets
++;
1281 ndev
->stats
.tx_bytes
+= txdesc
->buffer_length
;
1286 /* Packet receive function */
1287 static int sh_eth_rx(struct net_device
*ndev
, u32 intr_status
, int *quota
)
1289 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1290 struct sh_eth_rxdesc
*rxdesc
;
1292 int entry
= mdp
->cur_rx
% mdp
->num_rx_ring
;
1293 int boguscnt
= (mdp
->dirty_rx
+ mdp
->num_rx_ring
) - mdp
->cur_rx
;
1294 struct sk_buff
*skb
;
1299 rxdesc
= &mdp
->rx_ring
[entry
];
1300 while (!(rxdesc
->status
& cpu_to_edmac(mdp
, RD_RACT
))) {
1301 desc_status
= edmac_to_cpu(mdp
, rxdesc
->status
);
1302 pkt_len
= rxdesc
->frame_length
;
1313 if (!(desc_status
& RDFEND
))
1314 ndev
->stats
.rx_length_errors
++;
1317 * In case of almost all GETHER/ETHERs, the Receive Frame State
1318 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1319 * bit 0. However, in case of the R8A7740's GETHER, the RFS
1320 * bits are from bit 25 to bit 16. So, the driver needs right
1323 if (mdp
->cd
->shift_rd0
)
1326 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
1327 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
1328 ndev
->stats
.rx_errors
++;
1329 if (desc_status
& RD_RFS1
)
1330 ndev
->stats
.rx_crc_errors
++;
1331 if (desc_status
& RD_RFS2
)
1332 ndev
->stats
.rx_frame_errors
++;
1333 if (desc_status
& RD_RFS3
)
1334 ndev
->stats
.rx_length_errors
++;
1335 if (desc_status
& RD_RFS4
)
1336 ndev
->stats
.rx_length_errors
++;
1337 if (desc_status
& RD_RFS6
)
1338 ndev
->stats
.rx_missed_errors
++;
1339 if (desc_status
& RD_RFS10
)
1340 ndev
->stats
.rx_over_errors
++;
1342 if (!mdp
->cd
->hw_swap
)
1344 phys_to_virt(ALIGN(rxdesc
->addr
, 4)),
1346 skb
= mdp
->rx_skbuff
[entry
];
1347 mdp
->rx_skbuff
[entry
] = NULL
;
1348 if (mdp
->cd
->rpadir
)
1349 skb_reserve(skb
, NET_IP_ALIGN
);
1350 dma_sync_single_for_cpu(&ndev
->dev
, rxdesc
->addr
,
1353 skb_put(skb
, pkt_len
);
1354 skb
->protocol
= eth_type_trans(skb
, ndev
);
1355 netif_receive_skb(skb
);
1356 ndev
->stats
.rx_packets
++;
1357 ndev
->stats
.rx_bytes
+= pkt_len
;
1359 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RACT
);
1360 entry
= (++mdp
->cur_rx
) % mdp
->num_rx_ring
;
1361 rxdesc
= &mdp
->rx_ring
[entry
];
1364 /* Refill the Rx ring buffers. */
1365 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
1366 entry
= mdp
->dirty_rx
% mdp
->num_rx_ring
;
1367 rxdesc
= &mdp
->rx_ring
[entry
];
1368 /* The size of the buffer is 16 byte boundary. */
1369 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
1371 if (mdp
->rx_skbuff
[entry
] == NULL
) {
1372 skb
= netdev_alloc_skb(ndev
, mdp
->rx_buf_sz
);
1373 mdp
->rx_skbuff
[entry
] = skb
;
1375 break; /* Better luck next round. */
1376 dma_map_single(&ndev
->dev
, skb
->data
, mdp
->rx_buf_sz
,
1378 sh_eth_set_receive_align(skb
);
1380 skb_checksum_none_assert(skb
);
1381 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
1383 if (entry
>= mdp
->num_rx_ring
- 1)
1385 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
| RD_RDEL
);
1388 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1391 /* Restart Rx engine if stopped. */
1392 /* If we don't need to check status, don't. -KDU */
1393 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
)) {
1394 /* fix the values for the next receiving if RDE is set */
1395 if (intr_status
& EESR_RDE
)
1396 mdp
->cur_rx
= mdp
->dirty_rx
=
1397 (sh_eth_read(ndev
, RDFAR
) -
1398 sh_eth_read(ndev
, RDLAR
)) >> 4;
1399 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1405 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
1407 /* disable tx and rx */
1408 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) &
1409 ~(ECMR_RE
| ECMR_TE
), ECMR
);
1412 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
1414 /* enable tx and rx */
1415 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) |
1416 (ECMR_RE
| ECMR_TE
), ECMR
);
1419 /* error control function */
1420 static void sh_eth_error(struct net_device
*ndev
, int intr_status
)
1422 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1427 if (intr_status
& EESR_ECI
) {
1428 felic_stat
= sh_eth_read(ndev
, ECSR
);
1429 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1430 if (felic_stat
& ECSR_ICD
)
1431 ndev
->stats
.tx_carrier_errors
++;
1432 if (felic_stat
& ECSR_LCHNG
) {
1434 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
) {
1437 link_stat
= (sh_eth_read(ndev
, PSR
));
1438 if (mdp
->ether_link_active_low
)
1439 link_stat
= ~link_stat
;
1441 if (!(link_stat
& PHY_ST_LINK
))
1442 sh_eth_rcv_snd_disable(ndev
);
1445 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) &
1446 ~DMAC_M_ECI
, EESIPR
);
1448 sh_eth_write(ndev
, sh_eth_read(ndev
, ECSR
),
1450 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) |
1451 DMAC_M_ECI
, EESIPR
);
1452 /* enable tx and rx */
1453 sh_eth_rcv_snd_enable(ndev
);
1459 if (intr_status
& EESR_TWB
) {
1460 /* Unused write back interrupt */
1461 if (intr_status
& EESR_TABT
) { /* Transmit Abort int */
1462 ndev
->stats
.tx_aborted_errors
++;
1463 if (netif_msg_tx_err(mdp
))
1464 dev_err(&ndev
->dev
, "Transmit Abort\n");
1468 if (intr_status
& EESR_RABT
) {
1469 /* Receive Abort int */
1470 if (intr_status
& EESR_RFRMER
) {
1471 /* Receive Frame Overflow int */
1472 ndev
->stats
.rx_frame_errors
++;
1473 if (netif_msg_rx_err(mdp
))
1474 dev_err(&ndev
->dev
, "Receive Abort\n");
1478 if (intr_status
& EESR_TDE
) {
1479 /* Transmit Descriptor Empty int */
1480 ndev
->stats
.tx_fifo_errors
++;
1481 if (netif_msg_tx_err(mdp
))
1482 dev_err(&ndev
->dev
, "Transmit Descriptor Empty\n");
1485 if (intr_status
& EESR_TFE
) {
1486 /* FIFO under flow */
1487 ndev
->stats
.tx_fifo_errors
++;
1488 if (netif_msg_tx_err(mdp
))
1489 dev_err(&ndev
->dev
, "Transmit FIFO Under flow\n");
1492 if (intr_status
& EESR_RDE
) {
1493 /* Receive Descriptor Empty int */
1494 ndev
->stats
.rx_over_errors
++;
1496 if (netif_msg_rx_err(mdp
))
1497 dev_err(&ndev
->dev
, "Receive Descriptor Empty\n");
1500 if (intr_status
& EESR_RFE
) {
1501 /* Receive FIFO Overflow int */
1502 ndev
->stats
.rx_fifo_errors
++;
1503 if (netif_msg_rx_err(mdp
))
1504 dev_err(&ndev
->dev
, "Receive FIFO Overflow\n");
1507 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1509 ndev
->stats
.tx_fifo_errors
++;
1510 if (netif_msg_tx_err(mdp
))
1511 dev_err(&ndev
->dev
, "Address Error\n");
1514 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1515 if (mdp
->cd
->no_ade
)
1517 if (intr_status
& mask
) {
1519 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1521 dev_err(&ndev
->dev
, "TX error. status=%8.8x cur_tx=%8.8x ",
1522 intr_status
, mdp
->cur_tx
);
1523 dev_err(&ndev
->dev
, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1524 mdp
->dirty_tx
, (u32
) ndev
->state
, edtrr
);
1525 /* dirty buffer free */
1526 sh_eth_txfree(ndev
);
1529 if (edtrr
^ sh_eth_get_edtrr_trns(mdp
)) {
1531 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
1534 netif_wake_queue(ndev
);
1538 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1540 struct net_device
*ndev
= netdev
;
1541 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1542 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1543 irqreturn_t ret
= IRQ_NONE
;
1544 unsigned long intr_status
, intr_enable
;
1546 spin_lock(&mdp
->lock
);
1548 /* Get interrupt status */
1549 intr_status
= sh_eth_read(ndev
, EESR
);
1550 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1551 * enabled since it's the one that comes thru regardless of the mask,
1552 * and we need to fully handle it in sh_eth_error() in order to quench
1553 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1555 intr_enable
= sh_eth_read(ndev
, EESIPR
);
1556 intr_status
&= intr_enable
| DMAC_M_ECI
;
1557 if (intr_status
& (EESR_RX_CHECK
| cd
->tx_check
| cd
->eesr_err_check
))
1562 if (intr_status
& EESR_RX_CHECK
) {
1563 if (napi_schedule_prep(&mdp
->napi
)) {
1564 /* Mask Rx interrupts */
1565 sh_eth_write(ndev
, intr_enable
& ~EESR_RX_CHECK
,
1567 __napi_schedule(&mdp
->napi
);
1569 dev_warn(&ndev
->dev
,
1570 "ignoring interrupt, status 0x%08lx, mask 0x%08lx.\n",
1571 intr_status
, intr_enable
);
1576 if (intr_status
& cd
->tx_check
) {
1577 /* Clear Tx interrupts */
1578 sh_eth_write(ndev
, intr_status
& cd
->tx_check
, EESR
);
1580 sh_eth_txfree(ndev
);
1581 netif_wake_queue(ndev
);
1584 if (intr_status
& cd
->eesr_err_check
) {
1585 /* Clear error interrupts */
1586 sh_eth_write(ndev
, intr_status
& cd
->eesr_err_check
, EESR
);
1588 sh_eth_error(ndev
, intr_status
);
1592 spin_unlock(&mdp
->lock
);
1597 static int sh_eth_poll(struct napi_struct
*napi
, int budget
)
1599 struct sh_eth_private
*mdp
= container_of(napi
, struct sh_eth_private
,
1601 struct net_device
*ndev
= napi
->dev
;
1603 unsigned long intr_status
;
1606 intr_status
= sh_eth_read(ndev
, EESR
);
1607 if (!(intr_status
& EESR_RX_CHECK
))
1609 /* Clear Rx interrupts */
1610 sh_eth_write(ndev
, intr_status
& EESR_RX_CHECK
, EESR
);
1612 if (sh_eth_rx(ndev
, intr_status
, "a
))
1616 napi_complete(napi
);
1618 /* Reenable Rx interrupts */
1619 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1621 return budget
- quota
;
1624 /* PHY state control function */
1625 static void sh_eth_adjust_link(struct net_device
*ndev
)
1627 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1628 struct phy_device
*phydev
= mdp
->phydev
;
1632 if (phydev
->duplex
!= mdp
->duplex
) {
1634 mdp
->duplex
= phydev
->duplex
;
1635 if (mdp
->cd
->set_duplex
)
1636 mdp
->cd
->set_duplex(ndev
);
1639 if (phydev
->speed
!= mdp
->speed
) {
1641 mdp
->speed
= phydev
->speed
;
1642 if (mdp
->cd
->set_rate
)
1643 mdp
->cd
->set_rate(ndev
);
1647 (sh_eth_read(ndev
, ECMR
) & ~ECMR_TXF
), ECMR
);
1649 mdp
->link
= phydev
->link
;
1650 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1651 sh_eth_rcv_snd_enable(ndev
);
1653 } else if (mdp
->link
) {
1658 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1659 sh_eth_rcv_snd_disable(ndev
);
1662 if (new_state
&& netif_msg_link(mdp
))
1663 phy_print_status(phydev
);
1666 /* PHY init function */
1667 static int sh_eth_phy_init(struct net_device
*ndev
)
1669 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1670 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1671 struct phy_device
*phydev
= NULL
;
1673 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
1674 mdp
->mii_bus
->id
, mdp
->phy_id
);
1680 /* Try connect to PHY */
1681 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
1682 mdp
->phy_interface
);
1683 if (IS_ERR(phydev
)) {
1684 dev_err(&ndev
->dev
, "phy_connect failed\n");
1685 return PTR_ERR(phydev
);
1688 dev_info(&ndev
->dev
, "attached phy %i to driver %s\n",
1689 phydev
->addr
, phydev
->drv
->name
);
1691 mdp
->phydev
= phydev
;
1696 /* PHY control start function */
1697 static int sh_eth_phy_start(struct net_device
*ndev
)
1699 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1702 ret
= sh_eth_phy_init(ndev
);
1706 /* reset phy - this also wakes it from PDOWN */
1707 phy_write(mdp
->phydev
, MII_BMCR
, BMCR_RESET
);
1708 phy_start(mdp
->phydev
);
1713 static int sh_eth_get_settings(struct net_device
*ndev
,
1714 struct ethtool_cmd
*ecmd
)
1716 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1717 unsigned long flags
;
1720 spin_lock_irqsave(&mdp
->lock
, flags
);
1721 ret
= phy_ethtool_gset(mdp
->phydev
, ecmd
);
1722 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1727 static int sh_eth_set_settings(struct net_device
*ndev
,
1728 struct ethtool_cmd
*ecmd
)
1730 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1731 unsigned long flags
;
1734 spin_lock_irqsave(&mdp
->lock
, flags
);
1736 /* disable tx and rx */
1737 sh_eth_rcv_snd_disable(ndev
);
1739 ret
= phy_ethtool_sset(mdp
->phydev
, ecmd
);
1743 if (ecmd
->duplex
== DUPLEX_FULL
)
1748 if (mdp
->cd
->set_duplex
)
1749 mdp
->cd
->set_duplex(ndev
);
1754 /* enable tx and rx */
1755 sh_eth_rcv_snd_enable(ndev
);
1757 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1762 static int sh_eth_nway_reset(struct net_device
*ndev
)
1764 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1765 unsigned long flags
;
1768 spin_lock_irqsave(&mdp
->lock
, flags
);
1769 ret
= phy_start_aneg(mdp
->phydev
);
1770 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1775 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
1777 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1778 return mdp
->msg_enable
;
1781 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
1783 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1784 mdp
->msg_enable
= value
;
1787 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1788 "rx_current", "tx_current",
1789 "rx_dirty", "tx_dirty",
1791 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1793 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
1797 return SH_ETH_STATS_LEN
;
1803 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
1804 struct ethtool_stats
*stats
, u64
*data
)
1806 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1809 /* device-specific stats */
1810 data
[i
++] = mdp
->cur_rx
;
1811 data
[i
++] = mdp
->cur_tx
;
1812 data
[i
++] = mdp
->dirty_rx
;
1813 data
[i
++] = mdp
->dirty_tx
;
1816 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1818 switch (stringset
) {
1820 memcpy(data
, *sh_eth_gstrings_stats
,
1821 sizeof(sh_eth_gstrings_stats
));
1826 static void sh_eth_get_ringparam(struct net_device
*ndev
,
1827 struct ethtool_ringparam
*ring
)
1829 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1831 ring
->rx_max_pending
= RX_RING_MAX
;
1832 ring
->tx_max_pending
= TX_RING_MAX
;
1833 ring
->rx_pending
= mdp
->num_rx_ring
;
1834 ring
->tx_pending
= mdp
->num_tx_ring
;
1837 static int sh_eth_set_ringparam(struct net_device
*ndev
,
1838 struct ethtool_ringparam
*ring
)
1840 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1843 if (ring
->tx_pending
> TX_RING_MAX
||
1844 ring
->rx_pending
> RX_RING_MAX
||
1845 ring
->tx_pending
< TX_RING_MIN
||
1846 ring
->rx_pending
< RX_RING_MIN
)
1848 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
1851 if (netif_running(ndev
)) {
1852 netif_tx_disable(ndev
);
1853 /* Disable interrupts by clearing the interrupt mask. */
1854 sh_eth_write(ndev
, 0x0000, EESIPR
);
1855 /* Stop the chip's Tx and Rx processes. */
1856 sh_eth_write(ndev
, 0, EDTRR
);
1857 sh_eth_write(ndev
, 0, EDRRR
);
1858 synchronize_irq(ndev
->irq
);
1861 /* Free all the skbuffs in the Rx queue. */
1862 sh_eth_ring_free(ndev
);
1863 /* Free DMA buffer */
1864 sh_eth_free_dma_buffer(mdp
);
1866 /* Set new parameters */
1867 mdp
->num_rx_ring
= ring
->rx_pending
;
1868 mdp
->num_tx_ring
= ring
->tx_pending
;
1870 ret
= sh_eth_ring_init(ndev
);
1872 dev_err(&ndev
->dev
, "%s: sh_eth_ring_init failed.\n", __func__
);
1875 ret
= sh_eth_dev_init(ndev
, false);
1877 dev_err(&ndev
->dev
, "%s: sh_eth_dev_init failed.\n", __func__
);
1881 if (netif_running(ndev
)) {
1882 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1883 /* Setting the Rx mode will start the Rx process. */
1884 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1885 netif_wake_queue(ndev
);
1891 static const struct ethtool_ops sh_eth_ethtool_ops
= {
1892 .get_settings
= sh_eth_get_settings
,
1893 .set_settings
= sh_eth_set_settings
,
1894 .nway_reset
= sh_eth_nway_reset
,
1895 .get_msglevel
= sh_eth_get_msglevel
,
1896 .set_msglevel
= sh_eth_set_msglevel
,
1897 .get_link
= ethtool_op_get_link
,
1898 .get_strings
= sh_eth_get_strings
,
1899 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
1900 .get_sset_count
= sh_eth_get_sset_count
,
1901 .get_ringparam
= sh_eth_get_ringparam
,
1902 .set_ringparam
= sh_eth_set_ringparam
,
1905 /* network device open function */
1906 static int sh_eth_open(struct net_device
*ndev
)
1909 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1911 pm_runtime_get_sync(&mdp
->pdev
->dev
);
1913 napi_enable(&mdp
->napi
);
1915 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
1916 mdp
->cd
->irq_flags
, ndev
->name
, ndev
);
1918 dev_err(&ndev
->dev
, "Can not assign IRQ number\n");
1922 /* Descriptor set */
1923 ret
= sh_eth_ring_init(ndev
);
1928 ret
= sh_eth_dev_init(ndev
, true);
1932 /* PHY control start*/
1933 ret
= sh_eth_phy_start(ndev
);
1940 free_irq(ndev
->irq
, ndev
);
1942 napi_disable(&mdp
->napi
);
1943 pm_runtime_put_sync(&mdp
->pdev
->dev
);
1947 /* Timeout function */
1948 static void sh_eth_tx_timeout(struct net_device
*ndev
)
1950 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1951 struct sh_eth_rxdesc
*rxdesc
;
1954 netif_stop_queue(ndev
);
1956 if (netif_msg_timer(mdp
))
1957 dev_err(&ndev
->dev
, "%s: transmit timed out, status %8.8x,"
1958 " resetting...\n", ndev
->name
, (int)sh_eth_read(ndev
, EESR
));
1960 /* tx_errors count up */
1961 ndev
->stats
.tx_errors
++;
1963 /* Free all the skbuffs in the Rx queue. */
1964 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1965 rxdesc
= &mdp
->rx_ring
[i
];
1967 rxdesc
->addr
= 0xBADF00D0;
1968 if (mdp
->rx_skbuff
[i
])
1969 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1970 mdp
->rx_skbuff
[i
] = NULL
;
1972 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1973 if (mdp
->tx_skbuff
[i
])
1974 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1975 mdp
->tx_skbuff
[i
] = NULL
;
1979 sh_eth_dev_init(ndev
, true);
1982 /* Packet transmit function */
1983 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1985 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1986 struct sh_eth_txdesc
*txdesc
;
1988 unsigned long flags
;
1990 spin_lock_irqsave(&mdp
->lock
, flags
);
1991 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (mdp
->num_tx_ring
- 4)) {
1992 if (!sh_eth_txfree(ndev
)) {
1993 if (netif_msg_tx_queued(mdp
))
1994 dev_warn(&ndev
->dev
, "TxFD exhausted.\n");
1995 netif_stop_queue(ndev
);
1996 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1997 return NETDEV_TX_BUSY
;
2000 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2002 entry
= mdp
->cur_tx
% mdp
->num_tx_ring
;
2003 mdp
->tx_skbuff
[entry
] = skb
;
2004 txdesc
= &mdp
->tx_ring
[entry
];
2006 if (!mdp
->cd
->hw_swap
)
2007 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc
->addr
, 4)),
2009 txdesc
->addr
= dma_map_single(&ndev
->dev
, skb
->data
, skb
->len
,
2011 if (skb
->len
< ETHERSMALL
)
2012 txdesc
->buffer_length
= ETHERSMALL
;
2014 txdesc
->buffer_length
= skb
->len
;
2016 if (entry
>= mdp
->num_tx_ring
- 1)
2017 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
| TD_TDLE
);
2019 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
);
2023 if (!(sh_eth_read(ndev
, EDTRR
) & sh_eth_get_edtrr_trns(mdp
)))
2024 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
2026 return NETDEV_TX_OK
;
2029 /* device close function */
2030 static int sh_eth_close(struct net_device
*ndev
)
2032 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2034 netif_stop_queue(ndev
);
2036 /* Disable interrupts by clearing the interrupt mask. */
2037 sh_eth_write(ndev
, 0x0000, EESIPR
);
2039 /* Stop the chip's Tx and Rx processes. */
2040 sh_eth_write(ndev
, 0, EDTRR
);
2041 sh_eth_write(ndev
, 0, EDRRR
);
2043 /* PHY Disconnect */
2045 phy_stop(mdp
->phydev
);
2046 phy_disconnect(mdp
->phydev
);
2049 free_irq(ndev
->irq
, ndev
);
2051 napi_disable(&mdp
->napi
);
2053 /* Free all the skbuffs in the Rx queue. */
2054 sh_eth_ring_free(ndev
);
2056 /* free DMA buffer */
2057 sh_eth_free_dma_buffer(mdp
);
2059 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2064 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
2066 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2068 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2070 ndev
->stats
.tx_dropped
+= sh_eth_read(ndev
, TROCR
);
2071 sh_eth_write(ndev
, 0, TROCR
); /* (write clear) */
2072 ndev
->stats
.collisions
+= sh_eth_read(ndev
, CDCR
);
2073 sh_eth_write(ndev
, 0, CDCR
); /* (write clear) */
2074 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, LCCR
);
2075 sh_eth_write(ndev
, 0, LCCR
); /* (write clear) */
2076 if (sh_eth_is_gether(mdp
)) {
2077 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CERCR
);
2078 sh_eth_write(ndev
, 0, CERCR
); /* (write clear) */
2079 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CEECR
);
2080 sh_eth_write(ndev
, 0, CEECR
); /* (write clear) */
2082 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CNDCR
);
2083 sh_eth_write(ndev
, 0, CNDCR
); /* (write clear) */
2085 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2087 return &ndev
->stats
;
2090 /* ioctl to device function */
2091 static int sh_eth_do_ioctl(struct net_device
*ndev
, struct ifreq
*rq
,
2094 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2095 struct phy_device
*phydev
= mdp
->phydev
;
2097 if (!netif_running(ndev
))
2103 return phy_mii_ioctl(phydev
, rq
, cmd
);
2106 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2107 static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private
*mdp
,
2110 return sh_eth_tsu_get_offset(mdp
, TSU_POST1
) + (entry
/ 8 * 4);
2113 static u32
sh_eth_tsu_get_post_mask(int entry
)
2115 return 0x0f << (28 - ((entry
% 8) * 4));
2118 static u32
sh_eth_tsu_get_post_bit(struct sh_eth_private
*mdp
, int entry
)
2120 return (0x08 >> (mdp
->port
<< 1)) << (28 - ((entry
% 8) * 4));
2123 static void sh_eth_tsu_enable_cam_entry_post(struct net_device
*ndev
,
2126 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2130 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2131 tmp
= ioread32(reg_offset
);
2132 iowrite32(tmp
| sh_eth_tsu_get_post_bit(mdp
, entry
), reg_offset
);
2135 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device
*ndev
,
2138 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2139 u32 post_mask
, ref_mask
, tmp
;
2142 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2143 post_mask
= sh_eth_tsu_get_post_mask(entry
);
2144 ref_mask
= sh_eth_tsu_get_post_bit(mdp
, entry
) & ~post_mask
;
2146 tmp
= ioread32(reg_offset
);
2147 iowrite32(tmp
& ~post_mask
, reg_offset
);
2149 /* If other port enables, the function returns "true" */
2150 return tmp
& ref_mask
;
2153 static int sh_eth_tsu_busy(struct net_device
*ndev
)
2155 int timeout
= SH_ETH_TSU_TIMEOUT_MS
* 100;
2156 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2158 while ((sh_eth_tsu_read(mdp
, TSU_ADSBSY
) & TSU_ADSBSY_0
)) {
2162 dev_err(&ndev
->dev
, "%s: timeout\n", __func__
);
2170 static int sh_eth_tsu_write_entry(struct net_device
*ndev
, void *reg
,
2175 val
= addr
[0] << 24 | addr
[1] << 16 | addr
[2] << 8 | addr
[3];
2176 iowrite32(val
, reg
);
2177 if (sh_eth_tsu_busy(ndev
) < 0)
2180 val
= addr
[4] << 8 | addr
[5];
2181 iowrite32(val
, reg
+ 4);
2182 if (sh_eth_tsu_busy(ndev
) < 0)
2188 static void sh_eth_tsu_read_entry(void *reg
, u8
*addr
)
2192 val
= ioread32(reg
);
2193 addr
[0] = (val
>> 24) & 0xff;
2194 addr
[1] = (val
>> 16) & 0xff;
2195 addr
[2] = (val
>> 8) & 0xff;
2196 addr
[3] = val
& 0xff;
2197 val
= ioread32(reg
+ 4);
2198 addr
[4] = (val
>> 8) & 0xff;
2199 addr
[5] = val
& 0xff;
2203 static int sh_eth_tsu_find_entry(struct net_device
*ndev
, const u8
*addr
)
2205 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2206 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2208 u8 c_addr
[ETH_ALEN
];
2210 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2211 sh_eth_tsu_read_entry(reg_offset
, c_addr
);
2212 if (memcmp(addr
, c_addr
, ETH_ALEN
) == 0)
2219 static int sh_eth_tsu_find_empty(struct net_device
*ndev
)
2224 memset(blank
, 0, sizeof(blank
));
2225 entry
= sh_eth_tsu_find_entry(ndev
, blank
);
2226 return (entry
< 0) ? -ENOMEM
: entry
;
2229 static int sh_eth_tsu_disable_cam_entry_table(struct net_device
*ndev
,
2232 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2233 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2237 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) &
2238 ~(1 << (31 - entry
)), TSU_TEN
);
2240 memset(blank
, 0, sizeof(blank
));
2241 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ entry
* 8, blank
);
2247 static int sh_eth_tsu_add_entry(struct net_device
*ndev
, const u8
*addr
)
2249 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2250 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2256 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2258 /* No entry found, create one */
2259 i
= sh_eth_tsu_find_empty(ndev
);
2262 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ i
* 8, addr
);
2266 /* Enable the entry */
2267 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) |
2268 (1 << (31 - i
)), TSU_TEN
);
2271 /* Entry found or created, enable POST */
2272 sh_eth_tsu_enable_cam_entry_post(ndev
, i
);
2277 static int sh_eth_tsu_del_entry(struct net_device
*ndev
, const u8
*addr
)
2279 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2285 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2288 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2291 /* Disable the entry if both ports was disabled */
2292 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2300 static int sh_eth_tsu_purge_all(struct net_device
*ndev
)
2302 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2305 if (unlikely(!mdp
->cd
->tsu
))
2308 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++) {
2309 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2312 /* Disable the entry if both ports was disabled */
2313 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2321 static void sh_eth_tsu_purge_mcast(struct net_device
*ndev
)
2323 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2325 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2328 if (unlikely(!mdp
->cd
->tsu
))
2331 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2332 sh_eth_tsu_read_entry(reg_offset
, addr
);
2333 if (is_multicast_ether_addr(addr
))
2334 sh_eth_tsu_del_entry(ndev
, addr
);
2338 /* Multicast reception directions set */
2339 static void sh_eth_set_multicast_list(struct net_device
*ndev
)
2341 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2344 unsigned long flags
;
2346 spin_lock_irqsave(&mdp
->lock
, flags
);
2348 * Initial condition is MCT = 1, PRM = 0.
2349 * Depending on ndev->flags, set PRM or clear MCT
2351 ecmr_bits
= (sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
) | ECMR_MCT
;
2353 if (!(ndev
->flags
& IFF_MULTICAST
)) {
2354 sh_eth_tsu_purge_mcast(ndev
);
2357 if (ndev
->flags
& IFF_ALLMULTI
) {
2358 sh_eth_tsu_purge_mcast(ndev
);
2359 ecmr_bits
&= ~ECMR_MCT
;
2363 if (ndev
->flags
& IFF_PROMISC
) {
2364 sh_eth_tsu_purge_all(ndev
);
2365 ecmr_bits
= (ecmr_bits
& ~ECMR_MCT
) | ECMR_PRM
;
2366 } else if (mdp
->cd
->tsu
) {
2367 struct netdev_hw_addr
*ha
;
2368 netdev_for_each_mc_addr(ha
, ndev
) {
2369 if (mcast_all
&& is_multicast_ether_addr(ha
->addr
))
2372 if (sh_eth_tsu_add_entry(ndev
, ha
->addr
) < 0) {
2374 sh_eth_tsu_purge_mcast(ndev
);
2375 ecmr_bits
&= ~ECMR_MCT
;
2381 /* Normal, unicast/broadcast-only mode. */
2382 ecmr_bits
= (ecmr_bits
& ~ECMR_PRM
) | ECMR_MCT
;
2385 /* update the ethernet mode */
2386 sh_eth_write(ndev
, ecmr_bits
, ECMR
);
2388 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2391 static int sh_eth_get_vtag_index(struct sh_eth_private
*mdp
)
2399 static int sh_eth_vlan_rx_add_vid(struct net_device
*ndev
,
2400 __be16 proto
, u16 vid
)
2402 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2403 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2405 if (unlikely(!mdp
->cd
->tsu
))
2408 /* No filtering if vid = 0 */
2412 mdp
->vlan_num_ids
++;
2415 * The controller has one VLAN tag HW filter. So, if the filter is
2416 * already enabled, the driver disables it and the filte
2418 if (mdp
->vlan_num_ids
> 1) {
2419 /* disable VLAN filter */
2420 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2424 sh_eth_tsu_write(mdp
, TSU_VTAG_ENABLE
| (vid
& TSU_VTAG_VID_MASK
),
2430 static int sh_eth_vlan_rx_kill_vid(struct net_device
*ndev
,
2431 __be16 proto
, u16 vid
)
2433 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2434 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2436 if (unlikely(!mdp
->cd
->tsu
))
2439 /* No filtering if vid = 0 */
2443 mdp
->vlan_num_ids
--;
2444 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2449 /* SuperH's TSU register init function */
2450 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
2452 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
2453 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
2454 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
2455 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
2456 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
2457 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
2458 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
2459 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
2460 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
2461 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
2462 if (sh_eth_is_gether(mdp
)) {
2463 sh_eth_tsu_write(mdp
, 0, TSU_QTAG0
); /* Disable QTAG(0->1) */
2464 sh_eth_tsu_write(mdp
, 0, TSU_QTAG1
); /* Disable QTAG(1->0) */
2466 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
2467 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
2469 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
2470 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
2471 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2472 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
2473 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
2474 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
2475 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
2478 /* MDIO bus release function */
2479 static int sh_mdio_release(struct net_device
*ndev
)
2481 struct mii_bus
*bus
= dev_get_drvdata(&ndev
->dev
);
2483 /* unregister mdio bus */
2484 mdiobus_unregister(bus
);
2486 /* remove mdio bus info from net_device */
2487 dev_set_drvdata(&ndev
->dev
, NULL
);
2489 /* free bitbang info */
2490 free_mdio_bitbang(bus
);
2495 /* MDIO bus init function */
2496 static int sh_mdio_init(struct net_device
*ndev
, int id
,
2497 struct sh_eth_plat_data
*pd
)
2500 struct bb_info
*bitbang
;
2501 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2503 /* create bit control struct for PHY */
2504 bitbang
= devm_kzalloc(&ndev
->dev
, sizeof(struct bb_info
),
2512 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
2513 bitbang
->set_gate
= pd
->set_mdio_gate
;
2514 bitbang
->mdi_msk
= PIR_MDI
;
2515 bitbang
->mdo_msk
= PIR_MDO
;
2516 bitbang
->mmd_msk
= PIR_MMD
;
2517 bitbang
->mdc_msk
= PIR_MDC
;
2518 bitbang
->ctrl
.ops
= &bb_ops
;
2520 /* MII controller setting */
2521 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
2522 if (!mdp
->mii_bus
) {
2527 /* Hook up MII support for ethtool */
2528 mdp
->mii_bus
->name
= "sh_mii";
2529 mdp
->mii_bus
->parent
= &ndev
->dev
;
2530 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
2531 mdp
->pdev
->name
, id
);
2534 mdp
->mii_bus
->irq
= devm_kzalloc(&ndev
->dev
,
2535 sizeof(int) * PHY_MAX_ADDR
,
2537 if (!mdp
->mii_bus
->irq
) {
2542 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
2543 mdp
->mii_bus
->irq
[i
] = PHY_POLL
;
2545 /* register mdio bus */
2546 ret
= mdiobus_register(mdp
->mii_bus
);
2550 dev_set_drvdata(&ndev
->dev
, mdp
->mii_bus
);
2555 free_mdio_bitbang(mdp
->mii_bus
);
2561 static const u16
*sh_eth_get_register_offset(int register_type
)
2563 const u16
*reg_offset
= NULL
;
2565 switch (register_type
) {
2566 case SH_ETH_REG_GIGABIT
:
2567 reg_offset
= sh_eth_offset_gigabit
;
2569 case SH_ETH_REG_FAST_RCAR
:
2570 reg_offset
= sh_eth_offset_fast_rcar
;
2572 case SH_ETH_REG_FAST_SH4
:
2573 reg_offset
= sh_eth_offset_fast_sh4
;
2575 case SH_ETH_REG_FAST_SH3_SH2
:
2576 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
2579 pr_err("Unknown register type (%d)\n", register_type
);
2586 static const struct net_device_ops sh_eth_netdev_ops
= {
2587 .ndo_open
= sh_eth_open
,
2588 .ndo_stop
= sh_eth_close
,
2589 .ndo_start_xmit
= sh_eth_start_xmit
,
2590 .ndo_get_stats
= sh_eth_get_stats
,
2591 .ndo_tx_timeout
= sh_eth_tx_timeout
,
2592 .ndo_do_ioctl
= sh_eth_do_ioctl
,
2593 .ndo_validate_addr
= eth_validate_addr
,
2594 .ndo_set_mac_address
= eth_mac_addr
,
2595 .ndo_change_mtu
= eth_change_mtu
,
2598 static const struct net_device_ops sh_eth_netdev_ops_tsu
= {
2599 .ndo_open
= sh_eth_open
,
2600 .ndo_stop
= sh_eth_close
,
2601 .ndo_start_xmit
= sh_eth_start_xmit
,
2602 .ndo_get_stats
= sh_eth_get_stats
,
2603 .ndo_set_rx_mode
= sh_eth_set_multicast_list
,
2604 .ndo_vlan_rx_add_vid
= sh_eth_vlan_rx_add_vid
,
2605 .ndo_vlan_rx_kill_vid
= sh_eth_vlan_rx_kill_vid
,
2606 .ndo_tx_timeout
= sh_eth_tx_timeout
,
2607 .ndo_do_ioctl
= sh_eth_do_ioctl
,
2608 .ndo_validate_addr
= eth_validate_addr
,
2609 .ndo_set_mac_address
= eth_mac_addr
,
2610 .ndo_change_mtu
= eth_change_mtu
,
2613 static int sh_eth_drv_probe(struct platform_device
*pdev
)
2616 struct resource
*res
;
2617 struct net_device
*ndev
= NULL
;
2618 struct sh_eth_private
*mdp
= NULL
;
2619 struct sh_eth_plat_data
*pd
= dev_get_platdata(&pdev
->dev
);
2620 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
2623 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2624 if (unlikely(res
== NULL
)) {
2625 dev_err(&pdev
->dev
, "invalid resource\n");
2630 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
2636 /* The sh Ether-specific entries in the device structure. */
2637 ndev
->base_addr
= res
->start
;
2643 ret
= platform_get_irq(pdev
, 0);
2650 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2652 mdp
= netdev_priv(ndev
);
2653 mdp
->num_tx_ring
= TX_RING_SIZE
;
2654 mdp
->num_rx_ring
= RX_RING_SIZE
;
2655 mdp
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
2656 if (IS_ERR(mdp
->addr
)) {
2657 ret
= PTR_ERR(mdp
->addr
);
2661 spin_lock_init(&mdp
->lock
);
2663 pm_runtime_enable(&pdev
->dev
);
2664 pm_runtime_resume(&pdev
->dev
);
2667 mdp
->phy_id
= pd
->phy
;
2668 mdp
->phy_interface
= pd
->phy_interface
;
2670 mdp
->edmac_endian
= pd
->edmac_endian
;
2671 mdp
->no_ether_link
= pd
->no_ether_link
;
2672 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
2675 mdp
->cd
= (struct sh_eth_cpu_data
*)id
->driver_data
;
2676 mdp
->reg_offset
= sh_eth_get_register_offset(mdp
->cd
->register_type
);
2677 sh_eth_set_default_cpu_data(mdp
->cd
);
2681 ndev
->netdev_ops
= &sh_eth_netdev_ops_tsu
;
2683 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
2684 SET_ETHTOOL_OPS(ndev
, &sh_eth_ethtool_ops
);
2685 ndev
->watchdog_timeo
= TX_TIMEOUT
;
2687 /* debug message level */
2688 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
2690 /* read and set MAC address */
2691 read_mac_address(ndev
, pd
->mac_addr
);
2692 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
2693 dev_warn(&pdev
->dev
,
2694 "no valid MAC address supplied, using a random one.\n");
2695 eth_hw_addr_random(ndev
);
2698 /* ioremap the TSU registers */
2700 struct resource
*rtsu
;
2701 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2702 mdp
->tsu_addr
= devm_ioremap_resource(&pdev
->dev
, rtsu
);
2703 if (IS_ERR(mdp
->tsu_addr
)) {
2704 ret
= PTR_ERR(mdp
->tsu_addr
);
2707 mdp
->port
= devno
% 2;
2708 ndev
->features
= NETIF_F_HW_VLAN_CTAG_FILTER
;
2711 /* initialize first or needed device */
2712 if (!devno
|| pd
->needs_init
) {
2713 if (mdp
->cd
->chip_reset
)
2714 mdp
->cd
->chip_reset(ndev
);
2717 /* TSU init (Init only)*/
2718 sh_eth_tsu_init(mdp
);
2722 netif_napi_add(ndev
, &mdp
->napi
, sh_eth_poll
, 64);
2724 /* network device register */
2725 ret
= register_netdev(ndev
);
2730 ret
= sh_mdio_init(ndev
, pdev
->id
, pd
);
2732 goto out_unregister
;
2734 /* print device information */
2735 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2736 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
2738 platform_set_drvdata(pdev
, ndev
);
2743 unregister_netdev(ndev
);
2746 netif_napi_del(&mdp
->napi
);
2757 static int sh_eth_drv_remove(struct platform_device
*pdev
)
2759 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2760 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2762 sh_mdio_release(ndev
);
2763 unregister_netdev(ndev
);
2764 netif_napi_del(&mdp
->napi
);
2765 pm_runtime_disable(&pdev
->dev
);
2772 static int sh_eth_runtime_nop(struct device
*dev
)
2775 * Runtime PM callback shared between ->runtime_suspend()
2776 * and ->runtime_resume(). Simply returns success.
2778 * This driver re-initializes all registers after
2779 * pm_runtime_get_sync() anyway so there is no need
2780 * to save and restore registers here.
2785 static const struct dev_pm_ops sh_eth_dev_pm_ops
= {
2786 .runtime_suspend
= sh_eth_runtime_nop
,
2787 .runtime_resume
= sh_eth_runtime_nop
,
2789 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2791 #define SH_ETH_PM_OPS NULL
2794 static struct platform_device_id sh_eth_id_table
[] = {
2795 { "sh7619-ether", (kernel_ulong_t
)&sh7619_data
},
2796 { "sh771x-ether", (kernel_ulong_t
)&sh771x_data
},
2797 { "sh7724-ether", (kernel_ulong_t
)&sh7724_data
},
2798 { "sh7734-gether", (kernel_ulong_t
)&sh7734_data
},
2799 { "sh7757-ether", (kernel_ulong_t
)&sh7757_data
},
2800 { "sh7757-gether", (kernel_ulong_t
)&sh7757_data_giga
},
2801 { "sh7763-gether", (kernel_ulong_t
)&sh7763_data
},
2802 { "r8a7740-gether", (kernel_ulong_t
)&r8a7740_data
},
2803 { "r8a777x-ether", (kernel_ulong_t
)&r8a777x_data
},
2804 { "r8a7790-ether", (kernel_ulong_t
)&r8a7790_data
},
2807 MODULE_DEVICE_TABLE(platform
, sh_eth_id_table
);
2809 static struct platform_driver sh_eth_driver
= {
2810 .probe
= sh_eth_drv_probe
,
2811 .remove
= sh_eth_drv_remove
,
2812 .id_table
= sh_eth_id_table
,
2815 .pm
= SH_ETH_PM_OPS
,
2819 module_platform_driver(sh_eth_driver
);
2821 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2822 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2823 MODULE_LICENSE("GPL v2");