2 * SuperH Ethernet device driver
4 * Copyright (C) 2006-2008 Nobuhiro Iwamatsu
5 * Copyright (C) 2008-2009 Renesas Solutions Corp.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 * The full GNU General Public License is included in this distribution in
20 * the file called "COPYING".
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/etherdevice.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32 #include <linux/mdio-bitbang.h>
33 #include <linux/netdevice.h>
34 #include <linux/phy.h>
35 #include <linux/cache.h>
37 #include <linux/interrupt.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/sh_eth.h>
46 #define SH_ETH_DEF_MSG_ENABLE \
52 /* There is CPU dependent code */
53 #if defined(CONFIG_CPU_SUBTYPE_SH7724)
54 #define SH_ETH_RESET_DEFAULT 1
55 static void sh_eth_set_duplex(struct net_device
*ndev
)
57 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
59 if (mdp
->duplex
) /* Full */
60 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
62 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
65 static void sh_eth_set_rate(struct net_device
*ndev
)
67 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
71 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_RTM
, ECMR
);
73 case 100:/* 100BASE */
74 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_RTM
, ECMR
);
82 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
83 .set_duplex
= sh_eth_set_duplex
,
84 .set_rate
= sh_eth_set_rate
,
86 .ecsr_value
= ECSR_PSRTO
| ECSR_LCHNG
| ECSR_ICD
,
87 .ecsipr_value
= ECSIPR_PSRTOIP
| ECSIPR_LCHNGIP
| ECSIPR_ICDIP
,
88 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x01ff009f,
90 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
91 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RDE
|
92 EESR_RFRMER
| EESR_TFE
| EESR_TDE
| EESR_ECI
,
93 .tx_error_check
= EESR_TWB
| EESR_TABT
| EESR_TDE
| EESR_TFE
,
100 .rpadir_value
= 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
102 #elif defined(CONFIG_CPU_SUBTYPE_SH7757)
103 #define SH_ETH_HAS_BOTH_MODULES 1
104 #define SH_ETH_HAS_TSU 1
105 static void sh_eth_set_duplex(struct net_device
*ndev
)
107 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
109 if (mdp
->duplex
) /* Full */
110 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
112 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
115 static void sh_eth_set_rate(struct net_device
*ndev
)
117 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
119 switch (mdp
->speed
) {
120 case 10: /* 10BASE */
121 sh_eth_write(ndev
, 0, RTRATE
);
123 case 100:/* 100BASE */
124 sh_eth_write(ndev
, 1, RTRATE
);
132 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
133 .set_duplex
= sh_eth_set_duplex
,
134 .set_rate
= sh_eth_set_rate
,
136 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
137 .rmcr_value
= 0x00000001,
139 .tx_check
= EESR_FTC
| EESR_CND
| EESR_DLC
| EESR_CD
| EESR_RTO
,
140 .eesr_err_check
= EESR_TWB
| EESR_TABT
| EESR_RABT
| EESR_RDE
|
141 EESR_RFRMER
| EESR_TFE
| EESR_TDE
| EESR_ECI
,
142 .tx_error_check
= EESR_TWB
| EESR_TABT
| EESR_TDE
| EESR_TFE
,
150 .rpadir_value
= 2 << 16,
153 #define SH_GIGA_ETH_BASE 0xfee00000
154 #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
155 #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
156 static void sh_eth_chip_reset_giga(struct net_device
*ndev
)
159 unsigned long mahr
[2], malr
[2];
161 /* save MAHR and MALR */
162 for (i
= 0; i
< 2; i
++) {
163 malr
[i
] = ioread32((void *)GIGA_MALR(i
));
164 mahr
[i
] = ioread32((void *)GIGA_MAHR(i
));
168 iowrite32(ARSTR_ARSTR
, (void *)(SH_GIGA_ETH_BASE
+ 0x1800));
171 /* restore MAHR and MALR */
172 for (i
= 0; i
< 2; i
++) {
173 iowrite32(malr
[i
], (void *)GIGA_MALR(i
));
174 iowrite32(mahr
[i
], (void *)GIGA_MAHR(i
));
178 static int sh_eth_is_gether(struct sh_eth_private
*mdp
);
179 static void sh_eth_reset(struct net_device
*ndev
)
181 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
184 if (sh_eth_is_gether(mdp
)) {
185 sh_eth_write(ndev
, 0x03, EDSR
);
186 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
,
189 if (!(sh_eth_read(ndev
, EDMR
) & 0x3))
195 printk(KERN_ERR
"Device reset fail\n");
198 sh_eth_write(ndev
, 0x0, TDLAR
);
199 sh_eth_write(ndev
, 0x0, TDFAR
);
200 sh_eth_write(ndev
, 0x0, TDFXR
);
201 sh_eth_write(ndev
, 0x0, TDFFR
);
202 sh_eth_write(ndev
, 0x0, RDLAR
);
203 sh_eth_write(ndev
, 0x0, RDFAR
);
204 sh_eth_write(ndev
, 0x0, RDFXR
);
205 sh_eth_write(ndev
, 0x0, RDFFR
);
207 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_ETHER
,
210 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) & ~EDMR_SRST_ETHER
,
215 static void sh_eth_set_duplex_giga(struct net_device
*ndev
)
217 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
219 if (mdp
->duplex
) /* Full */
220 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
222 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
225 static void sh_eth_set_rate_giga(struct net_device
*ndev
)
227 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
229 switch (mdp
->speed
) {
230 case 10: /* 10BASE */
231 sh_eth_write(ndev
, 0x00000000, GECMR
);
233 case 100:/* 100BASE */
234 sh_eth_write(ndev
, 0x00000010, GECMR
);
236 case 1000: /* 1000BASE */
237 sh_eth_write(ndev
, 0x00000020, GECMR
);
244 /* SH7757(GETHERC) */
245 static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga
= {
246 .chip_reset
= sh_eth_chip_reset_giga
,
247 .set_duplex
= sh_eth_set_duplex_giga
,
248 .set_rate
= sh_eth_set_rate_giga
,
250 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
251 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
252 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
254 .tx_check
= EESR_TC1
| EESR_FTC
,
255 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
| \
256 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
| \
258 .tx_error_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_TDE
| \
260 .fdr_value
= 0x0000072f,
261 .rmcr_value
= 0x00000001,
269 .rpadir_value
= 2 << 16,
274 static struct sh_eth_cpu_data
*sh_eth_get_cpu_data(struct sh_eth_private
*mdp
)
276 if (sh_eth_is_gether(mdp
))
277 return &sh_eth_my_cpu_data_giga
;
279 return &sh_eth_my_cpu_data
;
282 #elif defined(CONFIG_CPU_SUBTYPE_SH7763)
283 #define SH_ETH_HAS_TSU 1
284 static void sh_eth_chip_reset(struct net_device
*ndev
)
286 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
289 sh_eth_tsu_write(mdp
, ARSTR_ARSTR
, ARSTR
);
293 static void sh_eth_reset(struct net_device
*ndev
)
297 sh_eth_write(ndev
, EDSR_ENALL
, EDSR
);
298 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_GETHER
, EDMR
);
300 if (!(sh_eth_read(ndev
, EDMR
) & 0x3))
306 printk(KERN_ERR
"Device reset fail\n");
309 sh_eth_write(ndev
, 0x0, TDLAR
);
310 sh_eth_write(ndev
, 0x0, TDFAR
);
311 sh_eth_write(ndev
, 0x0, TDFXR
);
312 sh_eth_write(ndev
, 0x0, TDFFR
);
313 sh_eth_write(ndev
, 0x0, RDLAR
);
314 sh_eth_write(ndev
, 0x0, RDFAR
);
315 sh_eth_write(ndev
, 0x0, RDFXR
);
316 sh_eth_write(ndev
, 0x0, RDFFR
);
319 static void sh_eth_set_duplex(struct net_device
*ndev
)
321 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
323 if (mdp
->duplex
) /* Full */
324 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) | ECMR_DM
, ECMR
);
326 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) & ~ECMR_DM
, ECMR
);
329 static void sh_eth_set_rate(struct net_device
*ndev
)
331 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
333 switch (mdp
->speed
) {
334 case 10: /* 10BASE */
335 sh_eth_write(ndev
, GECMR_10
, GECMR
);
337 case 100:/* 100BASE */
338 sh_eth_write(ndev
, GECMR_100
, GECMR
);
340 case 1000: /* 1000BASE */
341 sh_eth_write(ndev
, GECMR_1000
, GECMR
);
349 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
350 .chip_reset
= sh_eth_chip_reset
,
351 .set_duplex
= sh_eth_set_duplex
,
352 .set_rate
= sh_eth_set_rate
,
354 .ecsr_value
= ECSR_ICD
| ECSR_MPD
,
355 .ecsipr_value
= ECSIPR_LCHNGIP
| ECSIPR_ICDIP
| ECSIPR_MPDIP
,
356 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
358 .tx_check
= EESR_TC1
| EESR_FTC
,
359 .eesr_err_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_RABT
| \
360 EESR_RDE
| EESR_RFRMER
| EESR_TFE
| EESR_TDE
| \
362 .tx_error_check
= EESR_TWB1
| EESR_TWB
| EESR_TABT
| EESR_TDE
| \
375 #elif defined(CONFIG_CPU_SUBTYPE_SH7619)
376 #define SH_ETH_RESET_DEFAULT 1
377 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
378 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
385 #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
386 #define SH_ETH_RESET_DEFAULT 1
387 #define SH_ETH_HAS_TSU 1
388 static struct sh_eth_cpu_data sh_eth_my_cpu_data
= {
389 .eesipr_value
= DMAC_M_RFRMER
| DMAC_M_ECI
| 0x003fffff,
394 static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data
*cd
)
397 cd
->ecsr_value
= DEFAULT_ECSR_INIT
;
399 if (!cd
->ecsipr_value
)
400 cd
->ecsipr_value
= DEFAULT_ECSIPR_INIT
;
402 if (!cd
->fcftr_value
)
403 cd
->fcftr_value
= DEFAULT_FIFO_F_D_RFF
| \
404 DEFAULT_FIFO_F_D_RFD
;
407 cd
->fdr_value
= DEFAULT_FDR_INIT
;
410 cd
->rmcr_value
= DEFAULT_RMCR_VALUE
;
413 cd
->tx_check
= DEFAULT_TX_CHECK
;
415 if (!cd
->eesr_err_check
)
416 cd
->eesr_err_check
= DEFAULT_EESR_ERR_CHECK
;
418 if (!cd
->tx_error_check
)
419 cd
->tx_error_check
= DEFAULT_TX_ERROR_CHECK
;
422 #if defined(SH_ETH_RESET_DEFAULT)
424 static void sh_eth_reset(struct net_device
*ndev
)
426 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) | EDMR_SRST_ETHER
, EDMR
);
428 sh_eth_write(ndev
, sh_eth_read(ndev
, EDMR
) & ~EDMR_SRST_ETHER
, EDMR
);
432 #if defined(CONFIG_CPU_SH4)
433 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
437 reserve
= SH4_SKB_RX_ALIGN
- ((u32
)skb
->data
& (SH4_SKB_RX_ALIGN
- 1));
439 skb_reserve(skb
, reserve
);
442 static void sh_eth_set_receive_align(struct sk_buff
*skb
)
444 skb_reserve(skb
, SH2_SH3_SKB_RX_ALIGN
);
449 /* CPU <-> EDMAC endian convert */
450 static inline __u32
cpu_to_edmac(struct sh_eth_private
*mdp
, u32 x
)
452 switch (mdp
->edmac_endian
) {
453 case EDMAC_LITTLE_ENDIAN
:
454 return cpu_to_le32(x
);
455 case EDMAC_BIG_ENDIAN
:
456 return cpu_to_be32(x
);
461 static inline __u32
edmac_to_cpu(struct sh_eth_private
*mdp
, u32 x
)
463 switch (mdp
->edmac_endian
) {
464 case EDMAC_LITTLE_ENDIAN
:
465 return le32_to_cpu(x
);
466 case EDMAC_BIG_ENDIAN
:
467 return be32_to_cpu(x
);
473 * Program the hardware MAC address from dev->dev_addr.
475 static void update_mac_address(struct net_device
*ndev
)
478 (ndev
->dev_addr
[0] << 24) | (ndev
->dev_addr
[1] << 16) |
479 (ndev
->dev_addr
[2] << 8) | (ndev
->dev_addr
[3]), MAHR
);
481 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5]), MALR
);
485 * Get MAC address from SuperH MAC address register
487 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
488 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
489 * When you want use this device, you must set MAC address in bootloader.
492 static void read_mac_address(struct net_device
*ndev
, unsigned char *mac
)
494 if (mac
[0] || mac
[1] || mac
[2] || mac
[3] || mac
[4] || mac
[5]) {
495 memcpy(ndev
->dev_addr
, mac
, 6);
497 ndev
->dev_addr
[0] = (sh_eth_read(ndev
, MAHR
) >> 24);
498 ndev
->dev_addr
[1] = (sh_eth_read(ndev
, MAHR
) >> 16) & 0xFF;
499 ndev
->dev_addr
[2] = (sh_eth_read(ndev
, MAHR
) >> 8) & 0xFF;
500 ndev
->dev_addr
[3] = (sh_eth_read(ndev
, MAHR
) & 0xFF);
501 ndev
->dev_addr
[4] = (sh_eth_read(ndev
, MALR
) >> 8) & 0xFF;
502 ndev
->dev_addr
[5] = (sh_eth_read(ndev
, MALR
) & 0xFF);
506 static int sh_eth_is_gether(struct sh_eth_private
*mdp
)
508 if (mdp
->reg_offset
== sh_eth_offset_gigabit
)
514 static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private
*mdp
)
516 if (sh_eth_is_gether(mdp
))
517 return EDTRR_TRNS_GETHER
;
519 return EDTRR_TRNS_ETHER
;
523 void (*set_gate
)(void *addr
);
524 struct mdiobb_ctrl ctrl
;
526 u32 mmd_msk
;/* MMD */
533 static void bb_set(void *addr
, u32 msk
)
535 iowrite32(ioread32(addr
) | msk
, addr
);
539 static void bb_clr(void *addr
, u32 msk
)
541 iowrite32((ioread32(addr
) & ~msk
), addr
);
545 static int bb_read(void *addr
, u32 msk
)
547 return (ioread32(addr
) & msk
) != 0;
550 /* Data I/O pin control */
551 static void sh_mmd_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
553 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
555 if (bitbang
->set_gate
)
556 bitbang
->set_gate(bitbang
->addr
);
559 bb_set(bitbang
->addr
, bitbang
->mmd_msk
);
561 bb_clr(bitbang
->addr
, bitbang
->mmd_msk
);
565 static void sh_set_mdio(struct mdiobb_ctrl
*ctrl
, int bit
)
567 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
569 if (bitbang
->set_gate
)
570 bitbang
->set_gate(bitbang
->addr
);
573 bb_set(bitbang
->addr
, bitbang
->mdo_msk
);
575 bb_clr(bitbang
->addr
, bitbang
->mdo_msk
);
579 static int sh_get_mdio(struct mdiobb_ctrl
*ctrl
)
581 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
583 if (bitbang
->set_gate
)
584 bitbang
->set_gate(bitbang
->addr
);
586 return bb_read(bitbang
->addr
, bitbang
->mdi_msk
);
589 /* MDC pin control */
590 static void sh_mdc_ctrl(struct mdiobb_ctrl
*ctrl
, int bit
)
592 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
594 if (bitbang
->set_gate
)
595 bitbang
->set_gate(bitbang
->addr
);
598 bb_set(bitbang
->addr
, bitbang
->mdc_msk
);
600 bb_clr(bitbang
->addr
, bitbang
->mdc_msk
);
603 /* mdio bus control struct */
604 static struct mdiobb_ops bb_ops
= {
605 .owner
= THIS_MODULE
,
606 .set_mdc
= sh_mdc_ctrl
,
607 .set_mdio_dir
= sh_mmd_ctrl
,
608 .set_mdio_data
= sh_set_mdio
,
609 .get_mdio_data
= sh_get_mdio
,
612 /* free skb and descriptor buffer */
613 static void sh_eth_ring_free(struct net_device
*ndev
)
615 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
618 /* Free Rx skb ringbuffer */
619 if (mdp
->rx_skbuff
) {
620 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
621 if (mdp
->rx_skbuff
[i
])
622 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
625 kfree(mdp
->rx_skbuff
);
627 /* Free Tx skb ringbuffer */
628 if (mdp
->tx_skbuff
) {
629 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
630 if (mdp
->tx_skbuff
[i
])
631 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
634 kfree(mdp
->tx_skbuff
);
637 /* format skb and descriptor buffer */
638 static void sh_eth_ring_format(struct net_device
*ndev
)
640 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
643 struct sh_eth_rxdesc
*rxdesc
= NULL
;
644 struct sh_eth_txdesc
*txdesc
= NULL
;
645 int rx_ringsize
= sizeof(*rxdesc
) * RX_RING_SIZE
;
646 int tx_ringsize
= sizeof(*txdesc
) * TX_RING_SIZE
;
648 mdp
->cur_rx
= mdp
->cur_tx
= 0;
649 mdp
->dirty_rx
= mdp
->dirty_tx
= 0;
651 memset(mdp
->rx_ring
, 0, rx_ringsize
);
653 /* build Rx ring buffer */
654 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
656 mdp
->rx_skbuff
[i
] = NULL
;
657 skb
= dev_alloc_skb(mdp
->rx_buf_sz
);
658 mdp
->rx_skbuff
[i
] = skb
;
661 dma_map_single(&ndev
->dev
, skb
->tail
, mdp
->rx_buf_sz
,
663 skb
->dev
= ndev
; /* Mark as being used by this device. */
664 sh_eth_set_receive_align(skb
);
667 rxdesc
= &mdp
->rx_ring
[i
];
668 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
669 rxdesc
->status
= cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
671 /* The size of the buffer is 16 byte boundary. */
672 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
673 /* Rx descriptor address set */
675 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDLAR
);
676 if (sh_eth_is_gether(mdp
))
677 sh_eth_write(ndev
, mdp
->rx_desc_dma
, RDFAR
);
681 mdp
->dirty_rx
= (u32
) (i
- RX_RING_SIZE
);
683 /* Mark the last entry as wrapping the ring. */
684 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RDEL
);
686 memset(mdp
->tx_ring
, 0, tx_ringsize
);
688 /* build Tx ring buffer */
689 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
690 mdp
->tx_skbuff
[i
] = NULL
;
691 txdesc
= &mdp
->tx_ring
[i
];
692 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
693 txdesc
->buffer_length
= 0;
695 /* Tx descriptor address set */
696 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDLAR
);
697 if (sh_eth_is_gether(mdp
))
698 sh_eth_write(ndev
, mdp
->tx_desc_dma
, TDFAR
);
702 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
705 /* Get skb and descriptor buffer */
706 static int sh_eth_ring_init(struct net_device
*ndev
)
708 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
709 int rx_ringsize
, tx_ringsize
, ret
= 0;
712 * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
713 * card needs room to do 8 byte alignment, +2 so we can reserve
714 * the first 2 bytes, and +16 gets room for the status word from the
717 mdp
->rx_buf_sz
= (ndev
->mtu
<= 1492 ? PKT_BUF_SZ
:
718 (((ndev
->mtu
+ 26 + 7) & ~7) + 2 + 16));
720 mdp
->rx_buf_sz
+= NET_IP_ALIGN
;
722 /* Allocate RX and TX skb rings */
723 mdp
->rx_skbuff
= kmalloc(sizeof(*mdp
->rx_skbuff
) * RX_RING_SIZE
,
725 if (!mdp
->rx_skbuff
) {
726 dev_err(&ndev
->dev
, "Cannot allocate Rx skb\n");
731 mdp
->tx_skbuff
= kmalloc(sizeof(*mdp
->tx_skbuff
) * TX_RING_SIZE
,
733 if (!mdp
->tx_skbuff
) {
734 dev_err(&ndev
->dev
, "Cannot allocate Tx skb\n");
739 /* Allocate all Rx descriptors. */
740 rx_ringsize
= sizeof(struct sh_eth_rxdesc
) * RX_RING_SIZE
;
741 mdp
->rx_ring
= dma_alloc_coherent(NULL
, rx_ringsize
, &mdp
->rx_desc_dma
,
745 dev_err(&ndev
->dev
, "Cannot allocate Rx Ring (size %d bytes)\n",
753 /* Allocate all Tx descriptors. */
754 tx_ringsize
= sizeof(struct sh_eth_txdesc
) * TX_RING_SIZE
;
755 mdp
->tx_ring
= dma_alloc_coherent(NULL
, tx_ringsize
, &mdp
->tx_desc_dma
,
758 dev_err(&ndev
->dev
, "Cannot allocate Tx Ring (size %d bytes)\n",
766 /* free DMA buffer */
767 dma_free_coherent(NULL
, rx_ringsize
, mdp
->rx_ring
, mdp
->rx_desc_dma
);
770 /* Free Rx and Tx skb ring buffer */
771 sh_eth_ring_free(ndev
);
776 static int sh_eth_dev_init(struct net_device
*ndev
)
779 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
780 u_int32_t rx_int_var
, tx_int_var
;
786 /* Descriptor format */
787 sh_eth_ring_format(ndev
);
789 sh_eth_write(ndev
, mdp
->cd
->rpadir_value
, RPADIR
);
791 /* all sh_eth int mask */
792 sh_eth_write(ndev
, 0, EESIPR
);
794 #if defined(__LITTLE_ENDIAN__)
795 if (mdp
->cd
->hw_swap
)
796 sh_eth_write(ndev
, EDMR_EL
, EDMR
);
799 sh_eth_write(ndev
, 0, EDMR
);
802 sh_eth_write(ndev
, mdp
->cd
->fdr_value
, FDR
);
803 sh_eth_write(ndev
, 0, TFTR
);
805 /* Frame recv control */
806 sh_eth_write(ndev
, mdp
->cd
->rmcr_value
, RMCR
);
808 rx_int_var
= mdp
->rx_int_var
= DESC_I_RINT8
| DESC_I_RINT5
;
809 tx_int_var
= mdp
->tx_int_var
= DESC_I_TINT2
;
810 sh_eth_write(ndev
, rx_int_var
| tx_int_var
, TRSCER
);
813 sh_eth_write(ndev
, 0x800, BCULR
); /* Burst sycle set */
815 sh_eth_write(ndev
, mdp
->cd
->fcftr_value
, FCFTR
);
817 if (!mdp
->cd
->no_trimd
)
818 sh_eth_write(ndev
, 0, TRIMD
);
820 /* Recv frame limit set register */
821 sh_eth_write(ndev
, ndev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
+ ETH_FCS_LEN
,
824 sh_eth_write(ndev
, sh_eth_read(ndev
, EESR
), EESR
);
825 sh_eth_write(ndev
, mdp
->cd
->eesipr_value
, EESIPR
);
827 /* PAUSE Prohibition */
828 val
= (sh_eth_read(ndev
, ECMR
) & ECMR_DM
) |
829 ECMR_ZPF
| (mdp
->duplex
? ECMR_DM
: 0) | ECMR_TE
| ECMR_RE
;
831 sh_eth_write(ndev
, val
, ECMR
);
833 if (mdp
->cd
->set_rate
)
834 mdp
->cd
->set_rate(ndev
);
836 /* E-MAC Status Register clear */
837 sh_eth_write(ndev
, mdp
->cd
->ecsr_value
, ECSR
);
839 /* E-MAC Interrupt Enable register */
840 sh_eth_write(ndev
, mdp
->cd
->ecsipr_value
, ECSIPR
);
842 /* Set MAC address */
843 update_mac_address(ndev
);
847 sh_eth_write(ndev
, APR_AP
, APR
);
849 sh_eth_write(ndev
, MPR_MP
, MPR
);
850 if (mdp
->cd
->tpauser
)
851 sh_eth_write(ndev
, TPAUSER_UNLIMITED
, TPAUSER
);
853 /* Setting the Rx mode will start the Rx process. */
854 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
856 netif_start_queue(ndev
);
861 /* free Tx skb function */
862 static int sh_eth_txfree(struct net_device
*ndev
)
864 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
865 struct sh_eth_txdesc
*txdesc
;
869 for (; mdp
->cur_tx
- mdp
->dirty_tx
> 0; mdp
->dirty_tx
++) {
870 entry
= mdp
->dirty_tx
% TX_RING_SIZE
;
871 txdesc
= &mdp
->tx_ring
[entry
];
872 if (txdesc
->status
& cpu_to_edmac(mdp
, TD_TACT
))
874 /* Free the original skb. */
875 if (mdp
->tx_skbuff
[entry
]) {
876 dma_unmap_single(&ndev
->dev
, txdesc
->addr
,
877 txdesc
->buffer_length
, DMA_TO_DEVICE
);
878 dev_kfree_skb_irq(mdp
->tx_skbuff
[entry
]);
879 mdp
->tx_skbuff
[entry
] = NULL
;
882 txdesc
->status
= cpu_to_edmac(mdp
, TD_TFP
);
883 if (entry
>= TX_RING_SIZE
- 1)
884 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TDLE
);
886 mdp
->stats
.tx_packets
++;
887 mdp
->stats
.tx_bytes
+= txdesc
->buffer_length
;
892 /* Packet receive function */
893 static int sh_eth_rx(struct net_device
*ndev
)
895 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
896 struct sh_eth_rxdesc
*rxdesc
;
898 int entry
= mdp
->cur_rx
% RX_RING_SIZE
;
899 int boguscnt
= (mdp
->dirty_rx
+ RX_RING_SIZE
) - mdp
->cur_rx
;
904 rxdesc
= &mdp
->rx_ring
[entry
];
905 while (!(rxdesc
->status
& cpu_to_edmac(mdp
, RD_RACT
))) {
906 desc_status
= edmac_to_cpu(mdp
, rxdesc
->status
);
907 pkt_len
= rxdesc
->frame_length
;
912 if (!(desc_status
& RDFEND
))
913 mdp
->stats
.rx_length_errors
++;
915 if (desc_status
& (RD_RFS1
| RD_RFS2
| RD_RFS3
| RD_RFS4
|
916 RD_RFS5
| RD_RFS6
| RD_RFS10
)) {
917 mdp
->stats
.rx_errors
++;
918 if (desc_status
& RD_RFS1
)
919 mdp
->stats
.rx_crc_errors
++;
920 if (desc_status
& RD_RFS2
)
921 mdp
->stats
.rx_frame_errors
++;
922 if (desc_status
& RD_RFS3
)
923 mdp
->stats
.rx_length_errors
++;
924 if (desc_status
& RD_RFS4
)
925 mdp
->stats
.rx_length_errors
++;
926 if (desc_status
& RD_RFS6
)
927 mdp
->stats
.rx_missed_errors
++;
928 if (desc_status
& RD_RFS10
)
929 mdp
->stats
.rx_over_errors
++;
931 if (!mdp
->cd
->hw_swap
)
933 phys_to_virt(ALIGN(rxdesc
->addr
, 4)),
935 skb
= mdp
->rx_skbuff
[entry
];
936 mdp
->rx_skbuff
[entry
] = NULL
;
938 skb_reserve(skb
, NET_IP_ALIGN
);
939 skb_put(skb
, pkt_len
);
940 skb
->protocol
= eth_type_trans(skb
, ndev
);
942 mdp
->stats
.rx_packets
++;
943 mdp
->stats
.rx_bytes
+= pkt_len
;
945 rxdesc
->status
|= cpu_to_edmac(mdp
, RD_RACT
);
946 entry
= (++mdp
->cur_rx
) % RX_RING_SIZE
;
947 rxdesc
= &mdp
->rx_ring
[entry
];
950 /* Refill the Rx ring buffers. */
951 for (; mdp
->cur_rx
- mdp
->dirty_rx
> 0; mdp
->dirty_rx
++) {
952 entry
= mdp
->dirty_rx
% RX_RING_SIZE
;
953 rxdesc
= &mdp
->rx_ring
[entry
];
954 /* The size of the buffer is 16 byte boundary. */
955 rxdesc
->buffer_length
= ALIGN(mdp
->rx_buf_sz
, 16);
957 if (mdp
->rx_skbuff
[entry
] == NULL
) {
958 skb
= dev_alloc_skb(mdp
->rx_buf_sz
);
959 mdp
->rx_skbuff
[entry
] = skb
;
961 break; /* Better luck next round. */
962 dma_map_single(&ndev
->dev
, skb
->tail
, mdp
->rx_buf_sz
,
965 sh_eth_set_receive_align(skb
);
967 skb_checksum_none_assert(skb
);
968 rxdesc
->addr
= virt_to_phys(PTR_ALIGN(skb
->data
, 4));
970 if (entry
>= RX_RING_SIZE
- 1)
972 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
| RD_RDEL
);
975 cpu_to_edmac(mdp
, RD_RACT
| RD_RFP
);
978 /* Restart Rx engine if stopped. */
979 /* If we don't need to check status, don't. -KDU */
980 if (!(sh_eth_read(ndev
, EDRRR
) & EDRRR_R
))
981 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
986 static void sh_eth_rcv_snd_disable(struct net_device
*ndev
)
988 /* disable tx and rx */
989 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) &
990 ~(ECMR_RE
| ECMR_TE
), ECMR
);
993 static void sh_eth_rcv_snd_enable(struct net_device
*ndev
)
995 /* enable tx and rx */
996 sh_eth_write(ndev
, sh_eth_read(ndev
, ECMR
) |
997 (ECMR_RE
| ECMR_TE
), ECMR
);
1000 /* error control function */
1001 static void sh_eth_error(struct net_device
*ndev
, int intr_status
)
1003 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1008 if (intr_status
& EESR_ECI
) {
1009 felic_stat
= sh_eth_read(ndev
, ECSR
);
1010 sh_eth_write(ndev
, felic_stat
, ECSR
); /* clear int */
1011 if (felic_stat
& ECSR_ICD
)
1012 mdp
->stats
.tx_carrier_errors
++;
1013 if (felic_stat
& ECSR_LCHNG
) {
1015 if (mdp
->cd
->no_psr
|| mdp
->no_ether_link
) {
1016 if (mdp
->link
== PHY_DOWN
)
1019 link_stat
= PHY_ST_LINK
;
1021 link_stat
= (sh_eth_read(ndev
, PSR
));
1022 if (mdp
->ether_link_active_low
)
1023 link_stat
= ~link_stat
;
1025 if (!(link_stat
& PHY_ST_LINK
))
1026 sh_eth_rcv_snd_disable(ndev
);
1029 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) &
1030 ~DMAC_M_ECI
, EESIPR
);
1032 sh_eth_write(ndev
, sh_eth_read(ndev
, ECSR
),
1034 sh_eth_write(ndev
, sh_eth_read(ndev
, EESIPR
) |
1035 DMAC_M_ECI
, EESIPR
);
1036 /* enable tx and rx */
1037 sh_eth_rcv_snd_enable(ndev
);
1042 if (intr_status
& EESR_TWB
) {
1043 /* Write buck end. unused write back interrupt */
1044 if (intr_status
& EESR_TABT
) /* Transmit Abort int */
1045 mdp
->stats
.tx_aborted_errors
++;
1046 if (netif_msg_tx_err(mdp
))
1047 dev_err(&ndev
->dev
, "Transmit Abort\n");
1050 if (intr_status
& EESR_RABT
) {
1051 /* Receive Abort int */
1052 if (intr_status
& EESR_RFRMER
) {
1053 /* Receive Frame Overflow int */
1054 mdp
->stats
.rx_frame_errors
++;
1055 if (netif_msg_rx_err(mdp
))
1056 dev_err(&ndev
->dev
, "Receive Abort\n");
1060 if (intr_status
& EESR_TDE
) {
1061 /* Transmit Descriptor Empty int */
1062 mdp
->stats
.tx_fifo_errors
++;
1063 if (netif_msg_tx_err(mdp
))
1064 dev_err(&ndev
->dev
, "Transmit Descriptor Empty\n");
1067 if (intr_status
& EESR_TFE
) {
1068 /* FIFO under flow */
1069 mdp
->stats
.tx_fifo_errors
++;
1070 if (netif_msg_tx_err(mdp
))
1071 dev_err(&ndev
->dev
, "Transmit FIFO Under flow\n");
1074 if (intr_status
& EESR_RDE
) {
1075 /* Receive Descriptor Empty int */
1076 mdp
->stats
.rx_over_errors
++;
1078 if (sh_eth_read(ndev
, EDRRR
) ^ EDRRR_R
)
1079 sh_eth_write(ndev
, EDRRR_R
, EDRRR
);
1080 if (netif_msg_rx_err(mdp
))
1081 dev_err(&ndev
->dev
, "Receive Descriptor Empty\n");
1084 if (intr_status
& EESR_RFE
) {
1085 /* Receive FIFO Overflow int */
1086 mdp
->stats
.rx_fifo_errors
++;
1087 if (netif_msg_rx_err(mdp
))
1088 dev_err(&ndev
->dev
, "Receive FIFO Overflow\n");
1091 if (!mdp
->cd
->no_ade
&& (intr_status
& EESR_ADE
)) {
1093 mdp
->stats
.tx_fifo_errors
++;
1094 if (netif_msg_tx_err(mdp
))
1095 dev_err(&ndev
->dev
, "Address Error\n");
1098 mask
= EESR_TWB
| EESR_TABT
| EESR_ADE
| EESR_TDE
| EESR_TFE
;
1099 if (mdp
->cd
->no_ade
)
1101 if (intr_status
& mask
) {
1103 u32 edtrr
= sh_eth_read(ndev
, EDTRR
);
1105 dev_err(&ndev
->dev
, "TX error. status=%8.8x cur_tx=%8.8x ",
1106 intr_status
, mdp
->cur_tx
);
1107 dev_err(&ndev
->dev
, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1108 mdp
->dirty_tx
, (u32
) ndev
->state
, edtrr
);
1109 /* dirty buffer free */
1110 sh_eth_txfree(ndev
);
1113 if (edtrr
^ sh_eth_get_edtrr_trns(mdp
)) {
1115 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
1118 netif_wake_queue(ndev
);
1122 static irqreturn_t
sh_eth_interrupt(int irq
, void *netdev
)
1124 struct net_device
*ndev
= netdev
;
1125 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1126 struct sh_eth_cpu_data
*cd
= mdp
->cd
;
1127 irqreturn_t ret
= IRQ_NONE
;
1128 u32 intr_status
= 0;
1130 spin_lock(&mdp
->lock
);
1132 /* Get interrpt stat */
1133 intr_status
= sh_eth_read(ndev
, EESR
);
1134 /* Clear interrupt */
1135 if (intr_status
& (EESR_FRC
| EESR_RMAF
| EESR_RRF
|
1136 EESR_RTLF
| EESR_RTSF
| EESR_PRE
| EESR_CERF
|
1137 cd
->tx_check
| cd
->eesr_err_check
)) {
1138 sh_eth_write(ndev
, intr_status
, EESR
);
1143 if (intr_status
& (EESR_FRC
| /* Frame recv*/
1144 EESR_RMAF
| /* Multi cast address recv*/
1145 EESR_RRF
| /* Bit frame recv */
1146 EESR_RTLF
| /* Long frame recv*/
1147 EESR_RTSF
| /* short frame recv */
1148 EESR_PRE
| /* PHY-LSI recv error */
1149 EESR_CERF
)){ /* recv frame CRC error */
1154 if (intr_status
& cd
->tx_check
) {
1155 sh_eth_txfree(ndev
);
1156 netif_wake_queue(ndev
);
1159 if (intr_status
& cd
->eesr_err_check
)
1160 sh_eth_error(ndev
, intr_status
);
1163 spin_unlock(&mdp
->lock
);
1168 static void sh_eth_timer(unsigned long data
)
1170 struct net_device
*ndev
= (struct net_device
*)data
;
1171 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1173 mod_timer(&mdp
->timer
, jiffies
+ (10 * HZ
));
1176 /* PHY state control function */
1177 static void sh_eth_adjust_link(struct net_device
*ndev
)
1179 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1180 struct phy_device
*phydev
= mdp
->phydev
;
1183 if (phydev
->link
!= PHY_DOWN
) {
1184 if (phydev
->duplex
!= mdp
->duplex
) {
1186 mdp
->duplex
= phydev
->duplex
;
1187 if (mdp
->cd
->set_duplex
)
1188 mdp
->cd
->set_duplex(ndev
);
1191 if (phydev
->speed
!= mdp
->speed
) {
1193 mdp
->speed
= phydev
->speed
;
1194 if (mdp
->cd
->set_rate
)
1195 mdp
->cd
->set_rate(ndev
);
1197 if (mdp
->link
== PHY_DOWN
) {
1199 (sh_eth_read(ndev
, ECMR
) & ~ECMR_TXF
), ECMR
);
1201 mdp
->link
= phydev
->link
;
1203 } else if (mdp
->link
) {
1205 mdp
->link
= PHY_DOWN
;
1210 if (new_state
&& netif_msg_link(mdp
))
1211 phy_print_status(phydev
);
1214 /* PHY init function */
1215 static int sh_eth_phy_init(struct net_device
*ndev
)
1217 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1218 char phy_id
[MII_BUS_ID_SIZE
+ 3];
1219 struct phy_device
*phydev
= NULL
;
1221 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
,
1222 mdp
->mii_bus
->id
, mdp
->phy_id
);
1224 mdp
->link
= PHY_DOWN
;
1228 /* Try connect to PHY */
1229 phydev
= phy_connect(ndev
, phy_id
, sh_eth_adjust_link
,
1230 0, mdp
->phy_interface
);
1231 if (IS_ERR(phydev
)) {
1232 dev_err(&ndev
->dev
, "phy_connect failed\n");
1233 return PTR_ERR(phydev
);
1236 dev_info(&ndev
->dev
, "attached phy %i to driver %s\n",
1237 phydev
->addr
, phydev
->drv
->name
);
1239 mdp
->phydev
= phydev
;
1244 /* PHY control start function */
1245 static int sh_eth_phy_start(struct net_device
*ndev
)
1247 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1250 ret
= sh_eth_phy_init(ndev
);
1254 /* reset phy - this also wakes it from PDOWN */
1255 phy_write(mdp
->phydev
, MII_BMCR
, BMCR_RESET
);
1256 phy_start(mdp
->phydev
);
1261 static int sh_eth_get_settings(struct net_device
*ndev
,
1262 struct ethtool_cmd
*ecmd
)
1264 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1265 unsigned long flags
;
1268 spin_lock_irqsave(&mdp
->lock
, flags
);
1269 ret
= phy_ethtool_gset(mdp
->phydev
, ecmd
);
1270 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1275 static int sh_eth_set_settings(struct net_device
*ndev
,
1276 struct ethtool_cmd
*ecmd
)
1278 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1279 unsigned long flags
;
1282 spin_lock_irqsave(&mdp
->lock
, flags
);
1284 /* disable tx and rx */
1285 sh_eth_rcv_snd_disable(ndev
);
1287 ret
= phy_ethtool_sset(mdp
->phydev
, ecmd
);
1291 if (ecmd
->duplex
== DUPLEX_FULL
)
1296 if (mdp
->cd
->set_duplex
)
1297 mdp
->cd
->set_duplex(ndev
);
1302 /* enable tx and rx */
1303 sh_eth_rcv_snd_enable(ndev
);
1305 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1310 static int sh_eth_nway_reset(struct net_device
*ndev
)
1312 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1313 unsigned long flags
;
1316 spin_lock_irqsave(&mdp
->lock
, flags
);
1317 ret
= phy_start_aneg(mdp
->phydev
);
1318 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1323 static u32
sh_eth_get_msglevel(struct net_device
*ndev
)
1325 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1326 return mdp
->msg_enable
;
1329 static void sh_eth_set_msglevel(struct net_device
*ndev
, u32 value
)
1331 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1332 mdp
->msg_enable
= value
;
1335 static const char sh_eth_gstrings_stats
[][ETH_GSTRING_LEN
] = {
1336 "rx_current", "tx_current",
1337 "rx_dirty", "tx_dirty",
1339 #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
1341 static int sh_eth_get_sset_count(struct net_device
*netdev
, int sset
)
1345 return SH_ETH_STATS_LEN
;
1351 static void sh_eth_get_ethtool_stats(struct net_device
*ndev
,
1352 struct ethtool_stats
*stats
, u64
*data
)
1354 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1357 /* device-specific stats */
1358 data
[i
++] = mdp
->cur_rx
;
1359 data
[i
++] = mdp
->cur_tx
;
1360 data
[i
++] = mdp
->dirty_rx
;
1361 data
[i
++] = mdp
->dirty_tx
;
1364 static void sh_eth_get_strings(struct net_device
*ndev
, u32 stringset
, u8
*data
)
1366 switch (stringset
) {
1368 memcpy(data
, *sh_eth_gstrings_stats
,
1369 sizeof(sh_eth_gstrings_stats
));
1374 static const struct ethtool_ops sh_eth_ethtool_ops
= {
1375 .get_settings
= sh_eth_get_settings
,
1376 .set_settings
= sh_eth_set_settings
,
1377 .nway_reset
= sh_eth_nway_reset
,
1378 .get_msglevel
= sh_eth_get_msglevel
,
1379 .set_msglevel
= sh_eth_set_msglevel
,
1380 .get_link
= ethtool_op_get_link
,
1381 .get_strings
= sh_eth_get_strings
,
1382 .get_ethtool_stats
= sh_eth_get_ethtool_stats
,
1383 .get_sset_count
= sh_eth_get_sset_count
,
1386 /* network device open function */
1387 static int sh_eth_open(struct net_device
*ndev
)
1390 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1392 pm_runtime_get_sync(&mdp
->pdev
->dev
);
1394 ret
= request_irq(ndev
->irq
, sh_eth_interrupt
,
1395 #if defined(CONFIG_CPU_SUBTYPE_SH7763) || \
1396 defined(CONFIG_CPU_SUBTYPE_SH7764) || \
1397 defined(CONFIG_CPU_SUBTYPE_SH7757)
1404 dev_err(&ndev
->dev
, "Can not assign IRQ number\n");
1408 /* Descriptor set */
1409 ret
= sh_eth_ring_init(ndev
);
1414 ret
= sh_eth_dev_init(ndev
);
1418 /* PHY control start*/
1419 ret
= sh_eth_phy_start(ndev
);
1423 /* Set the timer to check for link beat. */
1424 init_timer(&mdp
->timer
);
1425 mdp
->timer
.expires
= (jiffies
+ (24 * HZ
)) / 10;/* 2.4 sec. */
1426 setup_timer(&mdp
->timer
, sh_eth_timer
, (unsigned long)ndev
);
1431 free_irq(ndev
->irq
, ndev
);
1432 pm_runtime_put_sync(&mdp
->pdev
->dev
);
1436 /* Timeout function */
1437 static void sh_eth_tx_timeout(struct net_device
*ndev
)
1439 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1440 struct sh_eth_rxdesc
*rxdesc
;
1443 netif_stop_queue(ndev
);
1445 if (netif_msg_timer(mdp
))
1446 dev_err(&ndev
->dev
, "%s: transmit timed out, status %8.8x,"
1447 " resetting...\n", ndev
->name
, (int)sh_eth_read(ndev
, EESR
));
1449 /* tx_errors count up */
1450 mdp
->stats
.tx_errors
++;
1453 del_timer_sync(&mdp
->timer
);
1455 /* Free all the skbuffs in the Rx queue. */
1456 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1457 rxdesc
= &mdp
->rx_ring
[i
];
1459 rxdesc
->addr
= 0xBADF00D0;
1460 if (mdp
->rx_skbuff
[i
])
1461 dev_kfree_skb(mdp
->rx_skbuff
[i
]);
1462 mdp
->rx_skbuff
[i
] = NULL
;
1464 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1465 if (mdp
->tx_skbuff
[i
])
1466 dev_kfree_skb(mdp
->tx_skbuff
[i
]);
1467 mdp
->tx_skbuff
[i
] = NULL
;
1471 sh_eth_dev_init(ndev
);
1474 mdp
->timer
.expires
= (jiffies
+ (24 * HZ
)) / 10;/* 2.4 sec. */
1475 add_timer(&mdp
->timer
);
1478 /* Packet transmit function */
1479 static int sh_eth_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1481 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1482 struct sh_eth_txdesc
*txdesc
;
1484 unsigned long flags
;
1486 spin_lock_irqsave(&mdp
->lock
, flags
);
1487 if ((mdp
->cur_tx
- mdp
->dirty_tx
) >= (TX_RING_SIZE
- 4)) {
1488 if (!sh_eth_txfree(ndev
)) {
1489 if (netif_msg_tx_queued(mdp
))
1490 dev_warn(&ndev
->dev
, "TxFD exhausted.\n");
1491 netif_stop_queue(ndev
);
1492 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1493 return NETDEV_TX_BUSY
;
1496 spin_unlock_irqrestore(&mdp
->lock
, flags
);
1498 entry
= mdp
->cur_tx
% TX_RING_SIZE
;
1499 mdp
->tx_skbuff
[entry
] = skb
;
1500 txdesc
= &mdp
->tx_ring
[entry
];
1502 if (!mdp
->cd
->hw_swap
)
1503 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc
->addr
, 4)),
1505 txdesc
->addr
= dma_map_single(&ndev
->dev
, skb
->data
, skb
->len
,
1507 if (skb
->len
< ETHERSMALL
)
1508 txdesc
->buffer_length
= ETHERSMALL
;
1510 txdesc
->buffer_length
= skb
->len
;
1512 if (entry
>= TX_RING_SIZE
- 1)
1513 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
| TD_TDLE
);
1515 txdesc
->status
|= cpu_to_edmac(mdp
, TD_TACT
);
1519 if (!(sh_eth_read(ndev
, EDTRR
) & sh_eth_get_edtrr_trns(mdp
)))
1520 sh_eth_write(ndev
, sh_eth_get_edtrr_trns(mdp
), EDTRR
);
1522 return NETDEV_TX_OK
;
1525 /* device close function */
1526 static int sh_eth_close(struct net_device
*ndev
)
1528 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1531 netif_stop_queue(ndev
);
1533 /* Disable interrupts by clearing the interrupt mask. */
1534 sh_eth_write(ndev
, 0x0000, EESIPR
);
1536 /* Stop the chip's Tx and Rx processes. */
1537 sh_eth_write(ndev
, 0, EDTRR
);
1538 sh_eth_write(ndev
, 0, EDRRR
);
1540 /* PHY Disconnect */
1542 phy_stop(mdp
->phydev
);
1543 phy_disconnect(mdp
->phydev
);
1546 free_irq(ndev
->irq
, ndev
);
1548 del_timer_sync(&mdp
->timer
);
1550 /* Free all the skbuffs in the Rx queue. */
1551 sh_eth_ring_free(ndev
);
1553 /* free DMA buffer */
1554 ringsize
= sizeof(struct sh_eth_rxdesc
) * RX_RING_SIZE
;
1555 dma_free_coherent(NULL
, ringsize
, mdp
->rx_ring
, mdp
->rx_desc_dma
);
1557 /* free DMA buffer */
1558 ringsize
= sizeof(struct sh_eth_txdesc
) * TX_RING_SIZE
;
1559 dma_free_coherent(NULL
, ringsize
, mdp
->tx_ring
, mdp
->tx_desc_dma
);
1561 pm_runtime_put_sync(&mdp
->pdev
->dev
);
1566 static struct net_device_stats
*sh_eth_get_stats(struct net_device
*ndev
)
1568 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1570 pm_runtime_get_sync(&mdp
->pdev
->dev
);
1572 mdp
->stats
.tx_dropped
+= sh_eth_read(ndev
, TROCR
);
1573 sh_eth_write(ndev
, 0, TROCR
); /* (write clear) */
1574 mdp
->stats
.collisions
+= sh_eth_read(ndev
, CDCR
);
1575 sh_eth_write(ndev
, 0, CDCR
); /* (write clear) */
1576 mdp
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, LCCR
);
1577 sh_eth_write(ndev
, 0, LCCR
); /* (write clear) */
1578 if (sh_eth_is_gether(mdp
)) {
1579 mdp
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CERCR
);
1580 sh_eth_write(ndev
, 0, CERCR
); /* (write clear) */
1581 mdp
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CEECR
);
1582 sh_eth_write(ndev
, 0, CEECR
); /* (write clear) */
1584 mdp
->stats
.tx_carrier_errors
+= sh_eth_read(ndev
, CNDCR
);
1585 sh_eth_write(ndev
, 0, CNDCR
); /* (write clear) */
1587 pm_runtime_put_sync(&mdp
->pdev
->dev
);
1592 /* ioctl to device funciotn*/
1593 static int sh_eth_do_ioctl(struct net_device
*ndev
, struct ifreq
*rq
,
1596 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1597 struct phy_device
*phydev
= mdp
->phydev
;
1599 if (!netif_running(ndev
))
1605 return phy_mii_ioctl(phydev
, rq
, cmd
);
1608 #if defined(SH_ETH_HAS_TSU)
1609 /* Multicast reception directions set */
1610 static void sh_eth_set_multicast_list(struct net_device
*ndev
)
1612 if (ndev
->flags
& IFF_PROMISC
) {
1613 /* Set promiscuous. */
1614 sh_eth_write(ndev
, (sh_eth_read(ndev
, ECMR
) & ~ECMR_MCT
) |
1617 /* Normal, unicast/broadcast-only mode. */
1618 sh_eth_write(ndev
, (sh_eth_read(ndev
, ECMR
) & ~ECMR_PRM
) |
1622 #endif /* SH_ETH_HAS_TSU */
1624 /* SuperH's TSU register init function */
1625 static void sh_eth_tsu_init(struct sh_eth_private
*mdp
)
1627 sh_eth_tsu_write(mdp
, 0, TSU_FWEN0
); /* Disable forward(0->1) */
1628 sh_eth_tsu_write(mdp
, 0, TSU_FWEN1
); /* Disable forward(1->0) */
1629 sh_eth_tsu_write(mdp
, 0, TSU_FCM
); /* forward fifo 3k-3k */
1630 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL0
);
1631 sh_eth_tsu_write(mdp
, 0xc, TSU_BSYSL1
);
1632 sh_eth_tsu_write(mdp
, 0, TSU_PRISL0
);
1633 sh_eth_tsu_write(mdp
, 0, TSU_PRISL1
);
1634 sh_eth_tsu_write(mdp
, 0, TSU_FWSL0
);
1635 sh_eth_tsu_write(mdp
, 0, TSU_FWSL1
);
1636 sh_eth_tsu_write(mdp
, TSU_FWSLC_POSTENU
| TSU_FWSLC_POSTENL
, TSU_FWSLC
);
1637 if (sh_eth_is_gether(mdp
)) {
1638 sh_eth_tsu_write(mdp
, 0, TSU_QTAG0
); /* Disable QTAG(0->1) */
1639 sh_eth_tsu_write(mdp
, 0, TSU_QTAG1
); /* Disable QTAG(1->0) */
1641 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM0
); /* Disable QTAG(0->1) */
1642 sh_eth_tsu_write(mdp
, 0, TSU_QTAGM1
); /* Disable QTAG(1->0) */
1644 sh_eth_tsu_write(mdp
, 0, TSU_FWSR
); /* all interrupt status clear */
1645 sh_eth_tsu_write(mdp
, 0, TSU_FWINMK
); /* Disable all interrupt */
1646 sh_eth_tsu_write(mdp
, 0, TSU_TEN
); /* Disable all CAM entry */
1647 sh_eth_tsu_write(mdp
, 0, TSU_POST1
); /* Disable CAM entry [ 0- 7] */
1648 sh_eth_tsu_write(mdp
, 0, TSU_POST2
); /* Disable CAM entry [ 8-15] */
1649 sh_eth_tsu_write(mdp
, 0, TSU_POST3
); /* Disable CAM entry [16-23] */
1650 sh_eth_tsu_write(mdp
, 0, TSU_POST4
); /* Disable CAM entry [24-31] */
1653 /* MDIO bus release function */
1654 static int sh_mdio_release(struct net_device
*ndev
)
1656 struct mii_bus
*bus
= dev_get_drvdata(&ndev
->dev
);
1658 /* unregister mdio bus */
1659 mdiobus_unregister(bus
);
1661 /* remove mdio bus info from net_device */
1662 dev_set_drvdata(&ndev
->dev
, NULL
);
1664 /* free interrupts memory */
1667 /* free bitbang info */
1668 free_mdio_bitbang(bus
);
1673 /* MDIO bus init function */
1674 static int sh_mdio_init(struct net_device
*ndev
, int id
,
1675 struct sh_eth_plat_data
*pd
)
1678 struct bb_info
*bitbang
;
1679 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1681 /* create bit control struct for PHY */
1682 bitbang
= kzalloc(sizeof(struct bb_info
), GFP_KERNEL
);
1689 bitbang
->addr
= mdp
->addr
+ mdp
->reg_offset
[PIR
];
1690 bitbang
->set_gate
= pd
->set_mdio_gate
;
1691 bitbang
->mdi_msk
= 0x08;
1692 bitbang
->mdo_msk
= 0x04;
1693 bitbang
->mmd_msk
= 0x02;/* MMD */
1694 bitbang
->mdc_msk
= 0x01;
1695 bitbang
->ctrl
.ops
= &bb_ops
;
1697 /* MII controller setting */
1698 mdp
->mii_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
1699 if (!mdp
->mii_bus
) {
1701 goto out_free_bitbang
;
1704 /* Hook up MII support for ethtool */
1705 mdp
->mii_bus
->name
= "sh_mii";
1706 mdp
->mii_bus
->parent
= &ndev
->dev
;
1707 snprintf(mdp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1708 mdp
->pdev
->name
, id
);
1711 mdp
->mii_bus
->irq
= kmalloc(sizeof(int)*PHY_MAX_ADDR
, GFP_KERNEL
);
1712 if (!mdp
->mii_bus
->irq
) {
1717 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
1718 mdp
->mii_bus
->irq
[i
] = PHY_POLL
;
1720 /* regist mdio bus */
1721 ret
= mdiobus_register(mdp
->mii_bus
);
1725 dev_set_drvdata(&ndev
->dev
, mdp
->mii_bus
);
1730 kfree(mdp
->mii_bus
->irq
);
1733 free_mdio_bitbang(mdp
->mii_bus
);
1742 static const u16
*sh_eth_get_register_offset(int register_type
)
1744 const u16
*reg_offset
= NULL
;
1746 switch (register_type
) {
1747 case SH_ETH_REG_GIGABIT
:
1748 reg_offset
= sh_eth_offset_gigabit
;
1750 case SH_ETH_REG_FAST_SH4
:
1751 reg_offset
= sh_eth_offset_fast_sh4
;
1753 case SH_ETH_REG_FAST_SH3_SH2
:
1754 reg_offset
= sh_eth_offset_fast_sh3_sh2
;
1757 printk(KERN_ERR
"Unknown register type (%d)\n", register_type
);
1764 static const struct net_device_ops sh_eth_netdev_ops
= {
1765 .ndo_open
= sh_eth_open
,
1766 .ndo_stop
= sh_eth_close
,
1767 .ndo_start_xmit
= sh_eth_start_xmit
,
1768 .ndo_get_stats
= sh_eth_get_stats
,
1769 #if defined(SH_ETH_HAS_TSU)
1770 .ndo_set_rx_mode
= sh_eth_set_multicast_list
,
1772 .ndo_tx_timeout
= sh_eth_tx_timeout
,
1773 .ndo_do_ioctl
= sh_eth_do_ioctl
,
1774 .ndo_validate_addr
= eth_validate_addr
,
1775 .ndo_set_mac_address
= eth_mac_addr
,
1776 .ndo_change_mtu
= eth_change_mtu
,
1779 static int sh_eth_drv_probe(struct platform_device
*pdev
)
1782 struct resource
*res
;
1783 struct net_device
*ndev
= NULL
;
1784 struct sh_eth_private
*mdp
= NULL
;
1785 struct sh_eth_plat_data
*pd
;
1788 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1789 if (unlikely(res
== NULL
)) {
1790 dev_err(&pdev
->dev
, "invalid resource\n");
1795 ndev
= alloc_etherdev(sizeof(struct sh_eth_private
));
1797 dev_err(&pdev
->dev
, "Could not allocate device.\n");
1802 /* The sh Ether-specific entries in the device structure. */
1803 ndev
->base_addr
= res
->start
;
1809 ret
= platform_get_irq(pdev
, 0);
1816 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1818 /* Fill in the fields of the device structure with ethernet values. */
1821 mdp
= netdev_priv(ndev
);
1822 mdp
->addr
= ioremap(res
->start
, resource_size(res
));
1823 if (mdp
->addr
== NULL
) {
1825 dev_err(&pdev
->dev
, "ioremap failed.\n");
1829 spin_lock_init(&mdp
->lock
);
1831 pm_runtime_enable(&pdev
->dev
);
1832 pm_runtime_resume(&pdev
->dev
);
1834 pd
= (struct sh_eth_plat_data
*)(pdev
->dev
.platform_data
);
1836 mdp
->phy_id
= pd
->phy
;
1837 mdp
->phy_interface
= pd
->phy_interface
;
1839 mdp
->edmac_endian
= pd
->edmac_endian
;
1840 mdp
->no_ether_link
= pd
->no_ether_link
;
1841 mdp
->ether_link_active_low
= pd
->ether_link_active_low
;
1842 mdp
->reg_offset
= sh_eth_get_register_offset(pd
->register_type
);
1845 #if defined(SH_ETH_HAS_BOTH_MODULES)
1846 mdp
->cd
= sh_eth_get_cpu_data(mdp
);
1848 mdp
->cd
= &sh_eth_my_cpu_data
;
1850 sh_eth_set_default_cpu_data(mdp
->cd
);
1853 ndev
->netdev_ops
= &sh_eth_netdev_ops
;
1854 SET_ETHTOOL_OPS(ndev
, &sh_eth_ethtool_ops
);
1855 ndev
->watchdog_timeo
= TX_TIMEOUT
;
1857 /* debug message level */
1858 mdp
->msg_enable
= SH_ETH_DEF_MSG_ENABLE
;
1859 mdp
->post_rx
= POST_RX
>> (devno
<< 1);
1860 mdp
->post_fw
= POST_FW
>> (devno
<< 1);
1862 /* read and set MAC address */
1863 read_mac_address(ndev
, pd
->mac_addr
);
1865 /* First device only init */
1868 struct resource
*rtsu
;
1869 rtsu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1871 dev_err(&pdev
->dev
, "Not found TSU resource\n");
1874 mdp
->tsu_addr
= ioremap(rtsu
->start
,
1875 resource_size(rtsu
));
1877 if (mdp
->cd
->chip_reset
)
1878 mdp
->cd
->chip_reset(ndev
);
1881 /* TSU init (Init only)*/
1882 sh_eth_tsu_init(mdp
);
1886 /* network device register */
1887 ret
= register_netdev(ndev
);
1892 ret
= sh_mdio_init(ndev
, pdev
->id
, pd
);
1894 goto out_unregister
;
1896 /* print device information */
1897 pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
1898 (u32
)ndev
->base_addr
, ndev
->dev_addr
, ndev
->irq
);
1900 platform_set_drvdata(pdev
, ndev
);
1905 unregister_netdev(ndev
);
1909 if (mdp
&& mdp
->addr
)
1911 if (mdp
&& mdp
->tsu_addr
)
1912 iounmap(mdp
->tsu_addr
);
1920 static int sh_eth_drv_remove(struct platform_device
*pdev
)
1922 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1923 struct sh_eth_private
*mdp
= netdev_priv(ndev
);
1925 iounmap(mdp
->tsu_addr
);
1926 sh_mdio_release(ndev
);
1927 unregister_netdev(ndev
);
1928 pm_runtime_disable(&pdev
->dev
);
1931 platform_set_drvdata(pdev
, NULL
);
1936 static int sh_eth_runtime_nop(struct device
*dev
)
1939 * Runtime PM callback shared between ->runtime_suspend()
1940 * and ->runtime_resume(). Simply returns success.
1942 * This driver re-initializes all registers after
1943 * pm_runtime_get_sync() anyway so there is no need
1944 * to save and restore registers here.
1949 static struct dev_pm_ops sh_eth_dev_pm_ops
= {
1950 .runtime_suspend
= sh_eth_runtime_nop
,
1951 .runtime_resume
= sh_eth_runtime_nop
,
1954 static struct platform_driver sh_eth_driver
= {
1955 .probe
= sh_eth_drv_probe
,
1956 .remove
= sh_eth_drv_remove
,
1959 .pm
= &sh_eth_dev_pm_ops
,
1963 module_platform_driver(sh_eth_driver
);
1965 MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
1966 MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
1967 MODULE_LICENSE("GPL v2");