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
] = {
296 [TSU_CTRST
] = 0x0004,
297 [TSU_FWEN0
] = 0x0010,
298 [TSU_FWEN1
] = 0x0014,
300 [TSU_BSYSL0
] = 0x0020,
301 [TSU_BSYSL1
] = 0x0024,
302 [TSU_PRISL0
] = 0x0028,
303 [TSU_PRISL1
] = 0x002c,
304 [TSU_FWSL0
] = 0x0030,
305 [TSU_FWSL1
] = 0x0034,
306 [TSU_FWSLC
] = 0x0038,
307 [TSU_QTAGM0
] = 0x0040,
308 [TSU_QTAGM1
] = 0x0044,
309 [TSU_ADQT0
] = 0x0048,
310 [TSU_ADQT1
] = 0x004c,
312 [TSU_FWINMK
] = 0x0054,
313 [TSU_ADSBSY
] = 0x0060,
315 [TSU_POST1
] = 0x0070,
316 [TSU_POST2
] = 0x0074,
317 [TSU_POST3
] = 0x0078,
318 [TSU_POST4
] = 0x007c,
333 [TSU_ADRH0
] = 0x0100,
334 [TSU_ADRL0
] = 0x0104,
335 [TSU_ADRL31
] = 0x01fc,
338 static int sh_eth_is_gether(struct sh_eth_private
*mdp
)
340 if (mdp
->reg_offset
== sh_eth_offset_gigabit
)
346 static void sh_eth_select_mii(struct net_device
*ndev
)
349 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
351 switch (mdp
->phy_interface
) {
352 case PHY_INTERFACE_MODE_GMII
:
355 case PHY_INTERFACE_MODE_MII
:
358 case PHY_INTERFACE_MODE_RMII
:
362 pr_warn("PHY interface mode was not setup. Set to MII.\n");
367 sh_eth_write(ndev
, value
, RMII_MII
);
370 static void sh_eth_set_duplex(struct net_device
*ndev
)
372 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
374 if (mdp
->duplex
) /* Full */
375 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
377 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
380 /* There is CPU dependent code */
381 static void sh_eth_set_rate_r8a777x(struct net_device
*ndev
)
383 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
385 switch (mdp
->speed
) {
386 case 10: /* 10BASE */
387 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_ELB
, ECMR
);
389 case 100:/* 100BASE */
390 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_ELB
, ECMR
);
398 static struct sh_eth_cpu_data r8a777x_data
= {
399 .set_duplex
= sh_eth_set_duplex
,
400 .set_rate
= sh_eth_set_rate_r8a777x
,
402 .register_type
= SH_ETH_REG_FAST_RCAR
,
404 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
405 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
406 .eesipr_value
= 0x01ff009f,
408 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
409 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
410 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
420 static struct sh_eth_cpu_data r8a7790_data
= {
421 .set_duplex
= sh_eth_set_duplex
,
422 .set_rate
= sh_eth_set_rate_r8a777x
,
424 .register_type
= SH_ETH_REG_FAST_RCAR
,
426 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
427 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
428 .eesipr_value
= 0x01ff009f,
430 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
431 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
432 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
443 static void sh_eth_set_rate_sh7724(struct net_device
*ndev
)
445 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
447 switch (mdp
->speed
) {
448 case 10: /* 10BASE */
449 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_RTM
, ECMR
);
451 case 100:/* 100BASE */
452 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_RTM
, ECMR
);
460 static struct sh_eth_cpu_data sh7724_data
= {
461 .set_duplex
= sh_eth_set_duplex
,
462 .set_rate
= sh_eth_set_rate_sh7724
,
464 .register_type
= SH_ETH_REG_FAST_SH4
,
466 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
467 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
468 .eesipr_value
= 0x01ff009f,
470 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
471 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
472 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
480 .rpadir_value
= 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
483 static void sh_eth_set_rate_sh7757(struct net_device
*ndev
)
485 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
487 switch (mdp
->speed
) {
488 case 10: /* 10BASE */
489 sh_eth_write(ndev
, 0, RTRATE
);
491 case 100:/* 100BASE */
492 sh_eth_write(ndev
, 1, RTRATE
);
500 static struct sh_eth_cpu_data sh7757_data
= {
501 .set_duplex
= sh_eth_set_duplex
,
502 .set_rate
= sh_eth_set_rate_sh7757
,
504 .register_type
= SH_ETH_REG_FAST_SH4
,
506 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
508 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
509 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RFE
|
510 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
|
513 .irq_flags
= IRQF_SHARED
,
520 .rpadir_value
= 2 << 16,
523 #define SH_GIGA_ETH_BASE 0xfee00000UL
524 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
525 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
526 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
529 unsigned long mahr
[2], malr
[2];
531 /* save MAHR and MALR */
532 for (i
= 0; i
< 2; i
++) {
533 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
534 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
538 iowrite32(ARSTR_ARSTR
, (void *)(SH_GIGA_ETH_BASE
+ 0x1800));
541 /* restore MAHR and MALR */
542 for (i
= 0; i
< 2; i
++) {
543 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
544 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
548 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
550 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
552 switch (mdp
->speed
) {
553 case 10: /* 10BASE */
554 sh_eth_write(ndev
, 0x00000000, GECMR
);
556 case 100:/* 100BASE */
557 sh_eth_write(ndev
, 0x00000010, GECMR
);
559 case 1000: /* 1000BASE */
560 sh_eth_write(ndev
, 0x00000020, GECMR
);
567 /* SH7757(GETHERC) */
568 static struct sh_eth_cpu_data sh7757_data_giga
= {
569 .chip_reset
= sh_eth_chip_reset_giga
,
570 .set_duplex
= sh_eth_set_duplex
,
571 .set_rate
= sh_eth_set_rate_giga
,
573 .register_type
= SH_ETH_REG_GIGABIT
,
575 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
576 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
577 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
579 .tx_check
= EESR_TC1
| EESR_FTC
,
580 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
581 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
583 .fdr_value
= 0x0000072f,
585 .irq_flags
= IRQF_SHARED
,
592 .rpadir_value
= 2 << 16,
598 static void sh_eth_chip_reset(struct net_device
*ndev
)
600 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
603 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
607 static void sh_eth_set_rate_gether(struct net_device
*ndev
)
609 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
611 switch (mdp
->speed
) {
612 case 10: /* 10BASE */
613 sh_eth_write(ndev
, GECMR_10
, GECMR
);
615 case 100:/* 100BASE */
616 sh_eth_write(ndev
, GECMR_100
, GECMR
);
618 case 1000: /* 1000BASE */
619 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
627 static struct sh_eth_cpu_data sh7734_data
= {
628 .chip_reset
= sh_eth_chip_reset
,
629 .set_duplex
= sh_eth_set_duplex
,
630 .set_rate
= sh_eth_set_rate_gether
,
632 .register_type
= SH_ETH_REG_GIGABIT
,
634 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
635 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
636 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
638 .tx_check
= EESR_TC1
| EESR_FTC
,
639 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
640 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
656 static struct sh_eth_cpu_data sh7763_data
= {
657 .chip_reset
= sh_eth_chip_reset
,
658 .set_duplex
= sh_eth_set_duplex
,
659 .set_rate
= sh_eth_set_rate_gether
,
661 .register_type
= SH_ETH_REG_GIGABIT
,
663 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
664 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
665 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
667 .tx_check
= EESR_TC1
| EESR_FTC
,
668 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
| \
669 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
| \
680 .irq_flags
= IRQF_SHARED
,
683 static void sh_eth_chip_reset_r8a7740(struct net_device
*ndev
)
685 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
688 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
691 sh_eth_select_mii(ndev
);
695 static struct sh_eth_cpu_data r8a7740_data
= {
696 .chip_reset
= sh_eth_chip_reset_r8a7740
,
697 .set_duplex
= sh_eth_set_duplex
,
698 .set_rate
= sh_eth_set_rate_gether
,
700 .register_type
= SH_ETH_REG_GIGABIT
,
702 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
703 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
704 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
706 .tx_check
= EESR_TC1
| EESR_FTC
,
707 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
|
708 EESR_RFE
| EESR_RDE
| EESR_RFRMER
| EESR_TFE
|
710 .fdr_value
= 0x0000070f,
718 .rpadir_value
= 2 << 16,
726 static struct sh_eth_cpu_data sh7619_data
= {
727 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
729 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
737 static struct sh_eth_cpu_data sh771x_data
= {
738 .register_type
= SH_ETH_REG_FAST_SH3_SH2
,
740 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
744 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
747 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
749 if (!cd
->ecsipr_value
)
750 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
752 if (!cd
->fcftr_value
)
753 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
| \
754 DEFAULT_FIFO_F_D_RFD
;
757 cd
->fdr_value
= DEFAULT_FDR_INIT
;
760 cd
->tx_check
= DEFAULT_TX_CHECK
;
762 if (!cd
->eesr_err_check
)
763 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
766 static int sh_eth_check_reset(struct net_device
*ndev
)
772 if (!(sh_eth_read(ndev
, EDMR
) & 0x3))
778 pr_err("Device reset failed\n");
784 static int sh_eth_reset(struct net_device
*ndev
)
786 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
789 if (sh_eth_is_gether(mdp
)) {
790 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
791 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
,
794 ret
= sh_eth_check_reset(ndev
);
799 sh_eth_write(ndev
, 0x0, TDLAR
);
800 sh_eth_write(ndev
, 0x0, TDFAR
);
801 sh_eth_write(ndev
, 0x0, TDFXR
);
802 sh_eth_write(ndev
, 0x0, TDFFR
);
803 sh_eth_write(ndev
, 0x0, RDLAR
);
804 sh_eth_write(ndev
, 0x0, RDFAR
);
805 sh_eth_write(ndev
, 0x0, RDFXR
);
806 sh_eth_write(ndev
, 0x0, RDFFR
);
808 /* Reset HW CRC register */
810 sh_eth_write(ndev
, 0x0, CSMR
);
812 /* Select MII mode */
813 if (mdp
->cd
->select_mii
)
814 sh_eth_select_mii(ndev
);
816 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_ETHER
,
819 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) & ~EDMR_SRST_ETHER
,
827 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
828 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
832 reserve
= SH4_SKB_RX_ALIGN
- ((u32
)skb
->data
& (SH4_SKB_RX_ALIGN
- 1));
834 skb_reserve(skb
, reserve
);
837 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
839 skb_reserve(skb
, SH2_SH3_SKB_RX_ALIGN
);
844 /* CPU <-> EDMAC endian convert */
845 static inline __u32
cpu_to_edmac(struct sh_eth_private
*mdp
, u32 x
)
847 switch (mdp
->edmac_endian
) {
848 case EDMAC_LITTLE_ENDIAN
:
849 return cpu_to_le32(x
);
850 case EDMAC_BIG_ENDIAN
:
851 return cpu_to_be32(x
);
856 static inline __u32
edmac_to_cpu(struct sh_eth_private
*mdp
, u32 x
)
858 switch (mdp
->edmac_endian
) {
859 case EDMAC_LITTLE_ENDIAN
:
860 return le32_to_cpu(x
);
861 case EDMAC_BIG_ENDIAN
:
862 return be32_to_cpu(x
);
868 * Program the hardware MAC address from dev->dev_addr.
870 static void update_mac_address(struct net_device
*ndev
)
873 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
874 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
876 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
880 * Get MAC address from SuperH MAC address register
882 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
883 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
884 * When you want use this device, you must set MAC address in bootloader.
887 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
889 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
890 memcpy(ndev
->dev_addr
, mac
, 6);
892 ndev
->dev_addr
[0] = (sh_eth_read(ndev
, MAHR
) >> 24);
893 ndev
->dev_addr
[1] = (sh_eth_read(ndev
, MAHR
) >> 16) & 0xFF;
894 ndev
->dev_addr
[2] = (sh_eth_read(ndev
, MAHR
) >> 8) & 0xFF;
895 ndev
->dev_addr
[3] = (sh_eth_read(ndev
, MAHR
) & 0xFF);
896 ndev
->dev_addr
[4] = (sh_eth_read(ndev
, MALR
) >> 8) & 0xFF;
897 ndev
->dev_addr
[5] = (sh_eth_read(ndev
, MALR
) & 0xFF);
901 static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private
*mdp
)
903 if (sh_eth_is_gether(mdp
))
904 return EDTRR_TRNS_GETHER
;
906 return EDTRR_TRNS_ETHER
;
910 void (*set_gate
)(void *addr
);
911 struct mdiobb_ctrl ctrl
;
913 u32 mmd_msk
;/* MMD */
920 static void bb_set(void *addr
, u32 msk
)
922 iowrite32(ioread32(addr
) | msk
, addr
);
926 static void bb_clr(void *addr
, u32 msk
)
928 iowrite32((ioread32(addr
) & ~msk
), addr
);
932 static int bb_read(void *addr
, u32 msk
)
934 return (ioread32(addr
) & msk
) != 0;
937 /* Data I/O pin control */
938 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
940 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
942 if (bitbang
->set_gate
)
943 bitbang
->set_gate(bitbang
->addr
);
946 bb_set(bitbang
->addr
, bitbang
->mmd_msk
);
948 bb_clr(bitbang
->addr
, bitbang
->mmd_msk
);
952 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
954 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
956 if (bitbang
->set_gate
)
957 bitbang
->set_gate(bitbang
->addr
);
960 bb_set(bitbang
->addr
, bitbang
->mdo_msk
);
962 bb_clr(bitbang
->addr
, bitbang
->mdo_msk
);
966 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
968 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
970 if (bitbang
->set_gate
)
971 bitbang
->set_gate(bitbang
->addr
);
973 return bb_read(bitbang
->addr
, bitbang
->mdi_msk
);
976 /* MDC pin control */
977 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
979 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
981 if (bitbang
->set_gate
)
982 bitbang
->set_gate(bitbang
->addr
);
985 bb_set(bitbang
->addr
, bitbang
->mdc_msk
);
987 bb_clr(bitbang
->addr
, bitbang
->mdc_msk
);
990 /* mdio bus control struct */
991 static struct mdiobb_ops bb_ops
= {
992 .owner
= THIS_MODULE
,
993 .set_mdc
= sh_mdc_ctrl
,
994 .set_mdio_dir
= sh_mmd_ctrl
,
995 .set_mdio_data
= sh_set_mdio
,
996 .get_mdio_data
= sh_get_mdio
,
999 /* free skb and descriptor buffer */
1000 static void sh_eth_ring_free(struct net_device
*ndev
)
1002 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1005 /* Free Rx skb ringbuffer */
1006 if (mdp
->rx_skbuff
) {
1007 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1008 if (mdp
->rx_skbuff
[i
])
1009 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1012 kfree(mdp
->rx_skbuff
);
1013 mdp
->rx_skbuff
= NULL
;
1015 /* Free Tx skb ringbuffer */
1016 if (mdp
->tx_skbuff
) {
1017 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1018 if (mdp
->tx_skbuff
[i
])
1019 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1022 kfree(mdp
->tx_skbuff
);
1023 mdp
->tx_skbuff
= NULL
;
1026 /* format skb and descriptor buffer */
1027 static void sh_eth_ring_format(struct net_device
*ndev
)
1029 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1031 struct sk_buff
*skb
;
1032 struct sh_eth_rxdesc
*rxdesc
= NULL
;
1033 struct sh_eth_txdesc
*txdesc
= NULL
;
1034 int rx_ringsize
= sizeof(*rxdesc
) * mdp
->num_rx_ring
;
1035 int tx_ringsize
= sizeof(*txdesc
) * mdp
->num_tx_ring
;
1037 mdp
->cur_rx
= mdp
->cur_tx
= 0;
1038 mdp
->dirty_rx
= mdp
->dirty_tx
= 0;
1040 memset(mdp
->rx_ring
, 0, rx_ringsize
);
1042 /* build Rx ring buffer */
1043 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1045 mdp
->rx_skbuff
[i
] = NULL
;
1046 skb
= netdev_alloc_skb(ndev
, mdp
->rx_buf_sz
);
1047 mdp
->rx_skbuff
[i
] = skb
;
1050 dma_map_single(&ndev
->dev
, skb
->data
, mdp
->rx_buf_sz
,
1052 sh_eth_set_receive_align(skb
);
1055 rxdesc
= &mdp
->rx_ring
[i
];
1056 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
1057 rxdesc
->status
= cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1059 /* The size of the buffer is 16 byte boundary. */
1060 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
1061 /* Rx descriptor address set */
1063 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
1064 if (sh_eth_is_gether(mdp
))
1065 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
1069 mdp
->dirty_rx
= (u32
) (i
- mdp
->num_rx_ring
);
1071 /* Mark the last entry as wrapping the ring. */
1072 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RDEL
);
1074 memset(mdp
->tx_ring
, 0, tx_ringsize
);
1076 /* build Tx ring buffer */
1077 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1078 mdp
->tx_skbuff
[i
] = NULL
;
1079 txdesc
= &mdp
->tx_ring
[i
];
1080 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1081 txdesc
->buffer_length
= 0;
1083 /* Tx descriptor address set */
1084 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
1085 if (sh_eth_is_gether(mdp
))
1086 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
1090 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1093 /* Get skb and descriptor buffer */
1094 static int sh_eth_ring_init(struct net_device
*ndev
)
1096 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1097 int rx_ringsize
, tx_ringsize
, ret
= 0;
1100 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1101 * card needs room to do 8 byte alignment, +2 so we can reserve
1102 * the first 2 bytes, and +16 gets room for the status word from the
1105 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
1106 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
1107 if (mdp
->cd
->rpadir
)
1108 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
1110 /* Allocate RX and TX skb rings */
1111 mdp
->rx_skbuff
= kmalloc_array(mdp
->num_rx_ring
,
1112 sizeof(*mdp
->rx_skbuff
), GFP_KERNEL
);
1113 if (!mdp
->rx_skbuff
) {
1118 mdp
->tx_skbuff
= kmalloc_array(mdp
->num_tx_ring
,
1119 sizeof(*mdp
->tx_skbuff
), GFP_KERNEL
);
1120 if (!mdp
->tx_skbuff
) {
1125 /* Allocate all Rx descriptors. */
1126 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1127 mdp
->rx_ring
= dma_alloc_coherent(NULL
, rx_ringsize
, &mdp
->rx_desc_dma
,
1129 if (!mdp
->rx_ring
) {
1131 goto desc_ring_free
;
1136 /* Allocate all Tx descriptors. */
1137 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1138 mdp
->tx_ring
= dma_alloc_coherent(NULL
, tx_ringsize
, &mdp
->tx_desc_dma
,
1140 if (!mdp
->tx_ring
) {
1142 goto desc_ring_free
;
1147 /* free DMA buffer */
1148 dma_free_coherent(NULL
, rx_ringsize
, mdp
->rx_ring
, mdp
->rx_desc_dma
);
1151 /* Free Rx and Tx skb ring buffer */
1152 sh_eth_ring_free(ndev
);
1153 mdp
->tx_ring
= NULL
;
1154 mdp
->rx_ring
= NULL
;
1159 static void sh_eth_free_dma_buffer(struct sh_eth_private
*mdp
)
1164 ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1165 dma_free_coherent(NULL
, ringsize
, mdp
->rx_ring
,
1167 mdp
->rx_ring
= NULL
;
1171 ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1172 dma_free_coherent(NULL
, ringsize
, mdp
->tx_ring
,
1174 mdp
->tx_ring
= NULL
;
1178 static int sh_eth_dev_init(struct net_device
*ndev
, bool start
)
1181 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1185 ret
= sh_eth_reset(ndev
);
1189 if (mdp
->cd
->rmiimode
)
1190 sh_eth_write(ndev
, 0x1, RMIIMODE
);
1192 /* Descriptor format */
1193 sh_eth_ring_format(ndev
);
1194 if (mdp
->cd
->rpadir
)
1195 sh_eth_write(ndev
, mdp
->cd
->rpadir_value
, RPADIR
);
1197 /* all sh_eth int mask */
1198 sh_eth_write(ndev
, 0, EESIPR
);
1200 #if defined(__LITTLE_ENDIAN)
1201 if (mdp
->cd
->hw_swap
)
1202 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
1205 sh_eth_write(ndev
, 0, EDMR
);
1208 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
1209 sh_eth_write(ndev
, 0, TFTR
);
1211 /* Frame recv control (enable multiple-packets per rx irq) */
1212 sh_eth_write(ndev
, 0x00000001, RMCR
);
1214 sh_eth_write(ndev
, DESC_I_RINT8
| DESC_I_RINT5
| DESC_I_TINT2
, TRSCER
);
1217 sh_eth_write(ndev
, 0x800, BCULR
); /* Burst sycle set */
1219 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
1221 if (!mdp
->cd
->no_trimd
)
1222 sh_eth_write(ndev
, 0, TRIMD
);
1224 /* Recv frame limit set register */
1225 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
1228 sh_eth_write(ndev
, sh_eth_read(ndev
, EESR
), EESR
);
1230 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1232 /* PAUSE Prohibition */
1233 val
= (sh_eth_read(ndev
, ECMR
) & ECMR_DM
) |
1234 ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) | ECMR_TE
| ECMR_RE
;
1236 sh_eth_write(ndev
, val
, ECMR
);
1238 if (mdp
->cd
->set_rate
)
1239 mdp
->cd
->set_rate(ndev
);
1241 /* E-MAC Status Register clear */
1242 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
1244 /* E-MAC Interrupt Enable register */
1246 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
1248 /* Set MAC address */
1249 update_mac_address(ndev
);
1253 sh_eth_write(ndev
, APR_AP
, APR
);
1255 sh_eth_write(ndev
, MPR_MP
, MPR
);
1256 if (mdp
->cd
->tpauser
)
1257 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
1260 /* Setting the Rx mode will start the Rx process. */
1261 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1263 netif_start_queue(ndev
);
1270 /* free Tx skb function */
1271 static int sh_eth_txfree(struct net_device
*ndev
)
1273 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1274 struct sh_eth_txdesc
*txdesc
;
1278 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
1279 entry
= mdp
->dirty_tx
% mdp
->num_tx_ring
;
1280 txdesc
= &mdp
->tx_ring
[entry
];
1281 if (txdesc
->status
& cpu_to_edmac(mdp
, TD_TACT
))
1283 /* Free the original skb. */
1284 if (mdp
->tx_skbuff
[entry
]) {
1285 dma_unmap_single(&ndev
->dev
, txdesc
->addr
,
1286 txdesc
->buffer_length
, DMA_TO_DEVICE
);
1287 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
1288 mdp
->tx_skbuff
[entry
] = NULL
;
1291 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1292 if (entry
>= mdp
->num_tx_ring
- 1)
1293 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1295 ndev
->stats
.tx_packets
++;
1296 ndev
->stats
.tx_bytes
+= txdesc
->buffer_length
;
1301 /* Packet receive function */
1302 static int sh_eth_rx(struct net_device
*ndev
, u32 intr_status
, int *quota
)
1304 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1305 struct sh_eth_rxdesc
*rxdesc
;
1307 int entry
= mdp
->cur_rx
% mdp
->num_rx_ring
;
1308 int boguscnt
= (mdp
->dirty_rx
+ mdp
->num_rx_ring
) - mdp
->cur_rx
;
1309 struct sk_buff
*skb
;
1314 rxdesc
= &mdp
->rx_ring
[entry
];
1315 while (!(rxdesc
->status
& cpu_to_edmac(mdp
, RD_RACT
))) {
1316 desc_status
= edmac_to_cpu(mdp
, rxdesc
->status
);
1317 pkt_len
= rxdesc
->frame_length
;
1328 if (!(desc_status
& RDFEND
))
1329 ndev
->stats
.rx_length_errors
++;
1332 * In case of almost all GETHER/ETHERs, the Receive Frame State
1333 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
1334 * bit 0. However, in case of the R8A7740's GETHER, the RFS
1335 * bits are from bit 25 to bit 16. So, the driver needs right
1338 if (mdp
->cd
->shift_rd0
)
1341 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
1342 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
1343 ndev
->stats
.rx_errors
++;
1344 if (desc_status
& RD_RFS1
)
1345 ndev
->stats
.rx_crc_errors
++;
1346 if (desc_status
& RD_RFS2
)
1347 ndev
->stats
.rx_frame_errors
++;
1348 if (desc_status
& RD_RFS3
)
1349 ndev
->stats
.rx_length_errors
++;
1350 if (desc_status
& RD_RFS4
)
1351 ndev
->stats
.rx_length_errors
++;
1352 if (desc_status
& RD_RFS6
)
1353 ndev
->stats
.rx_missed_errors
++;
1354 if (desc_status
& RD_RFS10
)
1355 ndev
->stats
.rx_over_errors
++;
1357 if (!mdp
->cd
->hw_swap
)
1359 phys_to_virt(ALIGN(rxdesc
->addr
, 4)),
1361 skb
= mdp
->rx_skbuff
[entry
];
1362 mdp
->rx_skbuff
[entry
] = NULL
;
1363 if (mdp
->cd
->rpadir
)
1364 skb_reserve(skb
, NET_IP_ALIGN
);
1365 dma_sync_single_for_cpu(&ndev
->dev
, rxdesc
->addr
,
1368 skb_put(skb
, pkt_len
);
1369 skb
->protocol
= eth_type_trans(skb
, ndev
);
1370 netif_receive_skb(skb
);
1371 ndev
->stats
.rx_packets
++;
1372 ndev
->stats
.rx_bytes
+= pkt_len
;
1374 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RACT
);
1375 entry
= (++mdp
->cur_rx
) % mdp
->num_rx_ring
;
1376 rxdesc
= &mdp
->rx_ring
[entry
];
1379 /* Refill the Rx ring buffers. */
1380 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
1381 entry
= mdp
->dirty_rx
% mdp
->num_rx_ring
;
1382 rxdesc
= &mdp
->rx_ring
[entry
];
1383 /* The size of the buffer is 16 byte boundary. */
1384 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
1386 if (mdp
->rx_skbuff
[entry
] == NULL
) {
1387 skb
= netdev_alloc_skb(ndev
, mdp
->rx_buf_sz
);
1388 mdp
->rx_skbuff
[entry
] = skb
;
1390 break; /* Better luck next round. */
1391 dma_map_single(&ndev
->dev
, skb
->data
, mdp
->rx_buf_sz
,
1393 sh_eth_set_receive_align(skb
);
1395 skb_checksum_none_assert(skb
);
1396 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
1398 if (entry
>= mdp
->num_rx_ring
- 1)
1400 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
| RD_RDEL
);
1403 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1406 /* Restart Rx engine if stopped. */
1407 /* If we don't need to check status, don't. -KDU */
1408 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
)) {
1409 /* fix the values for the next receiving if RDE is set */
1410 if (intr_status
& EESR_RDE
)
1411 mdp
->cur_rx
= mdp
->dirty_rx
=
1412 (sh_eth_read(ndev
, RDFAR
) -
1413 sh_eth_read(ndev
, RDLAR
)) >> 4;
1414 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1420 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
1422 /* disable tx and rx */
1423 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) &
1424 ~(ECMR_RE
| ECMR_TE
), ECMR
);
1427 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
1429 /* enable tx and rx */
1430 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) |
1431 (ECMR_RE
| ECMR_TE
), ECMR
);
1434 /* error control function */
1435 static void sh_eth_error(struct net_device
*ndev
, int intr_status
)
1437 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1442 if (intr_status
& EESR_ECI
) {
1443 felic_stat
= sh_eth_read(ndev
, ECSR
);
1444 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1445 if (felic_stat
& ECSR_ICD
)
1446 ndev
->stats
.tx_carrier_errors
++;
1447 if (felic_stat
& ECSR_LCHNG
) {
1449 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
) {
1452 link_stat
= (sh_eth_read(ndev
, PSR
));
1453 if (mdp
->ether_link_active_low
)
1454 link_stat
= ~link_stat
;
1456 if (!(link_stat
& PHY_ST_LINK
))
1457 sh_eth_rcv_snd_disable(ndev
);
1460 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) &
1461 ~DMAC_M_ECI
, EESIPR
);
1463 sh_eth_write(ndev
, sh_eth_read(ndev
, ECSR
),
1465 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) |
1466 DMAC_M_ECI
, EESIPR
);
1467 /* enable tx and rx */
1468 sh_eth_rcv_snd_enable(ndev
);
1474 if (intr_status
& EESR_TWB
) {
1475 /* Unused write back interrupt */
1476 if (intr_status
& EESR_TABT
) { /* Transmit Abort int */
1477 ndev
->stats
.tx_aborted_errors
++;
1478 if (netif_msg_tx_err(mdp
))
1479 dev_err(&ndev
->dev
, "Transmit Abort\n");
1483 if (intr_status
& EESR_RABT
) {
1484 /* Receive Abort int */
1485 if (intr_status
& EESR_RFRMER
) {
1486 /* Receive Frame Overflow int */
1487 ndev
->stats
.rx_frame_errors
++;
1488 if (netif_msg_rx_err(mdp
))
1489 dev_err(&ndev
->dev
, "Receive Abort\n");
1493 if (intr_status
& EESR_TDE
) {
1494 /* Transmit Descriptor Empty int */
1495 ndev
->stats
.tx_fifo_errors
++;
1496 if (netif_msg_tx_err(mdp
))
1497 dev_err(&ndev
->dev
, "Transmit Descriptor Empty\n");
1500 if (intr_status
& EESR_TFE
) {
1501 /* FIFO under flow */
1502 ndev
->stats
.tx_fifo_errors
++;
1503 if (netif_msg_tx_err(mdp
))
1504 dev_err(&ndev
->dev
, "Transmit FIFO Under flow\n");
1507 if (intr_status
& EESR_RDE
) {
1508 /* Receive Descriptor Empty int */
1509 ndev
->stats
.rx_over_errors
++;
1511 if (netif_msg_rx_err(mdp
))
1512 dev_err(&ndev
->dev
, "Receive Descriptor Empty\n");
1515 if (intr_status
& EESR_RFE
) {
1516 /* Receive FIFO Overflow int */
1517 ndev
->stats
.rx_fifo_errors
++;
1518 if (netif_msg_rx_err(mdp
))
1519 dev_err(&ndev
->dev
, "Receive FIFO Overflow\n");
1522 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1524 ndev
->stats
.tx_fifo_errors
++;
1525 if (netif_msg_tx_err(mdp
))
1526 dev_err(&ndev
->dev
, "Address Error\n");
1529 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1530 if (mdp
->cd
->no_ade
)
1532 if (intr_status
& mask
) {
1534 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1536 dev_err(&ndev
->dev
, "TX error. status=%8.8x cur_tx=%8.8x ",
1537 intr_status
, mdp
->cur_tx
);
1538 dev_err(&ndev
->dev
, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1539 mdp
->dirty_tx
, (u32
) ndev
->state
, edtrr
);
1540 /* dirty buffer free */
1541 sh_eth_txfree(ndev
);
1544 if (edtrr
^ sh_eth_get_edtrr_trns(mdp
)) {
1546 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
1549 netif_wake_queue(ndev
);
1553 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1555 struct net_device
*ndev
= netdev
;
1556 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1557 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1558 irqreturn_t ret
= IRQ_NONE
;
1559 unsigned long intr_status
, intr_enable
;
1561 spin_lock(&mdp
->lock
);
1563 /* Get interrupt status */
1564 intr_status
= sh_eth_read(ndev
, EESR
);
1565 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1566 * enabled since it's the one that comes thru regardless of the mask,
1567 * and we need to fully handle it in sh_eth_error() in order to quench
1568 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1570 intr_enable
= sh_eth_read(ndev
, EESIPR
);
1571 intr_status
&= intr_enable
| DMAC_M_ECI
;
1572 if (intr_status
& (EESR_RX_CHECK
| cd
->tx_check
| cd
->eesr_err_check
))
1577 if (intr_status
& EESR_RX_CHECK
) {
1578 if (napi_schedule_prep(&mdp
->napi
)) {
1579 /* Mask Rx interrupts */
1580 sh_eth_write(ndev
, intr_enable
& ~EESR_RX_CHECK
,
1582 __napi_schedule(&mdp
->napi
);
1584 dev_warn(&ndev
->dev
,
1585 "ignoring interrupt, status 0x%08lx, mask 0x%08lx.\n",
1586 intr_status
, intr_enable
);
1591 if (intr_status
& cd
->tx_check
) {
1592 /* Clear Tx interrupts */
1593 sh_eth_write(ndev
, intr_status
& cd
->tx_check
, EESR
);
1595 sh_eth_txfree(ndev
);
1596 netif_wake_queue(ndev
);
1599 if (intr_status
& cd
->eesr_err_check
) {
1600 /* Clear error interrupts */
1601 sh_eth_write(ndev
, intr_status
& cd
->eesr_err_check
, EESR
);
1603 sh_eth_error(ndev
, intr_status
);
1607 spin_unlock(&mdp
->lock
);
1612 static int sh_eth_poll(struct napi_struct
*napi
, int budget
)
1614 struct sh_eth_private
*mdp
= container_of(napi
, struct sh_eth_private
,
1616 struct net_device
*ndev
= napi
->dev
;
1618 unsigned long intr_status
;
1621 intr_status
= sh_eth_read(ndev
, EESR
);
1622 if (!(intr_status
& EESR_RX_CHECK
))
1624 /* Clear Rx interrupts */
1625 sh_eth_write(ndev
, intr_status
& EESR_RX_CHECK
, EESR
);
1627 if (sh_eth_rx(ndev
, intr_status
, "a
))
1631 napi_complete(napi
);
1633 /* Reenable Rx interrupts */
1634 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1636 return budget
- quota
;
1639 /* PHY state control function */
1640 static void sh_eth_adjust_link(struct net_device
*ndev
)
1642 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1643 struct phy_device
*phydev
= mdp
->phydev
;
1647 if (phydev
->duplex
!= mdp
->duplex
) {
1649 mdp
->duplex
= phydev
->duplex
;
1650 if (mdp
->cd
->set_duplex
)
1651 mdp
->cd
->set_duplex(ndev
);
1654 if (phydev
->speed
!= mdp
->speed
) {
1656 mdp
->speed
= phydev
->speed
;
1657 if (mdp
->cd
->set_rate
)
1658 mdp
->cd
->set_rate(ndev
);
1662 (sh_eth_read(ndev
, ECMR
) & ~ECMR_TXF
), ECMR
);
1664 mdp
->link
= phydev
->link
;
1665 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1666 sh_eth_rcv_snd_enable(ndev
);
1668 } else if (mdp
->link
) {
1673 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1674 sh_eth_rcv_snd_disable(ndev
);
1677 if (new_state
&& netif_msg_link(mdp
))
1678 phy_print_status(phydev
);
1681 /* PHY init function */
1682 static int sh_eth_phy_init(struct net_device
*ndev
)
1684 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1685 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1686 struct phy_device
*phydev
= NULL
;
1688 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
1689 mdp
->mii_bus
->id
, mdp
->phy_id
);
1695 /* Try connect to PHY */
1696 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
1697 mdp
->phy_interface
);
1698 if (IS_ERR(phydev
)) {
1699 dev_err(&ndev
->dev
, "phy_connect failed\n");
1700 return PTR_ERR(phydev
);
1703 dev_info(&ndev
->dev
, "attached phy %i to driver %s\n",
1704 phydev
->addr
, phydev
->drv
->name
);
1706 mdp
->phydev
= phydev
;
1711 /* PHY control start function */
1712 static int sh_eth_phy_start(struct net_device
*ndev
)
1714 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1717 ret
= sh_eth_phy_init(ndev
);
1721 /* reset phy - this also wakes it from PDOWN */
1722 phy_write(mdp
->phydev
, MII_BMCR
, BMCR_RESET
);
1723 phy_start(mdp
->phydev
);
1728 static int sh_eth_get_settings(struct net_device
*ndev
,
1729 struct ethtool_cmd
*ecmd
)
1731 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1732 unsigned long flags
;
1735 spin_lock_irqsave(&mdp
->lock
, flags
);
1736 ret
= phy_ethtool_gset(mdp
->phydev
, ecmd
);
1737 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1742 static int sh_eth_set_settings(struct net_device
*ndev
,
1743 struct ethtool_cmd
*ecmd
)
1745 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1746 unsigned long flags
;
1749 spin_lock_irqsave(&mdp
->lock
, flags
);
1751 /* disable tx and rx */
1752 sh_eth_rcv_snd_disable(ndev
);
1754 ret
= phy_ethtool_sset(mdp
->phydev
, ecmd
);
1758 if (ecmd
->duplex
== DUPLEX_FULL
)
1763 if (mdp
->cd
->set_duplex
)
1764 mdp
->cd
->set_duplex(ndev
);
1769 /* enable tx and rx */
1770 sh_eth_rcv_snd_enable(ndev
);
1772 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1777 static int sh_eth_nway_reset(struct net_device
*ndev
)
1779 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1780 unsigned long flags
;
1783 spin_lock_irqsave(&mdp
->lock
, flags
);
1784 ret
= phy_start_aneg(mdp
->phydev
);
1785 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1790 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
1792 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1793 return mdp
->msg_enable
;
1796 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
1798 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1799 mdp
->msg_enable
= value
;
1802 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1803 "rx_current", "tx_current",
1804 "rx_dirty", "tx_dirty",
1806 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1808 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
1812 return SH_ETH_STATS_LEN
;
1818 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
1819 struct ethtool_stats
*stats
, u64
*data
)
1821 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1824 /* device-specific stats */
1825 data
[i
++] = mdp
->cur_rx
;
1826 data
[i
++] = mdp
->cur_tx
;
1827 data
[i
++] = mdp
->dirty_rx
;
1828 data
[i
++] = mdp
->dirty_tx
;
1831 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1833 switch (stringset
) {
1835 memcpy(data
, *sh_eth_gstrings_stats
,
1836 sizeof(sh_eth_gstrings_stats
));
1841 static void sh_eth_get_ringparam(struct net_device
*ndev
,
1842 struct ethtool_ringparam
*ring
)
1844 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1846 ring
->rx_max_pending
= RX_RING_MAX
;
1847 ring
->tx_max_pending
= TX_RING_MAX
;
1848 ring
->rx_pending
= mdp
->num_rx_ring
;
1849 ring
->tx_pending
= mdp
->num_tx_ring
;
1852 static int sh_eth_set_ringparam(struct net_device
*ndev
,
1853 struct ethtool_ringparam
*ring
)
1855 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1858 if (ring
->tx_pending
> TX_RING_MAX
||
1859 ring
->rx_pending
> RX_RING_MAX
||
1860 ring
->tx_pending
< TX_RING_MIN
||
1861 ring
->rx_pending
< RX_RING_MIN
)
1863 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
1866 if (netif_running(ndev
)) {
1867 netif_tx_disable(ndev
);
1868 /* Disable interrupts by clearing the interrupt mask. */
1869 sh_eth_write(ndev
, 0x0000, EESIPR
);
1870 /* Stop the chip's Tx and Rx processes. */
1871 sh_eth_write(ndev
, 0, EDTRR
);
1872 sh_eth_write(ndev
, 0, EDRRR
);
1873 synchronize_irq(ndev
->irq
);
1876 /* Free all the skbuffs in the Rx queue. */
1877 sh_eth_ring_free(ndev
);
1878 /* Free DMA buffer */
1879 sh_eth_free_dma_buffer(mdp
);
1881 /* Set new parameters */
1882 mdp
->num_rx_ring
= ring
->rx_pending
;
1883 mdp
->num_tx_ring
= ring
->tx_pending
;
1885 ret
= sh_eth_ring_init(ndev
);
1887 dev_err(&ndev
->dev
, "%s: sh_eth_ring_init failed.\n", __func__
);
1890 ret
= sh_eth_dev_init(ndev
, false);
1892 dev_err(&ndev
->dev
, "%s: sh_eth_dev_init failed.\n", __func__
);
1896 if (netif_running(ndev
)) {
1897 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1898 /* Setting the Rx mode will start the Rx process. */
1899 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1900 netif_wake_queue(ndev
);
1906 static const struct ethtool_ops sh_eth_ethtool_ops
= {
1907 .get_settings
= sh_eth_get_settings
,
1908 .set_settings
= sh_eth_set_settings
,
1909 .nway_reset
= sh_eth_nway_reset
,
1910 .get_msglevel
= sh_eth_get_msglevel
,
1911 .set_msglevel
= sh_eth_set_msglevel
,
1912 .get_link
= ethtool_op_get_link
,
1913 .get_strings
= sh_eth_get_strings
,
1914 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
1915 .get_sset_count
= sh_eth_get_sset_count
,
1916 .get_ringparam
= sh_eth_get_ringparam
,
1917 .set_ringparam
= sh_eth_set_ringparam
,
1920 /* network device open function */
1921 static int sh_eth_open(struct net_device
*ndev
)
1924 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1926 pm_runtime_get_sync(&mdp
->pdev
->dev
);
1928 napi_enable(&mdp
->napi
);
1930 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
1931 mdp
->cd
->irq_flags
, ndev
->name
, ndev
);
1933 dev_err(&ndev
->dev
, "Can not assign IRQ number\n");
1937 /* Descriptor set */
1938 ret
= sh_eth_ring_init(ndev
);
1943 ret
= sh_eth_dev_init(ndev
, true);
1947 /* PHY control start*/
1948 ret
= sh_eth_phy_start(ndev
);
1955 free_irq(ndev
->irq
, ndev
);
1957 napi_disable(&mdp
->napi
);
1958 pm_runtime_put_sync(&mdp
->pdev
->dev
);
1962 /* Timeout function */
1963 static void sh_eth_tx_timeout(struct net_device
*ndev
)
1965 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1966 struct sh_eth_rxdesc
*rxdesc
;
1969 netif_stop_queue(ndev
);
1971 if (netif_msg_timer(mdp
))
1972 dev_err(&ndev
->dev
, "%s: transmit timed out, status %8.8x,"
1973 " resetting...\n", ndev
->name
, (int)sh_eth_read(ndev
, EESR
));
1975 /* tx_errors count up */
1976 ndev
->stats
.tx_errors
++;
1978 /* Free all the skbuffs in the Rx queue. */
1979 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1980 rxdesc
= &mdp
->rx_ring
[i
];
1982 rxdesc
->addr
= 0xBADF00D0;
1983 if (mdp
->rx_skbuff
[i
])
1984 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1985 mdp
->rx_skbuff
[i
] = NULL
;
1987 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1988 if (mdp
->tx_skbuff
[i
])
1989 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1990 mdp
->tx_skbuff
[i
] = NULL
;
1994 sh_eth_dev_init(ndev
, true);
1997 /* Packet transmit function */
1998 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
2000 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2001 struct sh_eth_txdesc
*txdesc
;
2003 unsigned long flags
;
2005 spin_lock_irqsave(&mdp
->lock
, flags
);
2006 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (mdp
->num_tx_ring
- 4)) {
2007 if (!sh_eth_txfree(ndev
)) {
2008 if (netif_msg_tx_queued(mdp
))
2009 dev_warn(&ndev
->dev
, "TxFD exhausted.\n");
2010 netif_stop_queue(ndev
);
2011 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2012 return NETDEV_TX_BUSY
;
2015 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2017 entry
= mdp
->cur_tx
% mdp
->num_tx_ring
;
2018 mdp
->tx_skbuff
[entry
] = skb
;
2019 txdesc
= &mdp
->tx_ring
[entry
];
2021 if (!mdp
->cd
->hw_swap
)
2022 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc
->addr
, 4)),
2024 txdesc
->addr
= dma_map_single(&ndev
->dev
, skb
->data
, skb
->len
,
2026 if (skb
->len
< ETHERSMALL
)
2027 txdesc
->buffer_length
= ETHERSMALL
;
2029 txdesc
->buffer_length
= skb
->len
;
2031 if (entry
>= mdp
->num_tx_ring
- 1)
2032 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
| TD_TDLE
);
2034 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
);
2038 if (!(sh_eth_read(ndev
, EDTRR
) & sh_eth_get_edtrr_trns(mdp
)))
2039 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
2041 return NETDEV_TX_OK
;
2044 /* device close function */
2045 static int sh_eth_close(struct net_device
*ndev
)
2047 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2049 netif_stop_queue(ndev
);
2051 /* Disable interrupts by clearing the interrupt mask. */
2052 sh_eth_write(ndev
, 0x0000, EESIPR
);
2054 /* Stop the chip's Tx and Rx processes. */
2055 sh_eth_write(ndev
, 0, EDTRR
);
2056 sh_eth_write(ndev
, 0, EDRRR
);
2058 /* PHY Disconnect */
2060 phy_stop(mdp
->phydev
);
2061 phy_disconnect(mdp
->phydev
);
2064 free_irq(ndev
->irq
, ndev
);
2066 napi_disable(&mdp
->napi
);
2068 /* Free all the skbuffs in the Rx queue. */
2069 sh_eth_ring_free(ndev
);
2071 /* free DMA buffer */
2072 sh_eth_free_dma_buffer(mdp
);
2074 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2079 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
2081 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2083 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2085 ndev
->stats
.tx_dropped
+= sh_eth_read(ndev
, TROCR
);
2086 sh_eth_write(ndev
, 0, TROCR
); /* (write clear) */
2087 ndev
->stats
.collisions
+= sh_eth_read(ndev
, CDCR
);
2088 sh_eth_write(ndev
, 0, CDCR
); /* (write clear) */
2089 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, LCCR
);
2090 sh_eth_write(ndev
, 0, LCCR
); /* (write clear) */
2091 if (sh_eth_is_gether(mdp
)) {
2092 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CERCR
);
2093 sh_eth_write(ndev
, 0, CERCR
); /* (write clear) */
2094 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CEECR
);
2095 sh_eth_write(ndev
, 0, CEECR
); /* (write clear) */
2097 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CNDCR
);
2098 sh_eth_write(ndev
, 0, CNDCR
); /* (write clear) */
2100 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2102 return &ndev
->stats
;
2105 /* ioctl to device function */
2106 static int sh_eth_do_ioctl(struct net_device
*ndev
, struct ifreq
*rq
,
2109 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2110 struct phy_device
*phydev
= mdp
->phydev
;
2112 if (!netif_running(ndev
))
2118 return phy_mii_ioctl(phydev
, rq
, cmd
);
2121 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2122 static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private
*mdp
,
2125 return sh_eth_tsu_get_offset(mdp
, TSU_POST1
) + (entry
/ 8 * 4);
2128 static u32
sh_eth_tsu_get_post_mask(int entry
)
2130 return 0x0f << (28 - ((entry
% 8) * 4));
2133 static u32
sh_eth_tsu_get_post_bit(struct sh_eth_private
*mdp
, int entry
)
2135 return (0x08 >> (mdp
->port
<< 1)) << (28 - ((entry
% 8) * 4));
2138 static void sh_eth_tsu_enable_cam_entry_post(struct net_device
*ndev
,
2141 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2145 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2146 tmp
= ioread32(reg_offset
);
2147 iowrite32(tmp
| sh_eth_tsu_get_post_bit(mdp
, entry
), reg_offset
);
2150 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device
*ndev
,
2153 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2154 u32 post_mask
, ref_mask
, tmp
;
2157 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2158 post_mask
= sh_eth_tsu_get_post_mask(entry
);
2159 ref_mask
= sh_eth_tsu_get_post_bit(mdp
, entry
) & ~post_mask
;
2161 tmp
= ioread32(reg_offset
);
2162 iowrite32(tmp
& ~post_mask
, reg_offset
);
2164 /* If other port enables, the function returns "true" */
2165 return tmp
& ref_mask
;
2168 static int sh_eth_tsu_busy(struct net_device
*ndev
)
2170 int timeout
= SH_ETH_TSU_TIMEOUT_MS
* 100;
2171 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2173 while ((sh_eth_tsu_read(mdp
, TSU_ADSBSY
) & TSU_ADSBSY_0
)) {
2177 dev_err(&ndev
->dev
, "%s: timeout\n", __func__
);
2185 static int sh_eth_tsu_write_entry(struct net_device
*ndev
, void *reg
,
2190 val
= addr
[0] << 24 | addr
[1] << 16 | addr
[2] << 8 | addr
[3];
2191 iowrite32(val
, reg
);
2192 if (sh_eth_tsu_busy(ndev
) < 0)
2195 val
= addr
[4] << 8 | addr
[5];
2196 iowrite32(val
, reg
+ 4);
2197 if (sh_eth_tsu_busy(ndev
) < 0)
2203 static void sh_eth_tsu_read_entry(void *reg
, u8
*addr
)
2207 val
= ioread32(reg
);
2208 addr
[0] = (val
>> 24) & 0xff;
2209 addr
[1] = (val
>> 16) & 0xff;
2210 addr
[2] = (val
>> 8) & 0xff;
2211 addr
[3] = val
& 0xff;
2212 val
= ioread32(reg
+ 4);
2213 addr
[4] = (val
>> 8) & 0xff;
2214 addr
[5] = val
& 0xff;
2218 static int sh_eth_tsu_find_entry(struct net_device
*ndev
, const u8
*addr
)
2220 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2221 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2223 u8 c_addr
[ETH_ALEN
];
2225 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2226 sh_eth_tsu_read_entry(reg_offset
, c_addr
);
2227 if (memcmp(addr
, c_addr
, ETH_ALEN
) == 0)
2234 static int sh_eth_tsu_find_empty(struct net_device
*ndev
)
2239 memset(blank
, 0, sizeof(blank
));
2240 entry
= sh_eth_tsu_find_entry(ndev
, blank
);
2241 return (entry
< 0) ? -ENOMEM
: entry
;
2244 static int sh_eth_tsu_disable_cam_entry_table(struct net_device
*ndev
,
2247 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2248 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2252 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) &
2253 ~(1 << (31 - entry
)), TSU_TEN
);
2255 memset(blank
, 0, sizeof(blank
));
2256 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ entry
* 8, blank
);
2262 static int sh_eth_tsu_add_entry(struct net_device
*ndev
, const u8
*addr
)
2264 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2265 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2271 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2273 /* No entry found, create one */
2274 i
= sh_eth_tsu_find_empty(ndev
);
2277 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ i
* 8, addr
);
2281 /* Enable the entry */
2282 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) |
2283 (1 << (31 - i
)), TSU_TEN
);
2286 /* Entry found or created, enable POST */
2287 sh_eth_tsu_enable_cam_entry_post(ndev
, i
);
2292 static int sh_eth_tsu_del_entry(struct net_device
*ndev
, const u8
*addr
)
2294 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2300 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2303 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2306 /* Disable the entry if both ports was disabled */
2307 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2315 static int sh_eth_tsu_purge_all(struct net_device
*ndev
)
2317 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2320 if (unlikely(!mdp
->cd
->tsu
))
2323 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++) {
2324 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2327 /* Disable the entry if both ports was disabled */
2328 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2336 static void sh_eth_tsu_purge_mcast(struct net_device
*ndev
)
2338 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2340 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2343 if (unlikely(!mdp
->cd
->tsu
))
2346 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2347 sh_eth_tsu_read_entry(reg_offset
, addr
);
2348 if (is_multicast_ether_addr(addr
))
2349 sh_eth_tsu_del_entry(ndev
, addr
);
2353 /* Multicast reception directions set */
2354 static void sh_eth_set_multicast_list(struct net_device
*ndev
)
2356 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2359 unsigned long flags
;
2361 spin_lock_irqsave(&mdp
->lock
, flags
);
2363 * Initial condition is MCT = 1, PRM = 0.
2364 * Depending on ndev->flags, set PRM or clear MCT
2366 ecmr_bits
= (sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
) | ECMR_MCT
;
2368 if (!(ndev
->flags
& IFF_MULTICAST
)) {
2369 sh_eth_tsu_purge_mcast(ndev
);
2372 if (ndev
->flags
& IFF_ALLMULTI
) {
2373 sh_eth_tsu_purge_mcast(ndev
);
2374 ecmr_bits
&= ~ECMR_MCT
;
2378 if (ndev
->flags
& IFF_PROMISC
) {
2379 sh_eth_tsu_purge_all(ndev
);
2380 ecmr_bits
= (ecmr_bits
& ~ECMR_MCT
) | ECMR_PRM
;
2381 } else if (mdp
->cd
->tsu
) {
2382 struct netdev_hw_addr
*ha
;
2383 netdev_for_each_mc_addr(ha
, ndev
) {
2384 if (mcast_all
&& is_multicast_ether_addr(ha
->addr
))
2387 if (sh_eth_tsu_add_entry(ndev
, ha
->addr
) < 0) {
2389 sh_eth_tsu_purge_mcast(ndev
);
2390 ecmr_bits
&= ~ECMR_MCT
;
2396 /* Normal, unicast/broadcast-only mode. */
2397 ecmr_bits
= (ecmr_bits
& ~ECMR_PRM
) | ECMR_MCT
;
2400 /* update the ethernet mode */
2401 sh_eth_write(ndev
, ecmr_bits
, ECMR
);
2403 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2406 static int sh_eth_get_vtag_index(struct sh_eth_private
*mdp
)
2414 static int sh_eth_vlan_rx_add_vid(struct net_device
*ndev
,
2415 __be16 proto
, u16 vid
)
2417 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2418 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2420 if (unlikely(!mdp
->cd
->tsu
))
2423 /* No filtering if vid = 0 */
2427 mdp
->vlan_num_ids
++;
2430 * The controller has one VLAN tag HW filter. So, if the filter is
2431 * already enabled, the driver disables it and the filte
2433 if (mdp
->vlan_num_ids
> 1) {
2434 /* disable VLAN filter */
2435 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2439 sh_eth_tsu_write(mdp
, TSU_VTAG_ENABLE
| (vid
& TSU_VTAG_VID_MASK
),
2445 static int sh_eth_vlan_rx_kill_vid(struct net_device
*ndev
,
2446 __be16 proto
, u16 vid
)
2448 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2449 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2451 if (unlikely(!mdp
->cd
->tsu
))
2454 /* No filtering if vid = 0 */
2458 mdp
->vlan_num_ids
--;
2459 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2464 /* SuperH's TSU register init function */
2465 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
2467 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
2468 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
2469 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
2470 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
2471 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
2472 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
2473 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
2474 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
2475 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
2476 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
2477 if (sh_eth_is_gether(mdp
)) {
2478 sh_eth_tsu_write(mdp
, 0, TSU_QTAG0
); /* Disable QTAG(0->1) */
2479 sh_eth_tsu_write(mdp
, 0, TSU_QTAG1
); /* Disable QTAG(1->0) */
2481 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
2482 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
2484 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
2485 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
2486 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2487 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
2488 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
2489 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
2490 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
2493 /* MDIO bus release function */
2494 static int sh_mdio_release(struct net_device
*ndev
)
2496 struct mii_bus
*bus
= dev_get_drvdata(&ndev
->dev
);
2498 /* unregister mdio bus */
2499 mdiobus_unregister(bus
);
2501 /* remove mdio bus info from net_device */
2502 dev_set_drvdata(&ndev
->dev
, NULL
);
2504 /* free bitbang info */
2505 free_mdio_bitbang(bus
);
2510 /* MDIO bus init function */
2511 static int sh_mdio_init(struct net_device
*ndev
, int id
,
2512 struct sh_eth_plat_data
*pd
)
2515 struct bb_info
*bitbang
;
2516 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2518 /* create bit control struct for PHY */
2519 bitbang
= devm_kzalloc(&ndev
->dev
, sizeof(struct bb_info
),
2527 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
2528 bitbang
->set_gate
= pd
->set_mdio_gate
;
2529 bitbang
->mdi_msk
= PIR_MDI
;
2530 bitbang
->mdo_msk
= PIR_MDO
;
2531 bitbang
->mmd_msk
= PIR_MMD
;
2532 bitbang
->mdc_msk
= PIR_MDC
;
2533 bitbang
->ctrl
.ops
= &bb_ops
;
2535 /* MII controller setting */
2536 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
2537 if (!mdp
->mii_bus
) {
2542 /* Hook up MII support for ethtool */
2543 mdp
->mii_bus
->name
= "sh_mii";
2544 mdp
->mii_bus
->parent
= &ndev
->dev
;
2545 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
2546 mdp
->pdev
->name
, id
);
2549 mdp
->mii_bus
->irq
= devm_kzalloc(&ndev
->dev
,
2550 sizeof(int) * PHY_MAX_ADDR
,
2552 if (!mdp
->mii_bus
->irq
) {
2557 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
2558 mdp
->mii_bus
->irq
[i
] = PHY_POLL
;
2560 /* register mdio bus */
2561 ret
= mdiobus_register(mdp
->mii_bus
);
2565 dev_set_drvdata(&ndev
->dev
, mdp
->mii_bus
);
2570 free_mdio_bitbang(mdp
->mii_bus
);
2576 static const u16
*sh_eth_get_register_offset(int register_type
)
2578 const u16
*reg_offset
= NULL
;
2580 switch (register_type
) {
2581 case SH_ETH_REG_GIGABIT
:
2582 reg_offset
= sh_eth_offset_gigabit
;
2584 case SH_ETH_REG_FAST_RCAR
:
2585 reg_offset
= sh_eth_offset_fast_rcar
;
2587 case SH_ETH_REG_FAST_SH4
:
2588 reg_offset
= sh_eth_offset_fast_sh4
;
2590 case SH_ETH_REG_FAST_SH3_SH2
:
2591 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
2594 pr_err("Unknown register type (%d)\n", register_type
);
2601 static const struct net_device_ops sh_eth_netdev_ops
= {
2602 .ndo_open
= sh_eth_open
,
2603 .ndo_stop
= sh_eth_close
,
2604 .ndo_start_xmit
= sh_eth_start_xmit
,
2605 .ndo_get_stats
= sh_eth_get_stats
,
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 const struct net_device_ops sh_eth_netdev_ops_tsu
= {
2614 .ndo_open
= sh_eth_open
,
2615 .ndo_stop
= sh_eth_close
,
2616 .ndo_start_xmit
= sh_eth_start_xmit
,
2617 .ndo_get_stats
= sh_eth_get_stats
,
2618 .ndo_set_rx_mode
= sh_eth_set_multicast_list
,
2619 .ndo_vlan_rx_add_vid
= sh_eth_vlan_rx_add_vid
,
2620 .ndo_vlan_rx_kill_vid
= sh_eth_vlan_rx_kill_vid
,
2621 .ndo_tx_timeout
= sh_eth_tx_timeout
,
2622 .ndo_do_ioctl
= sh_eth_do_ioctl
,
2623 .ndo_validate_addr
= eth_validate_addr
,
2624 .ndo_set_mac_address
= eth_mac_addr
,
2625 .ndo_change_mtu
= eth_change_mtu
,
2628 static int sh_eth_drv_probe(struct platform_device
*pdev
)
2631 struct resource
*res
;
2632 struct net_device
*ndev
= NULL
;
2633 struct sh_eth_private
*mdp
= NULL
;
2634 struct sh_eth_plat_data
*pd
= dev_get_platdata(&pdev
->dev
);
2635 const struct platform_device_id
*id
= platform_get_device_id(pdev
);
2638 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2639 if (unlikely(res
== NULL
)) {
2640 dev_err(&pdev
->dev
, "invalid resource\n");
2645 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
2651 /* The sh Ether-specific entries in the device structure. */
2652 ndev
->base_addr
= res
->start
;
2658 ret
= platform_get_irq(pdev
, 0);
2665 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2667 mdp
= netdev_priv(ndev
);
2668 mdp
->num_tx_ring
= TX_RING_SIZE
;
2669 mdp
->num_rx_ring
= RX_RING_SIZE
;
2670 mdp
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
2671 if (IS_ERR(mdp
->addr
)) {
2672 ret
= PTR_ERR(mdp
->addr
);
2676 spin_lock_init(&mdp
->lock
);
2678 pm_runtime_enable(&pdev
->dev
);
2679 pm_runtime_resume(&pdev
->dev
);
2682 mdp
->phy_id
= pd
->phy
;
2683 mdp
->phy_interface
= pd
->phy_interface
;
2685 mdp
->edmac_endian
= pd
->edmac_endian
;
2686 mdp
->no_ether_link
= pd
->no_ether_link
;
2687 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
2690 mdp
->cd
= (struct sh_eth_cpu_data
*)id
->driver_data
;
2691 mdp
->reg_offset
= sh_eth_get_register_offset(mdp
->cd
->register_type
);
2692 sh_eth_set_default_cpu_data(mdp
->cd
);
2696 ndev
->netdev_ops
= &sh_eth_netdev_ops_tsu
;
2698 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
2699 SET_ETHTOOL_OPS(ndev
, &sh_eth_ethtool_ops
);
2700 ndev
->watchdog_timeo
= TX_TIMEOUT
;
2702 /* debug message level */
2703 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
2705 /* read and set MAC address */
2706 read_mac_address(ndev
, pd
->mac_addr
);
2707 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
2708 dev_warn(&pdev
->dev
,
2709 "no valid MAC address supplied, using a random one.\n");
2710 eth_hw_addr_random(ndev
);
2713 /* ioremap the TSU registers */
2715 struct resource
*rtsu
;
2716 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2717 mdp
->tsu_addr
= devm_ioremap_resource(&pdev
->dev
, rtsu
);
2718 if (IS_ERR(mdp
->tsu_addr
)) {
2719 ret
= PTR_ERR(mdp
->tsu_addr
);
2722 mdp
->port
= devno
% 2;
2723 ndev
->features
= NETIF_F_HW_VLAN_CTAG_FILTER
;
2726 /* initialize first or needed device */
2727 if (!devno
|| pd
->needs_init
) {
2728 if (mdp
->cd
->chip_reset
)
2729 mdp
->cd
->chip_reset(ndev
);
2732 /* TSU init (Init only)*/
2733 sh_eth_tsu_init(mdp
);
2737 netif_napi_add(ndev
, &mdp
->napi
, sh_eth_poll
, 64);
2739 /* network device register */
2740 ret
= register_netdev(ndev
);
2745 ret
= sh_mdio_init(ndev
, pdev
->id
, pd
);
2747 goto out_unregister
;
2749 /* print device information */
2750 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2751 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
2753 platform_set_drvdata(pdev
, ndev
);
2758 unregister_netdev(ndev
);
2761 netif_napi_del(&mdp
->napi
);
2772 static int sh_eth_drv_remove(struct platform_device
*pdev
)
2774 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2775 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2777 sh_mdio_release(ndev
);
2778 unregister_netdev(ndev
);
2779 netif_napi_del(&mdp
->napi
);
2780 pm_runtime_disable(&pdev
->dev
);
2787 static int sh_eth_runtime_nop(struct device
*dev
)
2790 * Runtime PM callback shared between ->runtime_suspend()
2791 * and ->runtime_resume(). Simply returns success.
2793 * This driver re-initializes all registers after
2794 * pm_runtime_get_sync() anyway so there is no need
2795 * to save and restore registers here.
2800 static const struct dev_pm_ops sh_eth_dev_pm_ops
= {
2801 .runtime_suspend
= sh_eth_runtime_nop
,
2802 .runtime_resume
= sh_eth_runtime_nop
,
2804 #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
2806 #define SH_ETH_PM_OPS NULL
2809 static struct platform_device_id sh_eth_id_table
[] = {
2810 { "sh7619-ether", (kernel_ulong_t
)&sh7619_data
},
2811 { "sh771x-ether", (kernel_ulong_t
)&sh771x_data
},
2812 { "sh7724-ether", (kernel_ulong_t
)&sh7724_data
},
2813 { "sh7734-gether", (kernel_ulong_t
)&sh7734_data
},
2814 { "sh7757-ether", (kernel_ulong_t
)&sh7757_data
},
2815 { "sh7757-gether", (kernel_ulong_t
)&sh7757_data_giga
},
2816 { "sh7763-gether", (kernel_ulong_t
)&sh7763_data
},
2817 { "r8a7740-gether", (kernel_ulong_t
)&r8a7740_data
},
2818 { "r8a777x-ether", (kernel_ulong_t
)&r8a777x_data
},
2819 { "r8a7790-ether", (kernel_ulong_t
)&r8a7790_data
},
2822 MODULE_DEVICE_TABLE(platform
, sh_eth_id_table
);
2824 static struct platform_driver sh_eth_driver
= {
2825 .probe
= sh_eth_drv_probe
,
2826 .remove
= sh_eth_drv_remove
,
2827 .id_table
= sh_eth_id_table
,
2830 .pm
= SH_ETH_PM_OPS
,
2834 module_platform_driver(sh_eth_driver
);
2836 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2837 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2838 MODULE_LICENSE("GPL v2");