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
] = {
196 static const u16 sh_eth_offset_fast_sh4
[SH_ETH_MAX_REGISTER_OFFSET
] = {
248 static const u16 sh_eth_offset_fast_sh3_sh2
[SH_ETH_MAX_REGISTER_OFFSET
] = {
274 [TSU_CTRST
] = 0x0004,
275 [TSU_FWEN0
] = 0x0010,
276 [TSU_FWEN1
] = 0x0014,
278 [TSU_BSYSL0
] = 0x0020,
279 [TSU_BSYSL1
] = 0x0024,
280 [TSU_PRISL0
] = 0x0028,
281 [TSU_PRISL1
] = 0x002c,
282 [TSU_FWSL0
] = 0x0030,
283 [TSU_FWSL1
] = 0x0034,
284 [TSU_FWSLC
] = 0x0038,
285 [TSU_QTAGM0
] = 0x0040,
286 [TSU_QTAGM1
] = 0x0044,
287 [TSU_ADQT0
] = 0x0048,
288 [TSU_ADQT1
] = 0x004c,
290 [TSU_FWINMK
] = 0x0054,
291 [TSU_ADSBSY
] = 0x0060,
293 [TSU_POST1
] = 0x0070,
294 [TSU_POST2
] = 0x0074,
295 [TSU_POST3
] = 0x0078,
296 [TSU_POST4
] = 0x007c,
311 [TSU_ADRH0
] = 0x0100,
312 [TSU_ADRL0
] = 0x0104,
313 [TSU_ADRL31
] = 0x01fc,
316 #if defined(CONFIG_CPU_SUBTYPE_SH7734) || \
317 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
318 defined(CONFIG_ARCH_R8A7740)
319 static void sh_eth_select_mii(struct net_device
*ndev
)
322 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
324 switch (mdp
->phy_interface
) {
325 case PHY_INTERFACE_MODE_GMII
:
328 case PHY_INTERFACE_MODE_MII
:
331 case PHY_INTERFACE_MODE_RMII
:
335 pr_warn("PHY interface mode was not setup. Set to MII.\n");
340 sh_eth_write(ndev
, value
, RMII_MII
);
344 static void __maybe_unused
sh_eth_set_duplex(struct net_device
*ndev
)
346 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
348 if (mdp
->duplex
) /* Full */
349 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
351 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
354 /* There is CPU dependent code */
355 #if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779)
356 #define SH_ETH_RESET_DEFAULT 1
357 static void sh_eth_set_rate(struct net_device
*ndev
)
359 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
361 switch (mdp
->speed
) {
362 case 10: /* 10BASE */
363 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_ELB
, ECMR
);
365 case 100:/* 100BASE */
366 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_ELB
, ECMR
);
374 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
375 .set_duplex
= sh_eth_set_duplex
,
376 .set_rate
= sh_eth_set_rate
,
378 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
379 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
380 .eesipr_value
= 0x01ff009f,
382 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
383 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RDE
|
384 EESR_RFRMER
| EESR_TFE
| EESR_TDE
| EESR_ECI
,
385 .tx_error_check
= EESR_TWB
| EESR_TABT
| EESR_TDE
| EESR_TFE
,
392 #elif defined(CONFIG_CPU_SUBTYPE_SH7724)
393 #define SH_ETH_RESET_DEFAULT 1
395 static void sh_eth_set_rate(struct net_device
*ndev
)
397 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
399 switch (mdp
->speed
) {
400 case 10: /* 10BASE */
401 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_RTM
, ECMR
);
403 case 100:/* 100BASE */
404 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_RTM
, ECMR
);
412 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
413 .set_duplex
= sh_eth_set_duplex
,
414 .set_rate
= sh_eth_set_rate
,
416 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
417 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
418 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x01ff009f,
420 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
421 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RDE
|
422 EESR_RFRMER
| EESR_TFE
| EESR_TDE
| EESR_ECI
,
423 .tx_error_check
= EESR_TWB
| EESR_TABT
| EESR_TDE
| EESR_TFE
,
430 .rpadir_value
= 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
432 #elif defined(CONFIG_CPU_SUBTYPE_SH7757)
433 #define SH_ETH_HAS_BOTH_MODULES 1
434 static int sh_eth_check_reset(struct net_device
*ndev
);
436 static void sh_eth_set_rate(struct net_device
*ndev
)
438 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
440 switch (mdp
->speed
) {
441 case 10: /* 10BASE */
442 sh_eth_write(ndev
, 0, RTRATE
);
444 case 100:/* 100BASE */
445 sh_eth_write(ndev
, 1, RTRATE
);
453 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
454 .set_duplex
= sh_eth_set_duplex
,
455 .set_rate
= sh_eth_set_rate
,
457 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
458 .rmcr_value
= 0x00000001,
460 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
461 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RDE
|
462 EESR_RFRMER
| EESR_TFE
| EESR_TDE
| EESR_ECI
,
463 .tx_error_check
= EESR_TWB
| EESR_TABT
| EESR_TDE
| EESR_TFE
,
465 .irq_flags
= IRQF_SHARED
,
472 .rpadir_value
= 2 << 16,
475 #define SH_GIGA_ETH_BASE 0xfee00000
476 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
477 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
478 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
481 unsigned long mahr
[2], malr
[2];
483 /* save MAHR and MALR */
484 for (i
= 0; i
< 2; i
++) {
485 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
486 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
490 iowrite32(ARSTR_ARSTR
, (void *)(SH_GIGA_ETH_BASE
+ 0x1800));
493 /* restore MAHR and MALR */
494 for (i
= 0; i
< 2; i
++) {
495 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
496 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
500 static int sh_eth_is_gether(struct sh_eth_private
*mdp
);
501 static int sh_eth_reset(struct net_device
*ndev
)
503 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
506 if (sh_eth_is_gether(mdp
)) {
507 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
508 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
,
511 ret
= sh_eth_check_reset(ndev
);
516 sh_eth_write(ndev
, 0x0, TDLAR
);
517 sh_eth_write(ndev
, 0x0, TDFAR
);
518 sh_eth_write(ndev
, 0x0, TDFXR
);
519 sh_eth_write(ndev
, 0x0, TDFFR
);
520 sh_eth_write(ndev
, 0x0, RDLAR
);
521 sh_eth_write(ndev
, 0x0, RDFAR
);
522 sh_eth_write(ndev
, 0x0, RDFXR
);
523 sh_eth_write(ndev
, 0x0, RDFFR
);
525 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_ETHER
,
528 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) & ~EDMR_SRST_ETHER
,
536 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
538 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
540 switch (mdp
->speed
) {
541 case 10: /* 10BASE */
542 sh_eth_write(ndev
, 0x00000000, GECMR
);
544 case 100:/* 100BASE */
545 sh_eth_write(ndev
, 0x00000010, GECMR
);
547 case 1000: /* 1000BASE */
548 sh_eth_write(ndev
, 0x00000020, GECMR
);
555 /* SH7757(GETHERC) */
556 static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga
= {
557 .chip_reset
= sh_eth_chip_reset_giga
,
558 .set_duplex
= sh_eth_set_duplex
,
559 .set_rate
= sh_eth_set_rate_giga
,
561 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
562 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
563 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
565 .tx_check
= EESR_TC1
| EESR_FTC
,
566 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
| \
567 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
| \
569 .tx_error_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_TDE
| \
571 .fdr_value
= 0x0000072f,
572 .rmcr_value
= 0x00000001,
574 .irq_flags
= IRQF_SHARED
,
581 .rpadir_value
= 2 << 16,
587 static struct sh_eth_cpu_data
*sh_eth_get_cpu_data(struct sh_eth_private
*mdp
)
589 if (sh_eth_is_gether(mdp
))
590 return &sh_eth_my_cpu_data_giga
;
592 return &sh_eth_my_cpu_data
;
595 #elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
596 static int sh_eth_check_reset(struct net_device
*ndev
);
597 static void sh_eth_reset_hw_crc(struct net_device
*ndev
);
599 static void sh_eth_chip_reset(struct net_device
*ndev
)
601 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
604 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
608 static void sh_eth_set_rate(struct net_device
*ndev
)
610 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
612 switch (mdp
->speed
) {
613 case 10: /* 10BASE */
614 sh_eth_write(ndev
, GECMR_10
, GECMR
);
616 case 100:/* 100BASE */
617 sh_eth_write(ndev
, GECMR_100
, GECMR
);
619 case 1000: /* 1000BASE */
620 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
628 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
629 .chip_reset
= sh_eth_chip_reset
,
630 .set_duplex
= sh_eth_set_duplex
,
631 .set_rate
= sh_eth_set_rate
,
633 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
634 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
635 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
637 .tx_check
= EESR_TC1
| EESR_FTC
,
638 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
| \
639 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
| \
641 .tx_error_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_TDE
| \
652 #if defined(CONFIG_CPU_SUBTYPE_SH7734)
656 .irq_flags
= IRQF_SHARED
,
660 static int sh_eth_reset(struct net_device
*ndev
)
664 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
665 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
, EDMR
);
667 ret
= sh_eth_check_reset(ndev
);
672 sh_eth_write(ndev
, 0x0, TDLAR
);
673 sh_eth_write(ndev
, 0x0, TDFAR
);
674 sh_eth_write(ndev
, 0x0, TDFXR
);
675 sh_eth_write(ndev
, 0x0, TDFFR
);
676 sh_eth_write(ndev
, 0x0, RDLAR
);
677 sh_eth_write(ndev
, 0x0, RDFAR
);
678 sh_eth_write(ndev
, 0x0, RDFXR
);
679 sh_eth_write(ndev
, 0x0, RDFFR
);
681 /* Reset HW CRC register */
682 sh_eth_reset_hw_crc(ndev
);
684 /* Select MII mode */
685 if (sh_eth_my_cpu_data
.select_mii
)
686 sh_eth_select_mii(ndev
);
691 static void sh_eth_reset_hw_crc(struct net_device
*ndev
)
693 if (sh_eth_my_cpu_data
.hw_crc
)
694 sh_eth_write(ndev
, 0x0, CSMR
);
697 #elif defined(CONFIG_ARCH_R8A7740)
698 static int sh_eth_check_reset(struct net_device
*ndev
);
700 static void sh_eth_chip_reset(struct net_device
*ndev
)
702 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
705 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
708 sh_eth_select_mii(ndev
);
711 static int sh_eth_reset(struct net_device
*ndev
)
715 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
716 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
, EDMR
);
718 ret
= sh_eth_check_reset(ndev
);
723 sh_eth_write(ndev
, 0x0, TDLAR
);
724 sh_eth_write(ndev
, 0x0, TDFAR
);
725 sh_eth_write(ndev
, 0x0, TDFXR
);
726 sh_eth_write(ndev
, 0x0, TDFFR
);
727 sh_eth_write(ndev
, 0x0, RDLAR
);
728 sh_eth_write(ndev
, 0x0, RDFAR
);
729 sh_eth_write(ndev
, 0x0, RDFXR
);
730 sh_eth_write(ndev
, 0x0, RDFFR
);
736 static void sh_eth_set_rate(struct net_device
*ndev
)
738 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
740 switch (mdp
->speed
) {
741 case 10: /* 10BASE */
742 sh_eth_write(ndev
, GECMR_10
, GECMR
);
744 case 100:/* 100BASE */
745 sh_eth_write(ndev
, GECMR_100
, GECMR
);
747 case 1000: /* 1000BASE */
748 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
756 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
757 .chip_reset
= sh_eth_chip_reset
,
758 .set_duplex
= sh_eth_set_duplex
,
759 .set_rate
= sh_eth_set_rate
,
761 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
762 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
763 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
765 .tx_check
= EESR_TC1
| EESR_FTC
,
766 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
| \
767 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
| \
769 .tx_error_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_TDE
| \
783 #elif defined(CONFIG_CPU_SUBTYPE_SH7619)
784 #define SH_ETH_RESET_DEFAULT 1
785 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
786 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
793 #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
794 #define SH_ETH_RESET_DEFAULT 1
795 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
796 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
801 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
804 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
806 if (!cd
->ecsipr_value
)
807 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
809 if (!cd
->fcftr_value
)
810 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
| \
811 DEFAULT_FIFO_F_D_RFD
;
814 cd
->fdr_value
= DEFAULT_FDR_INIT
;
817 cd
->rmcr_value
= DEFAULT_RMCR_VALUE
;
820 cd
->tx_check
= DEFAULT_TX_CHECK
;
822 if (!cd
->eesr_err_check
)
823 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
825 if (!cd
->tx_error_check
)
826 cd
->tx_error_check
= DEFAULT_TX_ERROR_CHECK
;
829 #if defined(SH_ETH_RESET_DEFAULT)
831 static int sh_eth_reset(struct net_device
*ndev
)
833 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_ETHER
, EDMR
);
835 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) & ~EDMR_SRST_ETHER
, EDMR
);
840 static int sh_eth_check_reset(struct net_device
*ndev
)
846 if (!(sh_eth_read(ndev
, EDMR
) & 0x3))
852 pr_err("Device reset fail\n");
859 #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
860 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
864 reserve
= SH4_SKB_RX_ALIGN
- ((u32
)skb
->data
& (SH4_SKB_RX_ALIGN
- 1));
866 skb_reserve(skb
, reserve
);
869 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
871 skb_reserve(skb
, SH2_SH3_SKB_RX_ALIGN
);
876 /* CPU <-> EDMAC endian convert */
877 static inline __u32
cpu_to_edmac(struct sh_eth_private
*mdp
, u32 x
)
879 switch (mdp
->edmac_endian
) {
880 case EDMAC_LITTLE_ENDIAN
:
881 return cpu_to_le32(x
);
882 case EDMAC_BIG_ENDIAN
:
883 return cpu_to_be32(x
);
888 static inline __u32
edmac_to_cpu(struct sh_eth_private
*mdp
, u32 x
)
890 switch (mdp
->edmac_endian
) {
891 case EDMAC_LITTLE_ENDIAN
:
892 return le32_to_cpu(x
);
893 case EDMAC_BIG_ENDIAN
:
894 return be32_to_cpu(x
);
900 * Program the hardware MAC address from dev->dev_addr.
902 static void update_mac_address(struct net_device
*ndev
)
905 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
906 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
908 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
912 * Get MAC address from SuperH MAC address register
914 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
915 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
916 * When you want use this device, you must set MAC address in bootloader.
919 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
921 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
922 memcpy(ndev
->dev_addr
, mac
, 6);
924 ndev
->dev_addr
[0] = (sh_eth_read(ndev
, MAHR
) >> 24);
925 ndev
->dev_addr
[1] = (sh_eth_read(ndev
, MAHR
) >> 16) & 0xFF;
926 ndev
->dev_addr
[2] = (sh_eth_read(ndev
, MAHR
) >> 8) & 0xFF;
927 ndev
->dev_addr
[3] = (sh_eth_read(ndev
, MAHR
) & 0xFF);
928 ndev
->dev_addr
[4] = (sh_eth_read(ndev
, MALR
) >> 8) & 0xFF;
929 ndev
->dev_addr
[5] = (sh_eth_read(ndev
, MALR
) & 0xFF);
933 static int sh_eth_is_gether(struct sh_eth_private
*mdp
)
935 if (mdp
->reg_offset
== sh_eth_offset_gigabit
)
941 static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private
*mdp
)
943 if (sh_eth_is_gether(mdp
))
944 return EDTRR_TRNS_GETHER
;
946 return EDTRR_TRNS_ETHER
;
950 void (*set_gate
)(void *addr
);
951 struct mdiobb_ctrl ctrl
;
953 u32 mmd_msk
;/* MMD */
960 static void bb_set(void *addr
, u32 msk
)
962 iowrite32(ioread32(addr
) | msk
, addr
);
966 static void bb_clr(void *addr
, u32 msk
)
968 iowrite32((ioread32(addr
) & ~msk
), addr
);
972 static int bb_read(void *addr
, u32 msk
)
974 return (ioread32(addr
) & msk
) != 0;
977 /* Data I/O pin control */
978 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
980 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
982 if (bitbang
->set_gate
)
983 bitbang
->set_gate(bitbang
->addr
);
986 bb_set(bitbang
->addr
, bitbang
->mmd_msk
);
988 bb_clr(bitbang
->addr
, bitbang
->mmd_msk
);
992 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
994 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
996 if (bitbang
->set_gate
)
997 bitbang
->set_gate(bitbang
->addr
);
1000 bb_set(bitbang
->addr
, bitbang
->mdo_msk
);
1002 bb_clr(bitbang
->addr
, bitbang
->mdo_msk
);
1006 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
1008 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1010 if (bitbang
->set_gate
)
1011 bitbang
->set_gate(bitbang
->addr
);
1013 return bb_read(bitbang
->addr
, bitbang
->mdi_msk
);
1016 /* MDC pin control */
1017 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
1019 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
1021 if (bitbang
->set_gate
)
1022 bitbang
->set_gate(bitbang
->addr
);
1025 bb_set(bitbang
->addr
, bitbang
->mdc_msk
);
1027 bb_clr(bitbang
->addr
, bitbang
->mdc_msk
);
1030 /* mdio bus control struct */
1031 static struct mdiobb_ops bb_ops
= {
1032 .owner
= THIS_MODULE
,
1033 .set_mdc
= sh_mdc_ctrl
,
1034 .set_mdio_dir
= sh_mmd_ctrl
,
1035 .set_mdio_data
= sh_set_mdio
,
1036 .get_mdio_data
= sh_get_mdio
,
1039 /* free skb and descriptor buffer */
1040 static void sh_eth_ring_free(struct net_device
*ndev
)
1042 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1045 /* Free Rx skb ringbuffer */
1046 if (mdp
->rx_skbuff
) {
1047 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1048 if (mdp
->rx_skbuff
[i
])
1049 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1052 kfree(mdp
->rx_skbuff
);
1053 mdp
->rx_skbuff
= NULL
;
1055 /* Free Tx skb ringbuffer */
1056 if (mdp
->tx_skbuff
) {
1057 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1058 if (mdp
->tx_skbuff
[i
])
1059 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1062 kfree(mdp
->tx_skbuff
);
1063 mdp
->tx_skbuff
= NULL
;
1066 /* format skb and descriptor buffer */
1067 static void sh_eth_ring_format(struct net_device
*ndev
)
1069 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1071 struct sk_buff
*skb
;
1072 struct sh_eth_rxdesc
*rxdesc
= NULL
;
1073 struct sh_eth_txdesc
*txdesc
= NULL
;
1074 int rx_ringsize
= sizeof(*rxdesc
) * mdp
->num_rx_ring
;
1075 int tx_ringsize
= sizeof(*txdesc
) * mdp
->num_tx_ring
;
1077 mdp
->cur_rx
= mdp
->cur_tx
= 0;
1078 mdp
->dirty_rx
= mdp
->dirty_tx
= 0;
1080 memset(mdp
->rx_ring
, 0, rx_ringsize
);
1082 /* build Rx ring buffer */
1083 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1085 mdp
->rx_skbuff
[i
] = NULL
;
1086 skb
= netdev_alloc_skb(ndev
, mdp
->rx_buf_sz
);
1087 mdp
->rx_skbuff
[i
] = skb
;
1090 dma_map_single(&ndev
->dev
, skb
->data
, mdp
->rx_buf_sz
,
1092 sh_eth_set_receive_align(skb
);
1095 rxdesc
= &mdp
->rx_ring
[i
];
1096 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
1097 rxdesc
->status
= cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1099 /* The size of the buffer is 16 byte boundary. */
1100 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
1101 /* Rx descriptor address set */
1103 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
1104 if (sh_eth_is_gether(mdp
))
1105 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
1109 mdp
->dirty_rx
= (u32
) (i
- mdp
->num_rx_ring
);
1111 /* Mark the last entry as wrapping the ring. */
1112 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RDEL
);
1114 memset(mdp
->tx_ring
, 0, tx_ringsize
);
1116 /* build Tx ring buffer */
1117 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1118 mdp
->tx_skbuff
[i
] = NULL
;
1119 txdesc
= &mdp
->tx_ring
[i
];
1120 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1121 txdesc
->buffer_length
= 0;
1123 /* Tx descriptor address set */
1124 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
1125 if (sh_eth_is_gether(mdp
))
1126 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
1130 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1133 /* Get skb and descriptor buffer */
1134 static int sh_eth_ring_init(struct net_device
*ndev
)
1136 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1137 int rx_ringsize
, tx_ringsize
, ret
= 0;
1140 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
1141 * card needs room to do 8 byte alignment, +2 so we can reserve
1142 * the first 2 bytes, and +16 gets room for the status word from the
1145 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
1146 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
1147 if (mdp
->cd
->rpadir
)
1148 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
1150 /* Allocate RX and TX skb rings */
1151 mdp
->rx_skbuff
= kmalloc_array(mdp
->num_rx_ring
,
1152 sizeof(*mdp
->rx_skbuff
), GFP_KERNEL
);
1153 if (!mdp
->rx_skbuff
) {
1158 mdp
->tx_skbuff
= kmalloc_array(mdp
->num_tx_ring
,
1159 sizeof(*mdp
->tx_skbuff
), GFP_KERNEL
);
1160 if (!mdp
->tx_skbuff
) {
1165 /* Allocate all Rx descriptors. */
1166 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1167 mdp
->rx_ring
= dma_alloc_coherent(NULL
, rx_ringsize
, &mdp
->rx_desc_dma
,
1169 if (!mdp
->rx_ring
) {
1171 goto desc_ring_free
;
1176 /* Allocate all Tx descriptors. */
1177 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1178 mdp
->tx_ring
= dma_alloc_coherent(NULL
, tx_ringsize
, &mdp
->tx_desc_dma
,
1180 if (!mdp
->tx_ring
) {
1182 goto desc_ring_free
;
1187 /* free DMA buffer */
1188 dma_free_coherent(NULL
, rx_ringsize
, mdp
->rx_ring
, mdp
->rx_desc_dma
);
1191 /* Free Rx and Tx skb ring buffer */
1192 sh_eth_ring_free(ndev
);
1193 mdp
->tx_ring
= NULL
;
1194 mdp
->rx_ring
= NULL
;
1199 static void sh_eth_free_dma_buffer(struct sh_eth_private
*mdp
)
1204 ringsize
= sizeof(struct sh_eth_rxdesc
) * mdp
->num_rx_ring
;
1205 dma_free_coherent(NULL
, ringsize
, mdp
->rx_ring
,
1207 mdp
->rx_ring
= NULL
;
1211 ringsize
= sizeof(struct sh_eth_txdesc
) * mdp
->num_tx_ring
;
1212 dma_free_coherent(NULL
, ringsize
, mdp
->tx_ring
,
1214 mdp
->tx_ring
= NULL
;
1218 static int sh_eth_dev_init(struct net_device
*ndev
, bool start
)
1221 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1225 ret
= sh_eth_reset(ndev
);
1229 /* Descriptor format */
1230 sh_eth_ring_format(ndev
);
1231 if (mdp
->cd
->rpadir
)
1232 sh_eth_write(ndev
, mdp
->cd
->rpadir_value
, RPADIR
);
1234 /* all sh_eth int mask */
1235 sh_eth_write(ndev
, 0, EESIPR
);
1237 #if defined(__LITTLE_ENDIAN)
1238 if (mdp
->cd
->hw_swap
)
1239 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
1242 sh_eth_write(ndev
, 0, EDMR
);
1245 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
1246 sh_eth_write(ndev
, 0, TFTR
);
1248 /* Frame recv control */
1249 sh_eth_write(ndev
, mdp
->cd
->rmcr_value
, RMCR
);
1251 sh_eth_write(ndev
, DESC_I_RINT8
| DESC_I_RINT5
| DESC_I_TINT2
, TRSCER
);
1254 sh_eth_write(ndev
, 0x800, BCULR
); /* Burst sycle set */
1256 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
1258 if (!mdp
->cd
->no_trimd
)
1259 sh_eth_write(ndev
, 0, TRIMD
);
1261 /* Recv frame limit set register */
1262 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
1265 sh_eth_write(ndev
, sh_eth_read(ndev
, EESR
), EESR
);
1267 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1269 /* PAUSE Prohibition */
1270 val
= (sh_eth_read(ndev
, ECMR
) & ECMR_DM
) |
1271 ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) | ECMR_TE
| ECMR_RE
;
1273 sh_eth_write(ndev
, val
, ECMR
);
1275 if (mdp
->cd
->set_rate
)
1276 mdp
->cd
->set_rate(ndev
);
1278 /* E-MAC Status Register clear */
1279 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
1281 /* E-MAC Interrupt Enable register */
1283 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
1285 /* Set MAC address */
1286 update_mac_address(ndev
);
1290 sh_eth_write(ndev
, APR_AP
, APR
);
1292 sh_eth_write(ndev
, MPR_MP
, MPR
);
1293 if (mdp
->cd
->tpauser
)
1294 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
1297 /* Setting the Rx mode will start the Rx process. */
1298 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1300 netif_start_queue(ndev
);
1307 /* free Tx skb function */
1308 static int sh_eth_txfree(struct net_device
*ndev
)
1310 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1311 struct sh_eth_txdesc
*txdesc
;
1315 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
1316 entry
= mdp
->dirty_tx
% mdp
->num_tx_ring
;
1317 txdesc
= &mdp
->tx_ring
[entry
];
1318 if (txdesc
->status
& cpu_to_edmac(mdp
, TD_TACT
))
1320 /* Free the original skb. */
1321 if (mdp
->tx_skbuff
[entry
]) {
1322 dma_unmap_single(&ndev
->dev
, txdesc
->addr
,
1323 txdesc
->buffer_length
, DMA_TO_DEVICE
);
1324 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
1325 mdp
->tx_skbuff
[entry
] = NULL
;
1328 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
1329 if (entry
>= mdp
->num_tx_ring
- 1)
1330 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
1332 ndev
->stats
.tx_packets
++;
1333 ndev
->stats
.tx_bytes
+= txdesc
->buffer_length
;
1338 /* Packet receive function */
1339 static int sh_eth_rx(struct net_device
*ndev
, u32 intr_status
)
1341 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1342 struct sh_eth_rxdesc
*rxdesc
;
1344 int entry
= mdp
->cur_rx
% mdp
->num_rx_ring
;
1345 int boguscnt
= (mdp
->dirty_rx
+ mdp
->num_rx_ring
) - mdp
->cur_rx
;
1346 struct sk_buff
*skb
;
1350 rxdesc
= &mdp
->rx_ring
[entry
];
1351 while (!(rxdesc
->status
& cpu_to_edmac(mdp
, RD_RACT
))) {
1352 desc_status
= edmac_to_cpu(mdp
, rxdesc
->status
);
1353 pkt_len
= rxdesc
->frame_length
;
1355 #if defined(CONFIG_ARCH_R8A7740)
1362 if (!(desc_status
& RDFEND
))
1363 ndev
->stats
.rx_length_errors
++;
1365 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
1366 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
1367 ndev
->stats
.rx_errors
++;
1368 if (desc_status
& RD_RFS1
)
1369 ndev
->stats
.rx_crc_errors
++;
1370 if (desc_status
& RD_RFS2
)
1371 ndev
->stats
.rx_frame_errors
++;
1372 if (desc_status
& RD_RFS3
)
1373 ndev
->stats
.rx_length_errors
++;
1374 if (desc_status
& RD_RFS4
)
1375 ndev
->stats
.rx_length_errors
++;
1376 if (desc_status
& RD_RFS6
)
1377 ndev
->stats
.rx_missed_errors
++;
1378 if (desc_status
& RD_RFS10
)
1379 ndev
->stats
.rx_over_errors
++;
1381 if (!mdp
->cd
->hw_swap
)
1383 phys_to_virt(ALIGN(rxdesc
->addr
, 4)),
1385 skb
= mdp
->rx_skbuff
[entry
];
1386 mdp
->rx_skbuff
[entry
] = NULL
;
1387 if (mdp
->cd
->rpadir
)
1388 skb_reserve(skb
, NET_IP_ALIGN
);
1389 skb_put(skb
, pkt_len
);
1390 skb
->protocol
= eth_type_trans(skb
, ndev
);
1392 ndev
->stats
.rx_packets
++;
1393 ndev
->stats
.rx_bytes
+= pkt_len
;
1395 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RACT
);
1396 entry
= (++mdp
->cur_rx
) % mdp
->num_rx_ring
;
1397 rxdesc
= &mdp
->rx_ring
[entry
];
1400 /* Refill the Rx ring buffers. */
1401 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
1402 entry
= mdp
->dirty_rx
% mdp
->num_rx_ring
;
1403 rxdesc
= &mdp
->rx_ring
[entry
];
1404 /* The size of the buffer is 16 byte boundary. */
1405 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
1407 if (mdp
->rx_skbuff
[entry
] == NULL
) {
1408 skb
= netdev_alloc_skb(ndev
, mdp
->rx_buf_sz
);
1409 mdp
->rx_skbuff
[entry
] = skb
;
1411 break; /* Better luck next round. */
1412 dma_map_single(&ndev
->dev
, skb
->data
, mdp
->rx_buf_sz
,
1414 sh_eth_set_receive_align(skb
);
1416 skb_checksum_none_assert(skb
);
1417 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
1419 if (entry
>= mdp
->num_rx_ring
- 1)
1421 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
| RD_RDEL
);
1424 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
1427 /* Restart Rx engine if stopped. */
1428 /* If we don't need to check status, don't. -KDU */
1429 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
)) {
1430 /* fix the values for the next receiving if RDE is set */
1431 if (intr_status
& EESR_RDE
)
1432 mdp
->cur_rx
= mdp
->dirty_rx
=
1433 (sh_eth_read(ndev
, RDFAR
) -
1434 sh_eth_read(ndev
, RDLAR
)) >> 4;
1435 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1441 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
1443 /* disable tx and rx */
1444 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) &
1445 ~(ECMR_RE
| ECMR_TE
), ECMR
);
1448 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
1450 /* enable tx and rx */
1451 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) |
1452 (ECMR_RE
| ECMR_TE
), ECMR
);
1455 /* error control function */
1456 static void sh_eth_error(struct net_device
*ndev
, int intr_status
)
1458 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1463 if (intr_status
& EESR_ECI
) {
1464 felic_stat
= sh_eth_read(ndev
, ECSR
);
1465 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1466 if (felic_stat
& ECSR_ICD
)
1467 ndev
->stats
.tx_carrier_errors
++;
1468 if (felic_stat
& ECSR_LCHNG
) {
1470 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
) {
1473 link_stat
= (sh_eth_read(ndev
, PSR
));
1474 if (mdp
->ether_link_active_low
)
1475 link_stat
= ~link_stat
;
1477 if (!(link_stat
& PHY_ST_LINK
))
1478 sh_eth_rcv_snd_disable(ndev
);
1481 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) &
1482 ~DMAC_M_ECI
, EESIPR
);
1484 sh_eth_write(ndev
, sh_eth_read(ndev
, ECSR
),
1486 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) |
1487 DMAC_M_ECI
, EESIPR
);
1488 /* enable tx and rx */
1489 sh_eth_rcv_snd_enable(ndev
);
1495 if (intr_status
& EESR_TWB
) {
1496 /* Write buck end. unused write back interrupt */
1497 if (intr_status
& EESR_TABT
) /* Transmit Abort int */
1498 ndev
->stats
.tx_aborted_errors
++;
1499 if (netif_msg_tx_err(mdp
))
1500 dev_err(&ndev
->dev
, "Transmit Abort\n");
1503 if (intr_status
& EESR_RABT
) {
1504 /* Receive Abort int */
1505 if (intr_status
& EESR_RFRMER
) {
1506 /* Receive Frame Overflow int */
1507 ndev
->stats
.rx_frame_errors
++;
1508 if (netif_msg_rx_err(mdp
))
1509 dev_err(&ndev
->dev
, "Receive Abort\n");
1513 if (intr_status
& EESR_TDE
) {
1514 /* Transmit Descriptor Empty int */
1515 ndev
->stats
.tx_fifo_errors
++;
1516 if (netif_msg_tx_err(mdp
))
1517 dev_err(&ndev
->dev
, "Transmit Descriptor Empty\n");
1520 if (intr_status
& EESR_TFE
) {
1521 /* FIFO under flow */
1522 ndev
->stats
.tx_fifo_errors
++;
1523 if (netif_msg_tx_err(mdp
))
1524 dev_err(&ndev
->dev
, "Transmit FIFO Under flow\n");
1527 if (intr_status
& EESR_RDE
) {
1528 /* Receive Descriptor Empty int */
1529 ndev
->stats
.rx_over_errors
++;
1531 if (netif_msg_rx_err(mdp
))
1532 dev_err(&ndev
->dev
, "Receive Descriptor Empty\n");
1535 if (intr_status
& EESR_RFE
) {
1536 /* Receive FIFO Overflow int */
1537 ndev
->stats
.rx_fifo_errors
++;
1538 if (netif_msg_rx_err(mdp
))
1539 dev_err(&ndev
->dev
, "Receive FIFO Overflow\n");
1542 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1544 ndev
->stats
.tx_fifo_errors
++;
1545 if (netif_msg_tx_err(mdp
))
1546 dev_err(&ndev
->dev
, "Address Error\n");
1549 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1550 if (mdp
->cd
->no_ade
)
1552 if (intr_status
& mask
) {
1554 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1556 dev_err(&ndev
->dev
, "TX error. status=%8.8x cur_tx=%8.8x ",
1557 intr_status
, mdp
->cur_tx
);
1558 dev_err(&ndev
->dev
, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1559 mdp
->dirty_tx
, (u32
) ndev
->state
, edtrr
);
1560 /* dirty buffer free */
1561 sh_eth_txfree(ndev
);
1564 if (edtrr
^ sh_eth_get_edtrr_trns(mdp
)) {
1566 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
1569 netif_wake_queue(ndev
);
1573 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1575 struct net_device
*ndev
= netdev
;
1576 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1577 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1578 irqreturn_t ret
= IRQ_NONE
;
1579 unsigned long intr_status
;
1581 spin_lock(&mdp
->lock
);
1583 /* Get interrupt status */
1584 intr_status
= sh_eth_read(ndev
, EESR
);
1585 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1586 * enabled since it's the one that comes thru regardless of the mask,
1587 * and we need to fully handle it in sh_eth_error() in order to quench
1588 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1590 intr_status
&= sh_eth_read(ndev
, EESIPR
) | DMAC_M_ECI
;
1591 /* Clear interrupt */
1592 if (intr_status
& (EESR_FRC
| EESR_RMAF
| EESR_RRF
|
1593 EESR_RTLF
| EESR_RTSF
| EESR_PRE
| EESR_CERF
|
1594 cd
->tx_check
| cd
->eesr_err_check
)) {
1595 sh_eth_write(ndev
, intr_status
, EESR
);
1600 if (intr_status
& (EESR_FRC
| /* Frame recv*/
1601 EESR_RMAF
| /* Multi cast address recv*/
1602 EESR_RRF
| /* Bit frame recv */
1603 EESR_RTLF
| /* Long frame recv*/
1604 EESR_RTSF
| /* short frame recv */
1605 EESR_PRE
| /* PHY-LSI recv error */
1606 EESR_CERF
)){ /* recv frame CRC error */
1607 sh_eth_rx(ndev
, intr_status
);
1611 if (intr_status
& cd
->tx_check
) {
1612 sh_eth_txfree(ndev
);
1613 netif_wake_queue(ndev
);
1616 if (intr_status
& cd
->eesr_err_check
)
1617 sh_eth_error(ndev
, intr_status
);
1620 spin_unlock(&mdp
->lock
);
1625 /* PHY state control function */
1626 static void sh_eth_adjust_link(struct net_device
*ndev
)
1628 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1629 struct phy_device
*phydev
= mdp
->phydev
;
1633 if (phydev
->duplex
!= mdp
->duplex
) {
1635 mdp
->duplex
= phydev
->duplex
;
1636 if (mdp
->cd
->set_duplex
)
1637 mdp
->cd
->set_duplex(ndev
);
1640 if (phydev
->speed
!= mdp
->speed
) {
1642 mdp
->speed
= phydev
->speed
;
1643 if (mdp
->cd
->set_rate
)
1644 mdp
->cd
->set_rate(ndev
);
1648 (sh_eth_read(ndev
, ECMR
) & ~ECMR_TXF
), ECMR
);
1650 mdp
->link
= phydev
->link
;
1651 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1652 sh_eth_rcv_snd_enable(ndev
);
1654 } else if (mdp
->link
) {
1659 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
)
1660 sh_eth_rcv_snd_disable(ndev
);
1663 if (new_state
&& netif_msg_link(mdp
))
1664 phy_print_status(phydev
);
1667 /* PHY init function */
1668 static int sh_eth_phy_init(struct net_device
*ndev
)
1670 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1671 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1672 struct phy_device
*phydev
= NULL
;
1674 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
1675 mdp
->mii_bus
->id
, mdp
->phy_id
);
1681 /* Try connect to PHY */
1682 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
1683 mdp
->phy_interface
);
1684 if (IS_ERR(phydev
)) {
1685 dev_err(&ndev
->dev
, "phy_connect failed\n");
1686 return PTR_ERR(phydev
);
1689 dev_info(&ndev
->dev
, "attached phy %i to driver %s\n",
1690 phydev
->addr
, phydev
->drv
->name
);
1692 mdp
->phydev
= phydev
;
1697 /* PHY control start function */
1698 static int sh_eth_phy_start(struct net_device
*ndev
)
1700 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1703 ret
= sh_eth_phy_init(ndev
);
1707 /* reset phy - this also wakes it from PDOWN */
1708 phy_write(mdp
->phydev
, MII_BMCR
, BMCR_RESET
);
1709 phy_start(mdp
->phydev
);
1714 static int sh_eth_get_settings(struct net_device
*ndev
,
1715 struct ethtool_cmd
*ecmd
)
1717 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1718 unsigned long flags
;
1721 spin_lock_irqsave(&mdp
->lock
, flags
);
1722 ret
= phy_ethtool_gset(mdp
->phydev
, ecmd
);
1723 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1728 static int sh_eth_set_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
);
1737 /* disable tx and rx */
1738 sh_eth_rcv_snd_disable(ndev
);
1740 ret
= phy_ethtool_sset(mdp
->phydev
, ecmd
);
1744 if (ecmd
->duplex
== DUPLEX_FULL
)
1749 if (mdp
->cd
->set_duplex
)
1750 mdp
->cd
->set_duplex(ndev
);
1755 /* enable tx and rx */
1756 sh_eth_rcv_snd_enable(ndev
);
1758 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1763 static int sh_eth_nway_reset(struct net_device
*ndev
)
1765 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1766 unsigned long flags
;
1769 spin_lock_irqsave(&mdp
->lock
, flags
);
1770 ret
= phy_start_aneg(mdp
->phydev
);
1771 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1776 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
1778 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1779 return mdp
->msg_enable
;
1782 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
1784 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1785 mdp
->msg_enable
= value
;
1788 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1789 "rx_current", "tx_current",
1790 "rx_dirty", "tx_dirty",
1792 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1794 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
1798 return SH_ETH_STATS_LEN
;
1804 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
1805 struct ethtool_stats
*stats
, u64
*data
)
1807 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1810 /* device-specific stats */
1811 data
[i
++] = mdp
->cur_rx
;
1812 data
[i
++] = mdp
->cur_tx
;
1813 data
[i
++] = mdp
->dirty_rx
;
1814 data
[i
++] = mdp
->dirty_tx
;
1817 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1819 switch (stringset
) {
1821 memcpy(data
, *sh_eth_gstrings_stats
,
1822 sizeof(sh_eth_gstrings_stats
));
1827 static void sh_eth_get_ringparam(struct net_device
*ndev
,
1828 struct ethtool_ringparam
*ring
)
1830 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1832 ring
->rx_max_pending
= RX_RING_MAX
;
1833 ring
->tx_max_pending
= TX_RING_MAX
;
1834 ring
->rx_pending
= mdp
->num_rx_ring
;
1835 ring
->tx_pending
= mdp
->num_tx_ring
;
1838 static int sh_eth_set_ringparam(struct net_device
*ndev
,
1839 struct ethtool_ringparam
*ring
)
1841 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1844 if (ring
->tx_pending
> TX_RING_MAX
||
1845 ring
->rx_pending
> RX_RING_MAX
||
1846 ring
->tx_pending
< TX_RING_MIN
||
1847 ring
->rx_pending
< RX_RING_MIN
)
1849 if (ring
->rx_mini_pending
|| ring
->rx_jumbo_pending
)
1852 if (netif_running(ndev
)) {
1853 netif_tx_disable(ndev
);
1854 /* Disable interrupts by clearing the interrupt mask. */
1855 sh_eth_write(ndev
, 0x0000, EESIPR
);
1856 /* Stop the chip's Tx and Rx processes. */
1857 sh_eth_write(ndev
, 0, EDTRR
);
1858 sh_eth_write(ndev
, 0, EDRRR
);
1859 synchronize_irq(ndev
->irq
);
1862 /* Free all the skbuffs in the Rx queue. */
1863 sh_eth_ring_free(ndev
);
1864 /* Free DMA buffer */
1865 sh_eth_free_dma_buffer(mdp
);
1867 /* Set new parameters */
1868 mdp
->num_rx_ring
= ring
->rx_pending
;
1869 mdp
->num_tx_ring
= ring
->tx_pending
;
1871 ret
= sh_eth_ring_init(ndev
);
1873 dev_err(&ndev
->dev
, "%s: sh_eth_ring_init failed.\n", __func__
);
1876 ret
= sh_eth_dev_init(ndev
, false);
1878 dev_err(&ndev
->dev
, "%s: sh_eth_dev_init failed.\n", __func__
);
1882 if (netif_running(ndev
)) {
1883 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
1884 /* Setting the Rx mode will start the Rx process. */
1885 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1886 netif_wake_queue(ndev
);
1892 static const struct ethtool_ops sh_eth_ethtool_ops
= {
1893 .get_settings
= sh_eth_get_settings
,
1894 .set_settings
= sh_eth_set_settings
,
1895 .nway_reset
= sh_eth_nway_reset
,
1896 .get_msglevel
= sh_eth_get_msglevel
,
1897 .set_msglevel
= sh_eth_set_msglevel
,
1898 .get_link
= ethtool_op_get_link
,
1899 .get_strings
= sh_eth_get_strings
,
1900 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
1901 .get_sset_count
= sh_eth_get_sset_count
,
1902 .get_ringparam
= sh_eth_get_ringparam
,
1903 .set_ringparam
= sh_eth_set_ringparam
,
1906 /* network device open function */
1907 static int sh_eth_open(struct net_device
*ndev
)
1910 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1912 pm_runtime_get_sync(&mdp
->pdev
->dev
);
1914 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
1915 mdp
->cd
->irq_flags
, ndev
->name
, ndev
);
1917 dev_err(&ndev
->dev
, "Can not assign IRQ number\n");
1921 /* Descriptor set */
1922 ret
= sh_eth_ring_init(ndev
);
1927 ret
= sh_eth_dev_init(ndev
, true);
1931 /* PHY control start*/
1932 ret
= sh_eth_phy_start(ndev
);
1939 free_irq(ndev
->irq
, ndev
);
1940 pm_runtime_put_sync(&mdp
->pdev
->dev
);
1944 /* Timeout function */
1945 static void sh_eth_tx_timeout(struct net_device
*ndev
)
1947 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1948 struct sh_eth_rxdesc
*rxdesc
;
1951 netif_stop_queue(ndev
);
1953 if (netif_msg_timer(mdp
))
1954 dev_err(&ndev
->dev
, "%s: transmit timed out, status %8.8x,"
1955 " resetting...\n", ndev
->name
, (int)sh_eth_read(ndev
, EESR
));
1957 /* tx_errors count up */
1958 ndev
->stats
.tx_errors
++;
1960 /* Free all the skbuffs in the Rx queue. */
1961 for (i
= 0; i
< mdp
->num_rx_ring
; i
++) {
1962 rxdesc
= &mdp
->rx_ring
[i
];
1964 rxdesc
->addr
= 0xBADF00D0;
1965 if (mdp
->rx_skbuff
[i
])
1966 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1967 mdp
->rx_skbuff
[i
] = NULL
;
1969 for (i
= 0; i
< mdp
->num_tx_ring
; i
++) {
1970 if (mdp
->tx_skbuff
[i
])
1971 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1972 mdp
->tx_skbuff
[i
] = NULL
;
1976 sh_eth_dev_init(ndev
, true);
1979 /* Packet transmit function */
1980 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1982 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1983 struct sh_eth_txdesc
*txdesc
;
1985 unsigned long flags
;
1987 spin_lock_irqsave(&mdp
->lock
, flags
);
1988 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (mdp
->num_tx_ring
- 4)) {
1989 if (!sh_eth_txfree(ndev
)) {
1990 if (netif_msg_tx_queued(mdp
))
1991 dev_warn(&ndev
->dev
, "TxFD exhausted.\n");
1992 netif_stop_queue(ndev
);
1993 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1994 return NETDEV_TX_BUSY
;
1997 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1999 entry
= mdp
->cur_tx
% mdp
->num_tx_ring
;
2000 mdp
->tx_skbuff
[entry
] = skb
;
2001 txdesc
= &mdp
->tx_ring
[entry
];
2003 if (!mdp
->cd
->hw_swap
)
2004 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc
->addr
, 4)),
2006 txdesc
->addr
= dma_map_single(&ndev
->dev
, skb
->data
, skb
->len
,
2008 if (skb
->len
< ETHERSMALL
)
2009 txdesc
->buffer_length
= ETHERSMALL
;
2011 txdesc
->buffer_length
= skb
->len
;
2013 if (entry
>= mdp
->num_tx_ring
- 1)
2014 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
| TD_TDLE
);
2016 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
);
2020 if (!(sh_eth_read(ndev
, EDTRR
) & sh_eth_get_edtrr_trns(mdp
)))
2021 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
2023 return NETDEV_TX_OK
;
2026 /* device close function */
2027 static int sh_eth_close(struct net_device
*ndev
)
2029 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2031 netif_stop_queue(ndev
);
2033 /* Disable interrupts by clearing the interrupt mask. */
2034 sh_eth_write(ndev
, 0x0000, EESIPR
);
2036 /* Stop the chip's Tx and Rx processes. */
2037 sh_eth_write(ndev
, 0, EDTRR
);
2038 sh_eth_write(ndev
, 0, EDRRR
);
2040 /* PHY Disconnect */
2042 phy_stop(mdp
->phydev
);
2043 phy_disconnect(mdp
->phydev
);
2046 free_irq(ndev
->irq
, ndev
);
2048 /* Free all the skbuffs in the Rx queue. */
2049 sh_eth_ring_free(ndev
);
2051 /* free DMA buffer */
2052 sh_eth_free_dma_buffer(mdp
);
2054 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2059 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
2061 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2063 pm_runtime_get_sync(&mdp
->pdev
->dev
);
2065 ndev
->stats
.tx_dropped
+= sh_eth_read(ndev
, TROCR
);
2066 sh_eth_write(ndev
, 0, TROCR
); /* (write clear) */
2067 ndev
->stats
.collisions
+= sh_eth_read(ndev
, CDCR
);
2068 sh_eth_write(ndev
, 0, CDCR
); /* (write clear) */
2069 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, LCCR
);
2070 sh_eth_write(ndev
, 0, LCCR
); /* (write clear) */
2071 if (sh_eth_is_gether(mdp
)) {
2072 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CERCR
);
2073 sh_eth_write(ndev
, 0, CERCR
); /* (write clear) */
2074 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CEECR
);
2075 sh_eth_write(ndev
, 0, CEECR
); /* (write clear) */
2077 ndev
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CNDCR
);
2078 sh_eth_write(ndev
, 0, CNDCR
); /* (write clear) */
2080 pm_runtime_put_sync(&mdp
->pdev
->dev
);
2082 return &ndev
->stats
;
2085 /* ioctl to device function */
2086 static int sh_eth_do_ioctl(struct net_device
*ndev
, struct ifreq
*rq
,
2089 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2090 struct phy_device
*phydev
= mdp
->phydev
;
2092 if (!netif_running(ndev
))
2098 return phy_mii_ioctl(phydev
, rq
, cmd
);
2101 /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2102 static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private
*mdp
,
2105 return sh_eth_tsu_get_offset(mdp
, TSU_POST1
) + (entry
/ 8 * 4);
2108 static u32
sh_eth_tsu_get_post_mask(int entry
)
2110 return 0x0f << (28 - ((entry
% 8) * 4));
2113 static u32
sh_eth_tsu_get_post_bit(struct sh_eth_private
*mdp
, int entry
)
2115 return (0x08 >> (mdp
->port
<< 1)) << (28 - ((entry
% 8) * 4));
2118 static void sh_eth_tsu_enable_cam_entry_post(struct net_device
*ndev
,
2121 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2125 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2126 tmp
= ioread32(reg_offset
);
2127 iowrite32(tmp
| sh_eth_tsu_get_post_bit(mdp
, entry
), reg_offset
);
2130 static bool sh_eth_tsu_disable_cam_entry_post(struct net_device
*ndev
,
2133 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2134 u32 post_mask
, ref_mask
, tmp
;
2137 reg_offset
= sh_eth_tsu_get_post_reg_offset(mdp
, entry
);
2138 post_mask
= sh_eth_tsu_get_post_mask(entry
);
2139 ref_mask
= sh_eth_tsu_get_post_bit(mdp
, entry
) & ~post_mask
;
2141 tmp
= ioread32(reg_offset
);
2142 iowrite32(tmp
& ~post_mask
, reg_offset
);
2144 /* If other port enables, the function returns "true" */
2145 return tmp
& ref_mask
;
2148 static int sh_eth_tsu_busy(struct net_device
*ndev
)
2150 int timeout
= SH_ETH_TSU_TIMEOUT_MS
* 100;
2151 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2153 while ((sh_eth_tsu_read(mdp
, TSU_ADSBSY
) & TSU_ADSBSY_0
)) {
2157 dev_err(&ndev
->dev
, "%s: timeout\n", __func__
);
2165 static int sh_eth_tsu_write_entry(struct net_device
*ndev
, void *reg
,
2170 val
= addr
[0] << 24 | addr
[1] << 16 | addr
[2] << 8 | addr
[3];
2171 iowrite32(val
, reg
);
2172 if (sh_eth_tsu_busy(ndev
) < 0)
2175 val
= addr
[4] << 8 | addr
[5];
2176 iowrite32(val
, reg
+ 4);
2177 if (sh_eth_tsu_busy(ndev
) < 0)
2183 static void sh_eth_tsu_read_entry(void *reg
, u8
*addr
)
2187 val
= ioread32(reg
);
2188 addr
[0] = (val
>> 24) & 0xff;
2189 addr
[1] = (val
>> 16) & 0xff;
2190 addr
[2] = (val
>> 8) & 0xff;
2191 addr
[3] = val
& 0xff;
2192 val
= ioread32(reg
+ 4);
2193 addr
[4] = (val
>> 8) & 0xff;
2194 addr
[5] = val
& 0xff;
2198 static int sh_eth_tsu_find_entry(struct net_device
*ndev
, const u8
*addr
)
2200 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2201 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2203 u8 c_addr
[ETH_ALEN
];
2205 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2206 sh_eth_tsu_read_entry(reg_offset
, c_addr
);
2207 if (memcmp(addr
, c_addr
, ETH_ALEN
) == 0)
2214 static int sh_eth_tsu_find_empty(struct net_device
*ndev
)
2219 memset(blank
, 0, sizeof(blank
));
2220 entry
= sh_eth_tsu_find_entry(ndev
, blank
);
2221 return (entry
< 0) ? -ENOMEM
: entry
;
2224 static int sh_eth_tsu_disable_cam_entry_table(struct net_device
*ndev
,
2227 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2228 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2232 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) &
2233 ~(1 << (31 - entry
)), TSU_TEN
);
2235 memset(blank
, 0, sizeof(blank
));
2236 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ entry
* 8, blank
);
2242 static int sh_eth_tsu_add_entry(struct net_device
*ndev
, const u8
*addr
)
2244 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2245 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2251 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2253 /* No entry found, create one */
2254 i
= sh_eth_tsu_find_empty(ndev
);
2257 ret
= sh_eth_tsu_write_entry(ndev
, reg_offset
+ i
* 8, addr
);
2261 /* Enable the entry */
2262 sh_eth_tsu_write(mdp
, sh_eth_tsu_read(mdp
, TSU_TEN
) |
2263 (1 << (31 - i
)), TSU_TEN
);
2266 /* Entry found or created, enable POST */
2267 sh_eth_tsu_enable_cam_entry_post(ndev
, i
);
2272 static int sh_eth_tsu_del_entry(struct net_device
*ndev
, const u8
*addr
)
2274 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2280 i
= sh_eth_tsu_find_entry(ndev
, addr
);
2283 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2286 /* Disable the entry if both ports was disabled */
2287 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2295 static int sh_eth_tsu_purge_all(struct net_device
*ndev
)
2297 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2300 if (unlikely(!mdp
->cd
->tsu
))
2303 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++) {
2304 if (sh_eth_tsu_disable_cam_entry_post(ndev
, i
))
2307 /* Disable the entry if both ports was disabled */
2308 ret
= sh_eth_tsu_disable_cam_entry_table(ndev
, i
);
2316 static void sh_eth_tsu_purge_mcast(struct net_device
*ndev
)
2318 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2320 void *reg_offset
= sh_eth_tsu_get_offset(mdp
, TSU_ADRH0
);
2323 if (unlikely(!mdp
->cd
->tsu
))
2326 for (i
= 0; i
< SH_ETH_TSU_CAM_ENTRIES
; i
++, reg_offset
+= 8) {
2327 sh_eth_tsu_read_entry(reg_offset
, addr
);
2328 if (is_multicast_ether_addr(addr
))
2329 sh_eth_tsu_del_entry(ndev
, addr
);
2333 /* Multicast reception directions set */
2334 static void sh_eth_set_multicast_list(struct net_device
*ndev
)
2336 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2339 unsigned long flags
;
2341 spin_lock_irqsave(&mdp
->lock
, flags
);
2343 * Initial condition is MCT = 1, PRM = 0.
2344 * Depending on ndev->flags, set PRM or clear MCT
2346 ecmr_bits
= (sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
) | ECMR_MCT
;
2348 if (!(ndev
->flags
& IFF_MULTICAST
)) {
2349 sh_eth_tsu_purge_mcast(ndev
);
2352 if (ndev
->flags
& IFF_ALLMULTI
) {
2353 sh_eth_tsu_purge_mcast(ndev
);
2354 ecmr_bits
&= ~ECMR_MCT
;
2358 if (ndev
->flags
& IFF_PROMISC
) {
2359 sh_eth_tsu_purge_all(ndev
);
2360 ecmr_bits
= (ecmr_bits
& ~ECMR_MCT
) | ECMR_PRM
;
2361 } else if (mdp
->cd
->tsu
) {
2362 struct netdev_hw_addr
*ha
;
2363 netdev_for_each_mc_addr(ha
, ndev
) {
2364 if (mcast_all
&& is_multicast_ether_addr(ha
->addr
))
2367 if (sh_eth_tsu_add_entry(ndev
, ha
->addr
) < 0) {
2369 sh_eth_tsu_purge_mcast(ndev
);
2370 ecmr_bits
&= ~ECMR_MCT
;
2376 /* Normal, unicast/broadcast-only mode. */
2377 ecmr_bits
= (ecmr_bits
& ~ECMR_PRM
) | ECMR_MCT
;
2380 /* update the ethernet mode */
2381 sh_eth_write(ndev
, ecmr_bits
, ECMR
);
2383 spin_unlock_irqrestore(&mdp
->lock
, flags
);
2386 static int sh_eth_get_vtag_index(struct sh_eth_private
*mdp
)
2394 static int sh_eth_vlan_rx_add_vid(struct net_device
*ndev
,
2395 __be16 proto
, u16 vid
)
2397 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2398 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2400 if (unlikely(!mdp
->cd
->tsu
))
2403 /* No filtering if vid = 0 */
2407 mdp
->vlan_num_ids
++;
2410 * The controller has one VLAN tag HW filter. So, if the filter is
2411 * already enabled, the driver disables it and the filte
2413 if (mdp
->vlan_num_ids
> 1) {
2414 /* disable VLAN filter */
2415 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2419 sh_eth_tsu_write(mdp
, TSU_VTAG_ENABLE
| (vid
& TSU_VTAG_VID_MASK
),
2425 static int sh_eth_vlan_rx_kill_vid(struct net_device
*ndev
,
2426 __be16 proto
, u16 vid
)
2428 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2429 int vtag_reg_index
= sh_eth_get_vtag_index(mdp
);
2431 if (unlikely(!mdp
->cd
->tsu
))
2434 /* No filtering if vid = 0 */
2438 mdp
->vlan_num_ids
--;
2439 sh_eth_tsu_write(mdp
, 0, vtag_reg_index
);
2444 /* SuperH's TSU register init function */
2445 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
2447 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
2448 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
2449 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
2450 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
2451 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
2452 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
2453 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
2454 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
2455 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
2456 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
2457 if (sh_eth_is_gether(mdp
)) {
2458 sh_eth_tsu_write(mdp
, 0, TSU_QTAG0
); /* Disable QTAG(0->1) */
2459 sh_eth_tsu_write(mdp
, 0, TSU_QTAG1
); /* Disable QTAG(1->0) */
2461 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
2462 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
2464 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
2465 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
2466 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
2467 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
2468 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
2469 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
2470 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
2473 /* MDIO bus release function */
2474 static int sh_mdio_release(struct net_device
*ndev
)
2476 struct mii_bus
*bus
= dev_get_drvdata(&ndev
->dev
);
2478 /* unregister mdio bus */
2479 mdiobus_unregister(bus
);
2481 /* remove mdio bus info from net_device */
2482 dev_set_drvdata(&ndev
->dev
, NULL
);
2484 /* free bitbang info */
2485 free_mdio_bitbang(bus
);
2490 /* MDIO bus init function */
2491 static int sh_mdio_init(struct net_device
*ndev
, int id
,
2492 struct sh_eth_plat_data
*pd
)
2495 struct bb_info
*bitbang
;
2496 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
2498 /* create bit control struct for PHY */
2499 bitbang
= devm_kzalloc(&ndev
->dev
, sizeof(struct bb_info
),
2507 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
2508 bitbang
->set_gate
= pd
->set_mdio_gate
;
2509 bitbang
->mdi_msk
= PIR_MDI
;
2510 bitbang
->mdo_msk
= PIR_MDO
;
2511 bitbang
->mmd_msk
= PIR_MMD
;
2512 bitbang
->mdc_msk
= PIR_MDC
;
2513 bitbang
->ctrl
.ops
= &bb_ops
;
2515 /* MII controller setting */
2516 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
2517 if (!mdp
->mii_bus
) {
2522 /* Hook up MII support for ethtool */
2523 mdp
->mii_bus
->name
= "sh_mii";
2524 mdp
->mii_bus
->parent
= &ndev
->dev
;
2525 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
2526 mdp
->pdev
->name
, id
);
2529 mdp
->mii_bus
->irq
= devm_kzalloc(&ndev
->dev
,
2530 sizeof(int) * PHY_MAX_ADDR
,
2532 if (!mdp
->mii_bus
->irq
) {
2537 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
2538 mdp
->mii_bus
->irq
[i
] = PHY_POLL
;
2540 /* register mdio bus */
2541 ret
= mdiobus_register(mdp
->mii_bus
);
2545 dev_set_drvdata(&ndev
->dev
, mdp
->mii_bus
);
2550 free_mdio_bitbang(mdp
->mii_bus
);
2556 static const u16
*sh_eth_get_register_offset(int register_type
)
2558 const u16
*reg_offset
= NULL
;
2560 switch (register_type
) {
2561 case SH_ETH_REG_GIGABIT
:
2562 reg_offset
= sh_eth_offset_gigabit
;
2564 case SH_ETH_REG_FAST_RCAR
:
2565 reg_offset
= sh_eth_offset_fast_rcar
;
2567 case SH_ETH_REG_FAST_SH4
:
2568 reg_offset
= sh_eth_offset_fast_sh4
;
2570 case SH_ETH_REG_FAST_SH3_SH2
:
2571 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
2574 pr_err("Unknown register type (%d)\n", register_type
);
2581 static struct net_device_ops sh_eth_netdev_ops
= {
2582 .ndo_open
= sh_eth_open
,
2583 .ndo_stop
= sh_eth_close
,
2584 .ndo_start_xmit
= sh_eth_start_xmit
,
2585 .ndo_get_stats
= sh_eth_get_stats
,
2586 .ndo_tx_timeout
= sh_eth_tx_timeout
,
2587 .ndo_do_ioctl
= sh_eth_do_ioctl
,
2588 .ndo_validate_addr
= eth_validate_addr
,
2589 .ndo_set_mac_address
= eth_mac_addr
,
2590 .ndo_change_mtu
= eth_change_mtu
,
2593 static int sh_eth_drv_probe(struct platform_device
*pdev
)
2596 struct resource
*res
;
2597 struct net_device
*ndev
= NULL
;
2598 struct sh_eth_private
*mdp
= NULL
;
2599 struct sh_eth_plat_data
*pd
= pdev
->dev
.platform_data
;
2602 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2603 if (unlikely(res
== NULL
)) {
2604 dev_err(&pdev
->dev
, "invalid resource\n");
2609 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
2615 /* The sh Ether-specific entries in the device structure. */
2616 ndev
->base_addr
= res
->start
;
2622 ret
= platform_get_irq(pdev
, 0);
2629 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2631 /* Fill in the fields of the device structure with ethernet values. */
2634 mdp
= netdev_priv(ndev
);
2635 mdp
->num_tx_ring
= TX_RING_SIZE
;
2636 mdp
->num_rx_ring
= RX_RING_SIZE
;
2637 mdp
->addr
= devm_ioremap_resource(&pdev
->dev
, res
);
2638 if (IS_ERR(mdp
->addr
)) {
2639 ret
= PTR_ERR(mdp
->addr
);
2643 spin_lock_init(&mdp
->lock
);
2645 pm_runtime_enable(&pdev
->dev
);
2646 pm_runtime_resume(&pdev
->dev
);
2649 mdp
->phy_id
= pd
->phy
;
2650 mdp
->phy_interface
= pd
->phy_interface
;
2652 mdp
->edmac_endian
= pd
->edmac_endian
;
2653 mdp
->no_ether_link
= pd
->no_ether_link
;
2654 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
2655 mdp
->reg_offset
= sh_eth_get_register_offset(pd
->register_type
);
2658 #if defined(SH_ETH_HAS_BOTH_MODULES)
2659 mdp
->cd
= sh_eth_get_cpu_data(mdp
);
2661 mdp
->cd
= &sh_eth_my_cpu_data
;
2663 sh_eth_set_default_cpu_data(mdp
->cd
);
2667 sh_eth_netdev_ops
.ndo_set_rx_mode
= sh_eth_set_multicast_list
;
2668 sh_eth_netdev_ops
.ndo_vlan_rx_add_vid
= sh_eth_vlan_rx_add_vid
;
2669 sh_eth_netdev_ops
.ndo_vlan_rx_kill_vid
=
2670 sh_eth_vlan_rx_kill_vid
;
2673 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
2674 SET_ETHTOOL_OPS(ndev
, &sh_eth_ethtool_ops
);
2675 ndev
->watchdog_timeo
= TX_TIMEOUT
;
2677 /* debug message level */
2678 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
2680 /* read and set MAC address */
2681 read_mac_address(ndev
, pd
->mac_addr
);
2682 if (!is_valid_ether_addr(ndev
->dev_addr
)) {
2683 dev_warn(&pdev
->dev
,
2684 "no valid MAC address supplied, using a random one.\n");
2685 eth_hw_addr_random(ndev
);
2688 /* ioremap the TSU registers */
2690 struct resource
*rtsu
;
2691 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
2692 mdp
->tsu_addr
= devm_ioremap_resource(&pdev
->dev
, rtsu
);
2693 if (IS_ERR(mdp
->tsu_addr
)) {
2694 ret
= PTR_ERR(mdp
->tsu_addr
);
2697 mdp
->port
= devno
% 2;
2698 ndev
->features
= NETIF_F_HW_VLAN_CTAG_FILTER
;
2701 /* initialize first or needed device */
2702 if (!devno
|| pd
->needs_init
) {
2703 if (mdp
->cd
->chip_reset
)
2704 mdp
->cd
->chip_reset(ndev
);
2707 /* TSU init (Init only)*/
2708 sh_eth_tsu_init(mdp
);
2712 /* network device register */
2713 ret
= register_netdev(ndev
);
2718 ret
= sh_mdio_init(ndev
, pdev
->id
, pd
);
2720 goto out_unregister
;
2722 /* print device information */
2723 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
2724 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
2726 platform_set_drvdata(pdev
, ndev
);
2731 unregister_netdev(ndev
);
2742 static int sh_eth_drv_remove(struct platform_device
*pdev
)
2744 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2746 sh_mdio_release(ndev
);
2747 unregister_netdev(ndev
);
2748 pm_runtime_disable(&pdev
->dev
);
2754 static int sh_eth_runtime_nop(struct device
*dev
)
2757 * Runtime PM callback shared between ->runtime_suspend()
2758 * and ->runtime_resume(). Simply returns success.
2760 * This driver re-initializes all registers after
2761 * pm_runtime_get_sync() anyway so there is no need
2762 * to save and restore registers here.
2767 static struct dev_pm_ops sh_eth_dev_pm_ops
= {
2768 .runtime_suspend
= sh_eth_runtime_nop
,
2769 .runtime_resume
= sh_eth_runtime_nop
,
2772 static struct platform_driver sh_eth_driver
= {
2773 .probe
= sh_eth_drv_probe
,
2774 .remove
= sh_eth_drv_remove
,
2777 .pm
= &sh_eth_dev_pm_ops
,
2781 module_platform_driver(sh_eth_driver
);
2783 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
2784 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
2785 MODULE_LICENSE("GPL v2");