2 * Broadcom BCM7xxx System Port Ethernet MAC driver
4 * Copyright (C) 2014 Broadcom Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/platform_device.h>
21 #include <linux/of_net.h>
22 #include <linux/of_mdio.h>
23 #include <linux/phy.h>
24 #include <linux/phy_fixed.h>
28 #include "bcmsysport.h"
30 /* I/O accessors register helpers */
31 #define BCM_SYSPORT_IO_MACRO(name, offset) \
32 static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off) \
34 u32 reg = __raw_readl(priv->base + offset + off); \
37 static inline void name##_writel(struct bcm_sysport_priv *priv, \
40 __raw_writel(val, priv->base + offset + off); \
43 BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
44 BCM_SYSPORT_IO_MACRO(intrl2_1
, SYS_PORT_INTRL2_1_OFFSET
);
45 BCM_SYSPORT_IO_MACRO(umac
, SYS_PORT_UMAC_OFFSET
);
46 BCM_SYSPORT_IO_MACRO(gib
, SYS_PORT_GIB_OFFSET
);
47 BCM_SYSPORT_IO_MACRO(tdma
, SYS_PORT_TDMA_OFFSET
);
48 BCM_SYSPORT_IO_MACRO(rxchk
, SYS_PORT_RXCHK_OFFSET
);
49 BCM_SYSPORT_IO_MACRO(txchk
, SYS_PORT_TXCHK_OFFSET
);
50 BCM_SYSPORT_IO_MACRO(rbuf
, SYS_PORT_RBUF_OFFSET
);
51 BCM_SYSPORT_IO_MACRO(tbuf
, SYS_PORT_TBUF_OFFSET
);
52 BCM_SYSPORT_IO_MACRO(topctrl
, SYS_PORT_TOPCTRL_OFFSET
);
54 /* On SYSTEMPORT Lite, any register after RDMA_STATUS has the exact
55 * same layout, except it has been moved by 4 bytes up, *sigh*
57 static inline u32
rdma_readl(struct bcm_sysport_priv
*priv
, u32 off
)
59 if (priv
->is_lite
&& off
>= RDMA_STATUS
)
61 return __raw_readl(priv
->base
+ SYS_PORT_RDMA_OFFSET
+ off
);
64 static inline void rdma_writel(struct bcm_sysport_priv
*priv
, u32 val
, u32 off
)
66 if (priv
->is_lite
&& off
>= RDMA_STATUS
)
68 __raw_writel(val
, priv
->base
+ SYS_PORT_RDMA_OFFSET
+ off
);
71 static inline u32
tdma_control_bit(struct bcm_sysport_priv
*priv
, u32 bit
)
83 /* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
84 * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
86 #define BCM_SYSPORT_INTR_L2(which) \
87 static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
90 priv->irq##which##_mask &= ~(mask); \
91 intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
93 static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
96 intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
97 priv->irq##which##_mask |= (mask); \
100 BCM_SYSPORT_INTR_L2(0)
101 BCM_SYSPORT_INTR_L2(1)
103 /* Register accesses to GISB/RBUS registers are expensive (few hundred
104 * nanoseconds), so keep the check for 64-bits explicit here to save
105 * one register write per-packet on 32-bits platforms.
107 static inline void dma_desc_set_addr(struct bcm_sysport_priv
*priv
,
111 #ifdef CONFIG_PHYS_ADDR_T_64BIT
112 __raw_writel(upper_32_bits(addr
) & DESC_ADDR_HI_MASK
,
113 d
+ DESC_ADDR_HI_STATUS_LEN
);
115 __raw_writel(lower_32_bits(addr
), d
+ DESC_ADDR_LO
);
118 static inline void tdma_port_write_desc_addr(struct bcm_sysport_priv
*priv
,
119 struct dma_desc
*desc
,
122 /* Ports are latched, so write upper address first */
123 tdma_writel(priv
, desc
->addr_status_len
, TDMA_WRITE_PORT_HI(port
));
124 tdma_writel(priv
, desc
->addr_lo
, TDMA_WRITE_PORT_LO(port
));
127 /* Ethtool operations */
128 static int bcm_sysport_set_rx_csum(struct net_device
*dev
,
129 netdev_features_t wanted
)
131 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
134 priv
->rx_chk_en
= !!(wanted
& NETIF_F_RXCSUM
);
135 reg
= rxchk_readl(priv
, RXCHK_CONTROL
);
141 /* If UniMAC forwards CRC, we need to skip over it to get
142 * a valid CHK bit to be set in the per-packet status word
144 if (priv
->rx_chk_en
&& priv
->crc_fwd
)
145 reg
|= RXCHK_SKIP_FCS
;
147 reg
&= ~RXCHK_SKIP_FCS
;
149 /* If Broadcom tags are enabled (e.g: using a switch), make
150 * sure we tell the RXCHK hardware to expect a 4-bytes Broadcom
151 * tag after the Ethernet MAC Source Address.
153 if (netdev_uses_dsa(dev
))
154 reg
|= RXCHK_BRCM_TAG_EN
;
156 reg
&= ~RXCHK_BRCM_TAG_EN
;
158 rxchk_writel(priv
, reg
, RXCHK_CONTROL
);
163 static int bcm_sysport_set_tx_csum(struct net_device
*dev
,
164 netdev_features_t wanted
)
166 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
169 /* Hardware transmit checksum requires us to enable the Transmit status
170 * block prepended to the packet contents
172 priv
->tsb_en
= !!(wanted
& (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
));
173 reg
= tdma_readl(priv
, TDMA_CONTROL
);
175 reg
|= tdma_control_bit(priv
, TSB_EN
);
177 reg
&= ~tdma_control_bit(priv
, TSB_EN
);
178 tdma_writel(priv
, reg
, TDMA_CONTROL
);
183 static int bcm_sysport_set_features(struct net_device
*dev
,
184 netdev_features_t features
)
186 netdev_features_t changed
= features
^ dev
->features
;
187 netdev_features_t wanted
= dev
->wanted_features
;
190 if (changed
& NETIF_F_RXCSUM
)
191 ret
= bcm_sysport_set_rx_csum(dev
, wanted
);
192 if (changed
& (NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
))
193 ret
= bcm_sysport_set_tx_csum(dev
, wanted
);
198 /* Hardware counters must be kept in sync because the order/offset
199 * is important here (order in structure declaration = order in hardware)
201 static const struct bcm_sysport_stats bcm_sysport_gstrings_stats
[] = {
203 STAT_NETDEV(rx_packets
),
204 STAT_NETDEV(tx_packets
),
205 STAT_NETDEV(rx_bytes
),
206 STAT_NETDEV(tx_bytes
),
207 STAT_NETDEV(rx_errors
),
208 STAT_NETDEV(tx_errors
),
209 STAT_NETDEV(rx_dropped
),
210 STAT_NETDEV(tx_dropped
),
211 STAT_NETDEV(multicast
),
212 /* UniMAC RSV counters */
213 STAT_MIB_RX("rx_64_octets", mib
.rx
.pkt_cnt
.cnt_64
),
214 STAT_MIB_RX("rx_65_127_oct", mib
.rx
.pkt_cnt
.cnt_127
),
215 STAT_MIB_RX("rx_128_255_oct", mib
.rx
.pkt_cnt
.cnt_255
),
216 STAT_MIB_RX("rx_256_511_oct", mib
.rx
.pkt_cnt
.cnt_511
),
217 STAT_MIB_RX("rx_512_1023_oct", mib
.rx
.pkt_cnt
.cnt_1023
),
218 STAT_MIB_RX("rx_1024_1518_oct", mib
.rx
.pkt_cnt
.cnt_1518
),
219 STAT_MIB_RX("rx_vlan_1519_1522_oct", mib
.rx
.pkt_cnt
.cnt_mgv
),
220 STAT_MIB_RX("rx_1522_2047_oct", mib
.rx
.pkt_cnt
.cnt_2047
),
221 STAT_MIB_RX("rx_2048_4095_oct", mib
.rx
.pkt_cnt
.cnt_4095
),
222 STAT_MIB_RX("rx_4096_9216_oct", mib
.rx
.pkt_cnt
.cnt_9216
),
223 STAT_MIB_RX("rx_pkts", mib
.rx
.pkt
),
224 STAT_MIB_RX("rx_bytes", mib
.rx
.bytes
),
225 STAT_MIB_RX("rx_multicast", mib
.rx
.mca
),
226 STAT_MIB_RX("rx_broadcast", mib
.rx
.bca
),
227 STAT_MIB_RX("rx_fcs", mib
.rx
.fcs
),
228 STAT_MIB_RX("rx_control", mib
.rx
.cf
),
229 STAT_MIB_RX("rx_pause", mib
.rx
.pf
),
230 STAT_MIB_RX("rx_unknown", mib
.rx
.uo
),
231 STAT_MIB_RX("rx_align", mib
.rx
.aln
),
232 STAT_MIB_RX("rx_outrange", mib
.rx
.flr
),
233 STAT_MIB_RX("rx_code", mib
.rx
.cde
),
234 STAT_MIB_RX("rx_carrier", mib
.rx
.fcr
),
235 STAT_MIB_RX("rx_oversize", mib
.rx
.ovr
),
236 STAT_MIB_RX("rx_jabber", mib
.rx
.jbr
),
237 STAT_MIB_RX("rx_mtu_err", mib
.rx
.mtue
),
238 STAT_MIB_RX("rx_good_pkts", mib
.rx
.pok
),
239 STAT_MIB_RX("rx_unicast", mib
.rx
.uc
),
240 STAT_MIB_RX("rx_ppp", mib
.rx
.ppp
),
241 STAT_MIB_RX("rx_crc", mib
.rx
.rcrc
),
242 /* UniMAC TSV counters */
243 STAT_MIB_TX("tx_64_octets", mib
.tx
.pkt_cnt
.cnt_64
),
244 STAT_MIB_TX("tx_65_127_oct", mib
.tx
.pkt_cnt
.cnt_127
),
245 STAT_MIB_TX("tx_128_255_oct", mib
.tx
.pkt_cnt
.cnt_255
),
246 STAT_MIB_TX("tx_256_511_oct", mib
.tx
.pkt_cnt
.cnt_511
),
247 STAT_MIB_TX("tx_512_1023_oct", mib
.tx
.pkt_cnt
.cnt_1023
),
248 STAT_MIB_TX("tx_1024_1518_oct", mib
.tx
.pkt_cnt
.cnt_1518
),
249 STAT_MIB_TX("tx_vlan_1519_1522_oct", mib
.tx
.pkt_cnt
.cnt_mgv
),
250 STAT_MIB_TX("tx_1522_2047_oct", mib
.tx
.pkt_cnt
.cnt_2047
),
251 STAT_MIB_TX("tx_2048_4095_oct", mib
.tx
.pkt_cnt
.cnt_4095
),
252 STAT_MIB_TX("tx_4096_9216_oct", mib
.tx
.pkt_cnt
.cnt_9216
),
253 STAT_MIB_TX("tx_pkts", mib
.tx
.pkts
),
254 STAT_MIB_TX("tx_multicast", mib
.tx
.mca
),
255 STAT_MIB_TX("tx_broadcast", mib
.tx
.bca
),
256 STAT_MIB_TX("tx_pause", mib
.tx
.pf
),
257 STAT_MIB_TX("tx_control", mib
.tx
.cf
),
258 STAT_MIB_TX("tx_fcs_err", mib
.tx
.fcs
),
259 STAT_MIB_TX("tx_oversize", mib
.tx
.ovr
),
260 STAT_MIB_TX("tx_defer", mib
.tx
.drf
),
261 STAT_MIB_TX("tx_excess_defer", mib
.tx
.edf
),
262 STAT_MIB_TX("tx_single_col", mib
.tx
.scl
),
263 STAT_MIB_TX("tx_multi_col", mib
.tx
.mcl
),
264 STAT_MIB_TX("tx_late_col", mib
.tx
.lcl
),
265 STAT_MIB_TX("tx_excess_col", mib
.tx
.ecl
),
266 STAT_MIB_TX("tx_frags", mib
.tx
.frg
),
267 STAT_MIB_TX("tx_total_col", mib
.tx
.ncl
),
268 STAT_MIB_TX("tx_jabber", mib
.tx
.jbr
),
269 STAT_MIB_TX("tx_bytes", mib
.tx
.bytes
),
270 STAT_MIB_TX("tx_good_pkts", mib
.tx
.pok
),
271 STAT_MIB_TX("tx_unicast", mib
.tx
.uc
),
272 /* UniMAC RUNT counters */
273 STAT_RUNT("rx_runt_pkts", mib
.rx_runt_cnt
),
274 STAT_RUNT("rx_runt_valid_fcs", mib
.rx_runt_fcs
),
275 STAT_RUNT("rx_runt_inval_fcs_align", mib
.rx_runt_fcs_align
),
276 STAT_RUNT("rx_runt_bytes", mib
.rx_runt_bytes
),
277 /* RXCHK misc statistics */
278 STAT_RXCHK("rxchk_bad_csum", mib
.rxchk_bad_csum
, RXCHK_BAD_CSUM_CNTR
),
279 STAT_RXCHK("rxchk_other_pkt_disc", mib
.rxchk_other_pkt_disc
,
280 RXCHK_OTHER_DISC_CNTR
),
281 /* RBUF misc statistics */
282 STAT_RBUF("rbuf_ovflow_cnt", mib
.rbuf_ovflow_cnt
, RBUF_OVFL_DISC_CNTR
),
283 STAT_RBUF("rbuf_err_cnt", mib
.rbuf_err_cnt
, RBUF_ERR_PKT_CNTR
),
284 STAT_MIB_SOFT("alloc_rx_buff_failed", mib
.alloc_rx_buff_failed
),
285 STAT_MIB_SOFT("rx_dma_failed", mib
.rx_dma_failed
),
286 STAT_MIB_SOFT("tx_dma_failed", mib
.tx_dma_failed
),
289 #define BCM_SYSPORT_STATS_LEN ARRAY_SIZE(bcm_sysport_gstrings_stats)
291 static void bcm_sysport_get_drvinfo(struct net_device
*dev
,
292 struct ethtool_drvinfo
*info
)
294 strlcpy(info
->driver
, KBUILD_MODNAME
, sizeof(info
->driver
));
295 strlcpy(info
->version
, "0.1", sizeof(info
->version
));
296 strlcpy(info
->bus_info
, "platform", sizeof(info
->bus_info
));
299 static u32
bcm_sysport_get_msglvl(struct net_device
*dev
)
301 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
303 return priv
->msg_enable
;
306 static void bcm_sysport_set_msglvl(struct net_device
*dev
, u32 enable
)
308 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
310 priv
->msg_enable
= enable
;
313 static inline bool bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type
)
316 case BCM_SYSPORT_STAT_NETDEV
:
317 case BCM_SYSPORT_STAT_RXCHK
:
318 case BCM_SYSPORT_STAT_RBUF
:
319 case BCM_SYSPORT_STAT_SOFT
:
326 static int bcm_sysport_get_sset_count(struct net_device
*dev
, int string_set
)
328 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
329 const struct bcm_sysport_stats
*s
;
332 switch (string_set
) {
334 for (i
= 0, j
= 0; i
< BCM_SYSPORT_STATS_LEN
; i
++) {
335 s
= &bcm_sysport_gstrings_stats
[i
];
337 !bcm_sysport_lite_stat_valid(s
->type
))
347 static void bcm_sysport_get_strings(struct net_device
*dev
,
348 u32 stringset
, u8
*data
)
350 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
351 const struct bcm_sysport_stats
*s
;
356 for (i
= 0, j
= 0; i
< BCM_SYSPORT_STATS_LEN
; i
++) {
357 s
= &bcm_sysport_gstrings_stats
[i
];
359 !bcm_sysport_lite_stat_valid(s
->type
))
362 memcpy(data
+ j
* ETH_GSTRING_LEN
, s
->stat_string
,
372 static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv
*priv
)
376 for (i
= 0; i
< BCM_SYSPORT_STATS_LEN
; i
++) {
377 const struct bcm_sysport_stats
*s
;
382 s
= &bcm_sysport_gstrings_stats
[i
];
384 case BCM_SYSPORT_STAT_NETDEV
:
385 case BCM_SYSPORT_STAT_SOFT
:
387 case BCM_SYSPORT_STAT_MIB_RX
:
388 case BCM_SYSPORT_STAT_MIB_TX
:
389 case BCM_SYSPORT_STAT_RUNT
:
393 if (s
->type
!= BCM_SYSPORT_STAT_MIB_RX
)
394 offset
= UMAC_MIB_STAT_OFFSET
;
395 val
= umac_readl(priv
, UMAC_MIB_START
+ j
+ offset
);
397 case BCM_SYSPORT_STAT_RXCHK
:
398 val
= rxchk_readl(priv
, s
->reg_offset
);
400 rxchk_writel(priv
, 0, s
->reg_offset
);
402 case BCM_SYSPORT_STAT_RBUF
:
403 val
= rbuf_readl(priv
, s
->reg_offset
);
405 rbuf_writel(priv
, 0, s
->reg_offset
);
410 p
= (char *)priv
+ s
->stat_offset
;
414 netif_dbg(priv
, hw
, priv
->netdev
, "updated MIB counters\n");
417 static void bcm_sysport_get_stats(struct net_device
*dev
,
418 struct ethtool_stats
*stats
, u64
*data
)
420 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
423 if (netif_running(dev
))
424 bcm_sysport_update_mib_counters(priv
);
426 for (i
= 0, j
= 0; i
< BCM_SYSPORT_STATS_LEN
; i
++) {
427 const struct bcm_sysport_stats
*s
;
430 s
= &bcm_sysport_gstrings_stats
[i
];
431 if (s
->type
== BCM_SYSPORT_STAT_NETDEV
)
432 p
= (char *)&dev
->stats
;
436 data
[j
] = *(unsigned long *)p
;
441 static void bcm_sysport_get_wol(struct net_device
*dev
,
442 struct ethtool_wolinfo
*wol
)
444 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
447 wol
->supported
= WAKE_MAGIC
| WAKE_MAGICSECURE
;
448 wol
->wolopts
= priv
->wolopts
;
450 if (!(priv
->wolopts
& WAKE_MAGICSECURE
))
453 /* Return the programmed SecureOn password */
454 reg
= umac_readl(priv
, UMAC_PSW_MS
);
455 put_unaligned_be16(reg
, &wol
->sopass
[0]);
456 reg
= umac_readl(priv
, UMAC_PSW_LS
);
457 put_unaligned_be32(reg
, &wol
->sopass
[2]);
460 static int bcm_sysport_set_wol(struct net_device
*dev
,
461 struct ethtool_wolinfo
*wol
)
463 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
464 struct device
*kdev
= &priv
->pdev
->dev
;
465 u32 supported
= WAKE_MAGIC
| WAKE_MAGICSECURE
;
467 if (!device_can_wakeup(kdev
))
470 if (wol
->wolopts
& ~supported
)
473 /* Program the SecureOn password */
474 if (wol
->wolopts
& WAKE_MAGICSECURE
) {
475 umac_writel(priv
, get_unaligned_be16(&wol
->sopass
[0]),
477 umac_writel(priv
, get_unaligned_be32(&wol
->sopass
[2]),
481 /* Flag the device and relevant IRQ as wakeup capable */
483 device_set_wakeup_enable(kdev
, 1);
484 if (priv
->wol_irq_disabled
)
485 enable_irq_wake(priv
->wol_irq
);
486 priv
->wol_irq_disabled
= 0;
488 device_set_wakeup_enable(kdev
, 0);
489 /* Avoid unbalanced disable_irq_wake calls */
490 if (!priv
->wol_irq_disabled
)
491 disable_irq_wake(priv
->wol_irq
);
492 priv
->wol_irq_disabled
= 1;
495 priv
->wolopts
= wol
->wolopts
;
500 static int bcm_sysport_get_coalesce(struct net_device
*dev
,
501 struct ethtool_coalesce
*ec
)
503 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
506 reg
= tdma_readl(priv
, TDMA_DESC_RING_INTR_CONTROL(0));
508 ec
->tx_coalesce_usecs
= (reg
>> RING_TIMEOUT_SHIFT
) * 8192 / 1000;
509 ec
->tx_max_coalesced_frames
= reg
& RING_INTR_THRESH_MASK
;
511 reg
= rdma_readl(priv
, RDMA_MBDONE_INTR
);
513 ec
->rx_coalesce_usecs
= (reg
>> RDMA_TIMEOUT_SHIFT
) * 8192 / 1000;
514 ec
->rx_max_coalesced_frames
= reg
& RDMA_INTR_THRESH_MASK
;
519 static int bcm_sysport_set_coalesce(struct net_device
*dev
,
520 struct ethtool_coalesce
*ec
)
522 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
526 /* Base system clock is 125Mhz, DMA timeout is this reference clock
527 * divided by 1024, which yield roughly 8.192 us, our maximum value has
528 * to fit in the RING_TIMEOUT_MASK (16 bits).
530 if (ec
->tx_max_coalesced_frames
> RING_INTR_THRESH_MASK
||
531 ec
->tx_coalesce_usecs
> (RING_TIMEOUT_MASK
* 8) + 1 ||
532 ec
->rx_max_coalesced_frames
> RDMA_INTR_THRESH_MASK
||
533 ec
->rx_coalesce_usecs
> (RDMA_TIMEOUT_MASK
* 8) + 1)
536 if ((ec
->tx_coalesce_usecs
== 0 && ec
->tx_max_coalesced_frames
== 0) ||
537 (ec
->rx_coalesce_usecs
== 0 && ec
->rx_max_coalesced_frames
== 0))
540 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
541 reg
= tdma_readl(priv
, TDMA_DESC_RING_INTR_CONTROL(i
));
542 reg
&= ~(RING_INTR_THRESH_MASK
|
543 RING_TIMEOUT_MASK
<< RING_TIMEOUT_SHIFT
);
544 reg
|= ec
->tx_max_coalesced_frames
;
545 reg
|= DIV_ROUND_UP(ec
->tx_coalesce_usecs
* 1000, 8192) <<
547 tdma_writel(priv
, reg
, TDMA_DESC_RING_INTR_CONTROL(i
));
550 reg
= rdma_readl(priv
, RDMA_MBDONE_INTR
);
551 reg
&= ~(RDMA_INTR_THRESH_MASK
|
552 RDMA_TIMEOUT_MASK
<< RDMA_TIMEOUT_SHIFT
);
553 reg
|= ec
->rx_max_coalesced_frames
;
554 reg
|= DIV_ROUND_UP(ec
->rx_coalesce_usecs
* 1000, 8192) <<
556 rdma_writel(priv
, reg
, RDMA_MBDONE_INTR
);
561 static void bcm_sysport_free_cb(struct bcm_sysport_cb
*cb
)
563 dev_kfree_skb_any(cb
->skb
);
565 dma_unmap_addr_set(cb
, dma_addr
, 0);
568 static struct sk_buff
*bcm_sysport_rx_refill(struct bcm_sysport_priv
*priv
,
569 struct bcm_sysport_cb
*cb
)
571 struct device
*kdev
= &priv
->pdev
->dev
;
572 struct net_device
*ndev
= priv
->netdev
;
573 struct sk_buff
*skb
, *rx_skb
;
576 /* Allocate a new SKB for a new packet */
577 skb
= netdev_alloc_skb(priv
->netdev
, RX_BUF_LENGTH
);
579 priv
->mib
.alloc_rx_buff_failed
++;
580 netif_err(priv
, rx_err
, ndev
, "SKB alloc failed\n");
584 mapping
= dma_map_single(kdev
, skb
->data
,
585 RX_BUF_LENGTH
, DMA_FROM_DEVICE
);
586 if (dma_mapping_error(kdev
, mapping
)) {
587 priv
->mib
.rx_dma_failed
++;
588 dev_kfree_skb_any(skb
);
589 netif_err(priv
, rx_err
, ndev
, "DMA mapping failure\n");
593 /* Grab the current SKB on the ring */
596 dma_unmap_single(kdev
, dma_unmap_addr(cb
, dma_addr
),
597 RX_BUF_LENGTH
, DMA_FROM_DEVICE
);
599 /* Put the new SKB on the ring */
601 dma_unmap_addr_set(cb
, dma_addr
, mapping
);
602 dma_desc_set_addr(priv
, cb
->bd_addr
, mapping
);
604 netif_dbg(priv
, rx_status
, ndev
, "RX refill\n");
606 /* Return the current SKB to the caller */
610 static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv
*priv
)
612 struct bcm_sysport_cb
*cb
;
616 for (i
= 0; i
< priv
->num_rx_bds
; i
++) {
617 cb
= &priv
->rx_cbs
[i
];
618 skb
= bcm_sysport_rx_refill(priv
, cb
);
628 /* Poll the hardware for up to budget packets to process */
629 static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv
*priv
,
632 struct net_device
*ndev
= priv
->netdev
;
633 unsigned int processed
= 0, to_process
;
634 struct bcm_sysport_cb
*cb
;
636 unsigned int p_index
;
640 /* Determine how much we should process since last call, SYSTEMPORT Lite
641 * groups the producer and consumer indexes into the same 32-bit
642 * which we access using RDMA_CONS_INDEX
645 p_index
= rdma_readl(priv
, RDMA_PROD_INDEX
);
647 p_index
= rdma_readl(priv
, RDMA_CONS_INDEX
);
648 p_index
&= RDMA_PROD_INDEX_MASK
;
650 if (p_index
< priv
->rx_c_index
)
651 to_process
= (RDMA_CONS_INDEX_MASK
+ 1) -
652 priv
->rx_c_index
+ p_index
;
654 to_process
= p_index
- priv
->rx_c_index
;
656 netif_dbg(priv
, rx_status
, ndev
,
657 "p_index=%d rx_c_index=%d to_process=%d\n",
658 p_index
, priv
->rx_c_index
, to_process
);
660 while ((processed
< to_process
) && (processed
< budget
)) {
661 cb
= &priv
->rx_cbs
[priv
->rx_read_ptr
];
662 skb
= bcm_sysport_rx_refill(priv
, cb
);
665 /* We do not have a backing SKB, so we do not a corresponding
666 * DMA mapping for this incoming packet since
667 * bcm_sysport_rx_refill always either has both skb and mapping
670 if (unlikely(!skb
)) {
671 netif_err(priv
, rx_err
, ndev
, "out of memory!\n");
672 ndev
->stats
.rx_dropped
++;
673 ndev
->stats
.rx_errors
++;
677 /* Extract the Receive Status Block prepended */
678 rsb
= (struct bcm_rsb
*)skb
->data
;
679 len
= (rsb
->rx_status_len
>> DESC_LEN_SHIFT
) & DESC_LEN_MASK
;
680 status
= (rsb
->rx_status_len
>> DESC_STATUS_SHIFT
) &
683 netif_dbg(priv
, rx_status
, ndev
,
684 "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
685 p_index
, priv
->rx_c_index
, priv
->rx_read_ptr
,
688 if (unlikely(len
> RX_BUF_LENGTH
)) {
689 netif_err(priv
, rx_status
, ndev
, "oversized packet\n");
690 ndev
->stats
.rx_length_errors
++;
691 ndev
->stats
.rx_errors
++;
692 dev_kfree_skb_any(skb
);
696 if (unlikely(!(status
& DESC_EOP
) || !(status
& DESC_SOP
))) {
697 netif_err(priv
, rx_status
, ndev
, "fragmented packet!\n");
698 ndev
->stats
.rx_dropped
++;
699 ndev
->stats
.rx_errors
++;
700 dev_kfree_skb_any(skb
);
704 if (unlikely(status
& (RX_STATUS_ERR
| RX_STATUS_OVFLOW
))) {
705 netif_err(priv
, rx_err
, ndev
, "error packet\n");
706 if (status
& RX_STATUS_OVFLOW
)
707 ndev
->stats
.rx_over_errors
++;
708 ndev
->stats
.rx_dropped
++;
709 ndev
->stats
.rx_errors
++;
710 dev_kfree_skb_any(skb
);
716 /* Hardware validated our checksum */
717 if (likely(status
& DESC_L4_CSUM
))
718 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
720 /* Hardware pre-pends packets with 2bytes before Ethernet
721 * header plus we have the Receive Status Block, strip off all
722 * of this from the SKB.
724 skb_pull(skb
, sizeof(*rsb
) + 2);
725 len
-= (sizeof(*rsb
) + 2);
727 /* UniMAC may forward CRC */
729 skb_trim(skb
, len
- ETH_FCS_LEN
);
733 skb
->protocol
= eth_type_trans(skb
, ndev
);
734 ndev
->stats
.rx_packets
++;
735 ndev
->stats
.rx_bytes
+= len
;
737 napi_gro_receive(&priv
->napi
, skb
);
742 if (priv
->rx_read_ptr
== priv
->num_rx_bds
)
743 priv
->rx_read_ptr
= 0;
749 static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_priv
*priv
,
750 struct bcm_sysport_cb
*cb
,
751 unsigned int *bytes_compl
,
752 unsigned int *pkts_compl
)
754 struct device
*kdev
= &priv
->pdev
->dev
;
755 struct net_device
*ndev
= priv
->netdev
;
758 ndev
->stats
.tx_bytes
+= cb
->skb
->len
;
759 *bytes_compl
+= cb
->skb
->len
;
760 dma_unmap_single(kdev
, dma_unmap_addr(cb
, dma_addr
),
761 dma_unmap_len(cb
, dma_len
),
763 ndev
->stats
.tx_packets
++;
765 bcm_sysport_free_cb(cb
);
767 } else if (dma_unmap_addr(cb
, dma_addr
)) {
768 ndev
->stats
.tx_bytes
+= dma_unmap_len(cb
, dma_len
);
769 dma_unmap_page(kdev
, dma_unmap_addr(cb
, dma_addr
),
770 dma_unmap_len(cb
, dma_len
), DMA_TO_DEVICE
);
771 dma_unmap_addr_set(cb
, dma_addr
, 0);
775 /* Reclaim queued SKBs for transmission completion, lockless version */
776 static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv
*priv
,
777 struct bcm_sysport_tx_ring
*ring
)
779 struct net_device
*ndev
= priv
->netdev
;
780 unsigned int c_index
, last_c_index
, last_tx_cn
, num_tx_cbs
;
781 unsigned int pkts_compl
= 0, bytes_compl
= 0;
782 struct bcm_sysport_cb
*cb
;
785 /* Compute how many descriptors have been processed since last call */
786 hw_ind
= tdma_readl(priv
, TDMA_DESC_RING_PROD_CONS_INDEX(ring
->index
));
787 c_index
= (hw_ind
>> RING_CONS_INDEX_SHIFT
) & RING_CONS_INDEX_MASK
;
788 ring
->p_index
= (hw_ind
& RING_PROD_INDEX_MASK
);
790 last_c_index
= ring
->c_index
;
791 num_tx_cbs
= ring
->size
;
793 c_index
&= (num_tx_cbs
- 1);
795 if (c_index
>= last_c_index
)
796 last_tx_cn
= c_index
- last_c_index
;
798 last_tx_cn
= num_tx_cbs
- last_c_index
+ c_index
;
800 netif_dbg(priv
, tx_done
, ndev
,
801 "ring=%d c_index=%d last_tx_cn=%d last_c_index=%d\n",
802 ring
->index
, c_index
, last_tx_cn
, last_c_index
);
804 while (last_tx_cn
-- > 0) {
805 cb
= ring
->cbs
+ last_c_index
;
806 bcm_sysport_tx_reclaim_one(priv
, cb
, &bytes_compl
, &pkts_compl
);
810 last_c_index
&= (num_tx_cbs
- 1);
813 ring
->c_index
= c_index
;
815 netif_dbg(priv
, tx_done
, ndev
,
816 "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
817 ring
->index
, ring
->c_index
, pkts_compl
, bytes_compl
);
822 /* Locked version of the per-ring TX reclaim routine */
823 static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv
*priv
,
824 struct bcm_sysport_tx_ring
*ring
)
826 struct netdev_queue
*txq
;
827 unsigned int released
;
830 txq
= netdev_get_tx_queue(priv
->netdev
, ring
->index
);
832 spin_lock_irqsave(&ring
->lock
, flags
);
833 released
= __bcm_sysport_tx_reclaim(priv
, ring
);
835 netif_tx_wake_queue(txq
);
837 spin_unlock_irqrestore(&ring
->lock
, flags
);
842 /* Locked version of the per-ring TX reclaim, but does not wake the queue */
843 static void bcm_sysport_tx_clean(struct bcm_sysport_priv
*priv
,
844 struct bcm_sysport_tx_ring
*ring
)
848 spin_lock_irqsave(&ring
->lock
, flags
);
849 __bcm_sysport_tx_reclaim(priv
, ring
);
850 spin_unlock_irqrestore(&ring
->lock
, flags
);
853 static int bcm_sysport_tx_poll(struct napi_struct
*napi
, int budget
)
855 struct bcm_sysport_tx_ring
*ring
=
856 container_of(napi
, struct bcm_sysport_tx_ring
, napi
);
857 unsigned int work_done
= 0;
859 work_done
= bcm_sysport_tx_reclaim(ring
->priv
, ring
);
861 if (work_done
== 0) {
863 /* re-enable TX interrupt */
864 if (!ring
->priv
->is_lite
)
865 intrl2_1_mask_clear(ring
->priv
, BIT(ring
->index
));
867 intrl2_0_mask_clear(ring
->priv
, BIT(ring
->index
+
868 INTRL2_0_TDMA_MBDONE_SHIFT
));
876 static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv
*priv
)
880 for (q
= 0; q
< priv
->netdev
->num_tx_queues
; q
++)
881 bcm_sysport_tx_reclaim(priv
, &priv
->tx_rings
[q
]);
884 static int bcm_sysport_poll(struct napi_struct
*napi
, int budget
)
886 struct bcm_sysport_priv
*priv
=
887 container_of(napi
, struct bcm_sysport_priv
, napi
);
888 unsigned int work_done
= 0;
890 work_done
= bcm_sysport_desc_rx(priv
, budget
);
892 priv
->rx_c_index
+= work_done
;
893 priv
->rx_c_index
&= RDMA_CONS_INDEX_MASK
;
895 /* SYSTEMPORT Lite groups the producer/consumer index, producer is
896 * maintained by HW, but writes to it will be ignore while RDMA
900 rdma_writel(priv
, priv
->rx_c_index
, RDMA_CONS_INDEX
);
902 rdma_writel(priv
, priv
->rx_c_index
<< 16, RDMA_CONS_INDEX
);
904 if (work_done
< budget
) {
905 napi_complete_done(napi
, work_done
);
906 /* re-enable RX interrupts */
907 intrl2_0_mask_clear(priv
, INTRL2_0_RDMA_MBDONE
);
913 static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv
*priv
)
917 /* Stop monitoring MPD interrupt */
918 intrl2_0_mask_set(priv
, INTRL2_0_MPD
);
920 /* Clear the MagicPacket detection logic */
921 reg
= umac_readl(priv
, UMAC_MPD_CTRL
);
923 umac_writel(priv
, reg
, UMAC_MPD_CTRL
);
925 netif_dbg(priv
, wol
, priv
->netdev
, "resumed from WOL\n");
928 /* RX and misc interrupt routine */
929 static irqreturn_t
bcm_sysport_rx_isr(int irq
, void *dev_id
)
931 struct net_device
*dev
= dev_id
;
932 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
933 struct bcm_sysport_tx_ring
*txr
;
934 unsigned int ring
, ring_bit
;
936 priv
->irq0_stat
= intrl2_0_readl(priv
, INTRL2_CPU_STATUS
) &
937 ~intrl2_0_readl(priv
, INTRL2_CPU_MASK_STATUS
);
938 intrl2_0_writel(priv
, priv
->irq0_stat
, INTRL2_CPU_CLEAR
);
940 if (unlikely(priv
->irq0_stat
== 0)) {
941 netdev_warn(priv
->netdev
, "spurious RX interrupt\n");
945 if (priv
->irq0_stat
& INTRL2_0_RDMA_MBDONE
) {
946 if (likely(napi_schedule_prep(&priv
->napi
))) {
947 /* disable RX interrupts */
948 intrl2_0_mask_set(priv
, INTRL2_0_RDMA_MBDONE
);
949 __napi_schedule_irqoff(&priv
->napi
);
953 /* TX ring is full, perform a full reclaim since we do not know
954 * which one would trigger this interrupt
956 if (priv
->irq0_stat
& INTRL2_0_TX_RING_FULL
)
957 bcm_sysport_tx_reclaim_all(priv
);
959 if (priv
->irq0_stat
& INTRL2_0_MPD
) {
960 netdev_info(priv
->netdev
, "Wake-on-LAN interrupt!\n");
961 bcm_sysport_resume_from_wol(priv
);
967 for (ring
= 0; ring
< dev
->num_tx_queues
; ring
++) {
968 ring_bit
= BIT(ring
+ INTRL2_0_TDMA_MBDONE_SHIFT
);
969 if (!(priv
->irq0_stat
& ring_bit
))
972 txr
= &priv
->tx_rings
[ring
];
974 if (likely(napi_schedule_prep(&txr
->napi
))) {
975 intrl2_0_mask_set(priv
, ring_bit
);
976 __napi_schedule(&txr
->napi
);
983 /* TX interrupt service routine */
984 static irqreturn_t
bcm_sysport_tx_isr(int irq
, void *dev_id
)
986 struct net_device
*dev
= dev_id
;
987 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
988 struct bcm_sysport_tx_ring
*txr
;
991 priv
->irq1_stat
= intrl2_1_readl(priv
, INTRL2_CPU_STATUS
) &
992 ~intrl2_1_readl(priv
, INTRL2_CPU_MASK_STATUS
);
993 intrl2_1_writel(priv
, 0xffffffff, INTRL2_CPU_CLEAR
);
995 if (unlikely(priv
->irq1_stat
== 0)) {
996 netdev_warn(priv
->netdev
, "spurious TX interrupt\n");
1000 for (ring
= 0; ring
< dev
->num_tx_queues
; ring
++) {
1001 if (!(priv
->irq1_stat
& BIT(ring
)))
1004 txr
= &priv
->tx_rings
[ring
];
1006 if (likely(napi_schedule_prep(&txr
->napi
))) {
1007 intrl2_1_mask_set(priv
, BIT(ring
));
1008 __napi_schedule_irqoff(&txr
->napi
);
1015 static irqreturn_t
bcm_sysport_wol_isr(int irq
, void *dev_id
)
1017 struct bcm_sysport_priv
*priv
= dev_id
;
1019 pm_wakeup_event(&priv
->pdev
->dev
, 0);
1024 #ifdef CONFIG_NET_POLL_CONTROLLER
1025 static void bcm_sysport_poll_controller(struct net_device
*dev
)
1027 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1029 disable_irq(priv
->irq0
);
1030 bcm_sysport_rx_isr(priv
->irq0
, priv
);
1031 enable_irq(priv
->irq0
);
1033 if (!priv
->is_lite
) {
1034 disable_irq(priv
->irq1
);
1035 bcm_sysport_tx_isr(priv
->irq1
, priv
);
1036 enable_irq(priv
->irq1
);
1041 static struct sk_buff
*bcm_sysport_insert_tsb(struct sk_buff
*skb
,
1042 struct net_device
*dev
)
1044 struct sk_buff
*nskb
;
1045 struct bcm_tsb
*tsb
;
1051 /* Re-allocate SKB if needed */
1052 if (unlikely(skb_headroom(skb
) < sizeof(*tsb
))) {
1053 nskb
= skb_realloc_headroom(skb
, sizeof(*tsb
));
1056 dev
->stats
.tx_errors
++;
1057 dev
->stats
.tx_dropped
++;
1063 tsb
= (struct bcm_tsb
*)skb_push(skb
, sizeof(*tsb
));
1064 /* Zero-out TSB by default */
1065 memset(tsb
, 0, sizeof(*tsb
));
1067 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1068 ip_ver
= htons(skb
->protocol
);
1071 ip_proto
= ip_hdr(skb
)->protocol
;
1074 ip_proto
= ipv6_hdr(skb
)->nexthdr
;
1080 /* Get the checksum offset and the L4 (transport) offset */
1081 csum_start
= skb_checksum_start_offset(skb
) - sizeof(*tsb
);
1082 csum_info
= (csum_start
+ skb
->csum_offset
) & L4_CSUM_PTR_MASK
;
1083 csum_info
|= (csum_start
<< L4_PTR_SHIFT
);
1085 if (ip_proto
== IPPROTO_TCP
|| ip_proto
== IPPROTO_UDP
) {
1086 csum_info
|= L4_LENGTH_VALID
;
1087 if (ip_proto
== IPPROTO_UDP
&& ip_ver
== ETH_P_IP
)
1088 csum_info
|= L4_UDP
;
1093 tsb
->l4_ptr_dest_map
= csum_info
;
1099 static netdev_tx_t
bcm_sysport_xmit(struct sk_buff
*skb
,
1100 struct net_device
*dev
)
1102 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1103 struct device
*kdev
= &priv
->pdev
->dev
;
1104 struct bcm_sysport_tx_ring
*ring
;
1105 struct bcm_sysport_cb
*cb
;
1106 struct netdev_queue
*txq
;
1107 struct dma_desc
*desc
;
1108 unsigned int skb_len
;
1109 unsigned long flags
;
1115 queue
= skb_get_queue_mapping(skb
);
1116 txq
= netdev_get_tx_queue(dev
, queue
);
1117 ring
= &priv
->tx_rings
[queue
];
1119 /* lock against tx reclaim in BH context and TX ring full interrupt */
1120 spin_lock_irqsave(&ring
->lock
, flags
);
1121 if (unlikely(ring
->desc_count
== 0)) {
1122 netif_tx_stop_queue(txq
);
1123 netdev_err(dev
, "queue %d awake and ring full!\n", queue
);
1124 ret
= NETDEV_TX_BUSY
;
1128 /* The Ethernet switch we are interfaced with needs packets to be at
1129 * least 64 bytes (including FCS) otherwise they will be discarded when
1130 * they enter the switch port logic. When Broadcom tags are enabled, we
1131 * need to make sure that packets are at least 68 bytes
1132 * (including FCS and tag) because the length verification is done after
1133 * the Broadcom tag is stripped off the ingress packet.
1135 if (skb_put_padto(skb
, ETH_ZLEN
+ ENET_BRCM_TAG_LEN
)) {
1140 /* Insert TSB and checksum infos */
1142 skb
= bcm_sysport_insert_tsb(skb
, dev
);
1151 mapping
= dma_map_single(kdev
, skb
->data
, skb_len
, DMA_TO_DEVICE
);
1152 if (dma_mapping_error(kdev
, mapping
)) {
1153 priv
->mib
.tx_dma_failed
++;
1154 netif_err(priv
, tx_err
, dev
, "DMA map failed at %p (len=%d)\n",
1155 skb
->data
, skb_len
);
1160 /* Remember the SKB for future freeing */
1161 cb
= &ring
->cbs
[ring
->curr_desc
];
1163 dma_unmap_addr_set(cb
, dma_addr
, mapping
);
1164 dma_unmap_len_set(cb
, dma_len
, skb_len
);
1166 /* Fetch a descriptor entry from our pool */
1167 desc
= ring
->desc_cpu
;
1169 desc
->addr_lo
= lower_32_bits(mapping
);
1170 len_status
= upper_32_bits(mapping
) & DESC_ADDR_HI_MASK
;
1171 len_status
|= (skb_len
<< DESC_LEN_SHIFT
);
1172 len_status
|= (DESC_SOP
| DESC_EOP
| TX_STATUS_APP_CRC
) <<
1174 if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1175 len_status
|= (DESC_L4_CSUM
<< DESC_STATUS_SHIFT
);
1178 if (ring
->curr_desc
== ring
->size
)
1179 ring
->curr_desc
= 0;
1182 /* Ensure write completion of the descriptor status/length
1183 * in DRAM before the System Port WRITE_PORT register latches
1187 desc
->addr_status_len
= len_status
;
1190 /* Write this descriptor address to the RING write port */
1191 tdma_port_write_desc_addr(priv
, desc
, ring
->index
);
1193 /* Check ring space and update SW control flow */
1194 if (ring
->desc_count
== 0)
1195 netif_tx_stop_queue(txq
);
1197 netif_dbg(priv
, tx_queued
, dev
, "ring=%d desc_count=%d, curr_desc=%d\n",
1198 ring
->index
, ring
->desc_count
, ring
->curr_desc
);
1202 spin_unlock_irqrestore(&ring
->lock
, flags
);
1206 static void bcm_sysport_tx_timeout(struct net_device
*dev
)
1208 netdev_warn(dev
, "transmit timeout!\n");
1210 netif_trans_update(dev
);
1211 dev
->stats
.tx_errors
++;
1213 netif_tx_wake_all_queues(dev
);
1216 /* phylib adjust link callback */
1217 static void bcm_sysport_adj_link(struct net_device
*dev
)
1219 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1220 struct phy_device
*phydev
= dev
->phydev
;
1221 unsigned int changed
= 0;
1222 u32 cmd_bits
= 0, reg
;
1224 if (priv
->old_link
!= phydev
->link
) {
1226 priv
->old_link
= phydev
->link
;
1229 if (priv
->old_duplex
!= phydev
->duplex
) {
1231 priv
->old_duplex
= phydev
->duplex
;
1237 switch (phydev
->speed
) {
1239 cmd_bits
= CMD_SPEED_2500
;
1242 cmd_bits
= CMD_SPEED_1000
;
1245 cmd_bits
= CMD_SPEED_100
;
1248 cmd_bits
= CMD_SPEED_10
;
1253 cmd_bits
<<= CMD_SPEED_SHIFT
;
1255 if (phydev
->duplex
== DUPLEX_HALF
)
1256 cmd_bits
|= CMD_HD_EN
;
1258 if (priv
->old_pause
!= phydev
->pause
) {
1260 priv
->old_pause
= phydev
->pause
;
1264 cmd_bits
|= CMD_RX_PAUSE_IGNORE
| CMD_TX_PAUSE_IGNORE
;
1270 reg
= umac_readl(priv
, UMAC_CMD
);
1271 reg
&= ~((CMD_SPEED_MASK
<< CMD_SPEED_SHIFT
) |
1272 CMD_HD_EN
| CMD_RX_PAUSE_IGNORE
|
1273 CMD_TX_PAUSE_IGNORE
);
1275 umac_writel(priv
, reg
, UMAC_CMD
);
1279 phy_print_status(phydev
);
1282 static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv
*priv
,
1285 struct bcm_sysport_tx_ring
*ring
= &priv
->tx_rings
[index
];
1286 struct device
*kdev
= &priv
->pdev
->dev
;
1291 /* Simple descriptors partitioning for now */
1294 /* We just need one DMA descriptor which is DMA-able, since writing to
1295 * the port will allocate a new descriptor in its internal linked-list
1297 p
= dma_zalloc_coherent(kdev
, sizeof(struct dma_desc
), &ring
->desc_dma
,
1300 netif_err(priv
, hw
, priv
->netdev
, "DMA alloc failed\n");
1304 ring
->cbs
= kcalloc(size
, sizeof(struct bcm_sysport_cb
), GFP_KERNEL
);
1306 netif_err(priv
, hw
, priv
->netdev
, "CB allocation failed\n");
1310 /* Initialize SW view of the ring */
1311 spin_lock_init(&ring
->lock
);
1313 netif_tx_napi_add(priv
->netdev
, &ring
->napi
, bcm_sysport_tx_poll
, 64);
1314 ring
->index
= index
;
1316 ring
->alloc_size
= ring
->size
;
1318 ring
->desc_count
= ring
->size
;
1319 ring
->curr_desc
= 0;
1321 /* Initialize HW ring */
1322 tdma_writel(priv
, RING_EN
, TDMA_DESC_RING_HEAD_TAIL_PTR(index
));
1323 tdma_writel(priv
, 0, TDMA_DESC_RING_COUNT(index
));
1324 tdma_writel(priv
, 1, TDMA_DESC_RING_INTR_CONTROL(index
));
1325 tdma_writel(priv
, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index
));
1326 tdma_writel(priv
, RING_IGNORE_STATUS
, TDMA_DESC_RING_MAPPING(index
));
1327 tdma_writel(priv
, 0, TDMA_DESC_RING_PCP_DEI_VID(index
));
1329 /* Program the number of descriptors as MAX_THRESHOLD and half of
1330 * its size for the hysteresis trigger
1332 tdma_writel(priv
, ring
->size
|
1333 1 << RING_HYST_THRESH_SHIFT
,
1334 TDMA_DESC_RING_MAX_HYST(index
));
1336 /* Enable the ring queue in the arbiter */
1337 reg
= tdma_readl(priv
, TDMA_TIER1_ARB_0_QUEUE_EN
);
1338 reg
|= (1 << index
);
1339 tdma_writel(priv
, reg
, TDMA_TIER1_ARB_0_QUEUE_EN
);
1341 napi_enable(&ring
->napi
);
1343 netif_dbg(priv
, hw
, priv
->netdev
,
1344 "TDMA cfg, size=%d, desc_cpu=%p\n",
1345 ring
->size
, ring
->desc_cpu
);
1350 static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv
*priv
,
1353 struct bcm_sysport_tx_ring
*ring
= &priv
->tx_rings
[index
];
1354 struct device
*kdev
= &priv
->pdev
->dev
;
1357 /* Caller should stop the TDMA engine */
1358 reg
= tdma_readl(priv
, TDMA_STATUS
);
1359 if (!(reg
& TDMA_DISABLED
))
1360 netdev_warn(priv
->netdev
, "TDMA not stopped!\n");
1362 /* ring->cbs is the last part in bcm_sysport_init_tx_ring which could
1363 * fail, so by checking this pointer we know whether the TX ring was
1364 * fully initialized or not.
1369 napi_disable(&ring
->napi
);
1370 netif_napi_del(&ring
->napi
);
1372 bcm_sysport_tx_clean(priv
, ring
);
1377 if (ring
->desc_dma
) {
1378 dma_free_coherent(kdev
, sizeof(struct dma_desc
),
1379 ring
->desc_cpu
, ring
->desc_dma
);
1383 ring
->alloc_size
= 0;
1385 netif_dbg(priv
, hw
, priv
->netdev
, "TDMA fini done\n");
1389 static inline int rdma_enable_set(struct bcm_sysport_priv
*priv
,
1390 unsigned int enable
)
1392 unsigned int timeout
= 1000;
1395 reg
= rdma_readl(priv
, RDMA_CONTROL
);
1400 rdma_writel(priv
, reg
, RDMA_CONTROL
);
1402 /* Poll for RMDA disabling completion */
1404 reg
= rdma_readl(priv
, RDMA_STATUS
);
1405 if (!!(reg
& RDMA_DISABLED
) == !enable
)
1407 usleep_range(1000, 2000);
1408 } while (timeout
-- > 0);
1410 netdev_err(priv
->netdev
, "timeout waiting for RDMA to finish\n");
1416 static inline int tdma_enable_set(struct bcm_sysport_priv
*priv
,
1417 unsigned int enable
)
1419 unsigned int timeout
= 1000;
1422 reg
= tdma_readl(priv
, TDMA_CONTROL
);
1424 reg
|= tdma_control_bit(priv
, TDMA_EN
);
1426 reg
&= ~tdma_control_bit(priv
, TDMA_EN
);
1427 tdma_writel(priv
, reg
, TDMA_CONTROL
);
1429 /* Poll for TMDA disabling completion */
1431 reg
= tdma_readl(priv
, TDMA_STATUS
);
1432 if (!!(reg
& TDMA_DISABLED
) == !enable
)
1435 usleep_range(1000, 2000);
1436 } while (timeout
-- > 0);
1438 netdev_err(priv
->netdev
, "timeout waiting for TDMA to finish\n");
1443 static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv
*priv
)
1445 struct bcm_sysport_cb
*cb
;
1450 /* Initialize SW view of the RX ring */
1451 priv
->num_rx_bds
= priv
->num_rx_desc_words
/ WORDS_PER_DESC
;
1452 priv
->rx_bds
= priv
->base
+ SYS_PORT_RDMA_OFFSET
;
1453 priv
->rx_c_index
= 0;
1454 priv
->rx_read_ptr
= 0;
1455 priv
->rx_cbs
= kcalloc(priv
->num_rx_bds
, sizeof(struct bcm_sysport_cb
),
1457 if (!priv
->rx_cbs
) {
1458 netif_err(priv
, hw
, priv
->netdev
, "CB allocation failed\n");
1462 for (i
= 0; i
< priv
->num_rx_bds
; i
++) {
1463 cb
= priv
->rx_cbs
+ i
;
1464 cb
->bd_addr
= priv
->rx_bds
+ i
* DESC_SIZE
;
1467 ret
= bcm_sysport_alloc_rx_bufs(priv
);
1469 netif_err(priv
, hw
, priv
->netdev
, "SKB allocation failed\n");
1473 /* Initialize HW, ensure RDMA is disabled */
1474 reg
= rdma_readl(priv
, RDMA_STATUS
);
1475 if (!(reg
& RDMA_DISABLED
))
1476 rdma_enable_set(priv
, 0);
1478 rdma_writel(priv
, 0, RDMA_WRITE_PTR_LO
);
1479 rdma_writel(priv
, 0, RDMA_WRITE_PTR_HI
);
1480 rdma_writel(priv
, 0, RDMA_PROD_INDEX
);
1481 rdma_writel(priv
, 0, RDMA_CONS_INDEX
);
1482 rdma_writel(priv
, priv
->num_rx_bds
<< RDMA_RING_SIZE_SHIFT
|
1483 RX_BUF_LENGTH
, RDMA_RING_BUF_SIZE
);
1484 /* Operate the queue in ring mode */
1485 rdma_writel(priv
, 0, RDMA_START_ADDR_HI
);
1486 rdma_writel(priv
, 0, RDMA_START_ADDR_LO
);
1487 rdma_writel(priv
, 0, RDMA_END_ADDR_HI
);
1488 rdma_writel(priv
, priv
->num_rx_desc_words
- 1, RDMA_END_ADDR_LO
);
1490 rdma_writel(priv
, 1, RDMA_MBDONE_INTR
);
1492 netif_dbg(priv
, hw
, priv
->netdev
,
1493 "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
1494 priv
->num_rx_bds
, priv
->rx_bds
);
1499 static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv
*priv
)
1501 struct bcm_sysport_cb
*cb
;
1505 /* Caller should ensure RDMA is disabled */
1506 reg
= rdma_readl(priv
, RDMA_STATUS
);
1507 if (!(reg
& RDMA_DISABLED
))
1508 netdev_warn(priv
->netdev
, "RDMA not stopped!\n");
1510 for (i
= 0; i
< priv
->num_rx_bds
; i
++) {
1511 cb
= &priv
->rx_cbs
[i
];
1512 if (dma_unmap_addr(cb
, dma_addr
))
1513 dma_unmap_single(&priv
->pdev
->dev
,
1514 dma_unmap_addr(cb
, dma_addr
),
1515 RX_BUF_LENGTH
, DMA_FROM_DEVICE
);
1516 bcm_sysport_free_cb(cb
);
1519 kfree(priv
->rx_cbs
);
1520 priv
->rx_cbs
= NULL
;
1522 netif_dbg(priv
, hw
, priv
->netdev
, "RDMA fini done\n");
1525 static void bcm_sysport_set_rx_mode(struct net_device
*dev
)
1527 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1533 reg
= umac_readl(priv
, UMAC_CMD
);
1534 if (dev
->flags
& IFF_PROMISC
)
1537 reg
&= ~CMD_PROMISC
;
1538 umac_writel(priv
, reg
, UMAC_CMD
);
1540 /* No support for ALLMULTI */
1541 if (dev
->flags
& IFF_ALLMULTI
)
1545 static inline void umac_enable_set(struct bcm_sysport_priv
*priv
,
1546 u32 mask
, unsigned int enable
)
1550 if (!priv
->is_lite
) {
1551 reg
= umac_readl(priv
, UMAC_CMD
);
1556 umac_writel(priv
, reg
, UMAC_CMD
);
1558 reg
= gib_readl(priv
, GIB_CONTROL
);
1563 gib_writel(priv
, reg
, GIB_CONTROL
);
1566 /* UniMAC stops on a packet boundary, wait for a full-sized packet
1567 * to be processed (1 msec).
1570 usleep_range(1000, 2000);
1573 static inline void umac_reset(struct bcm_sysport_priv
*priv
)
1580 reg
= umac_readl(priv
, UMAC_CMD
);
1581 reg
|= CMD_SW_RESET
;
1582 umac_writel(priv
, reg
, UMAC_CMD
);
1584 reg
= umac_readl(priv
, UMAC_CMD
);
1585 reg
&= ~CMD_SW_RESET
;
1586 umac_writel(priv
, reg
, UMAC_CMD
);
1589 static void umac_set_hw_addr(struct bcm_sysport_priv
*priv
,
1590 unsigned char *addr
)
1592 u32 mac0
= (addr
[0] << 24) | (addr
[1] << 16) | (addr
[2] << 8) |
1594 u32 mac1
= (addr
[4] << 8) | addr
[5];
1596 if (!priv
->is_lite
) {
1597 umac_writel(priv
, mac0
, UMAC_MAC0
);
1598 umac_writel(priv
, mac1
, UMAC_MAC1
);
1600 gib_writel(priv
, mac0
, GIB_MAC0
);
1601 gib_writel(priv
, mac1
, GIB_MAC1
);
1605 static void topctrl_flush(struct bcm_sysport_priv
*priv
)
1607 topctrl_writel(priv
, RX_FLUSH
, RX_FLUSH_CNTL
);
1608 topctrl_writel(priv
, TX_FLUSH
, TX_FLUSH_CNTL
);
1610 topctrl_writel(priv
, 0, RX_FLUSH_CNTL
);
1611 topctrl_writel(priv
, 0, TX_FLUSH_CNTL
);
1614 static int bcm_sysport_change_mac(struct net_device
*dev
, void *p
)
1616 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1617 struct sockaddr
*addr
= p
;
1619 if (!is_valid_ether_addr(addr
->sa_data
))
1622 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
1624 /* interface is disabled, changes to MAC will be reflected on next
1627 if (!netif_running(dev
))
1630 umac_set_hw_addr(priv
, dev
->dev_addr
);
1635 static void bcm_sysport_netif_start(struct net_device
*dev
)
1637 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1640 napi_enable(&priv
->napi
);
1642 /* Enable RX interrupt and TX ring full interrupt */
1643 intrl2_0_mask_clear(priv
, INTRL2_0_RDMA_MBDONE
| INTRL2_0_TX_RING_FULL
);
1645 phy_start(dev
->phydev
);
1647 /* Enable TX interrupts for the TXQs */
1649 intrl2_1_mask_clear(priv
, 0xffffffff);
1651 intrl2_0_mask_clear(priv
, INTRL2_0_TDMA_MBDONE_MASK
);
1653 /* Last call before we start the real business */
1654 netif_tx_start_all_queues(dev
);
1657 static void rbuf_init(struct bcm_sysport_priv
*priv
)
1661 reg
= rbuf_readl(priv
, RBUF_CONTROL
);
1662 reg
|= RBUF_4B_ALGN
| RBUF_RSB_EN
;
1663 /* Set a correct RSB format on SYSTEMPORT Lite */
1664 if (priv
->is_lite
) {
1665 reg
&= ~RBUF_RSB_SWAP1
;
1666 reg
|= RBUF_RSB_SWAP0
;
1668 rbuf_writel(priv
, reg
, RBUF_CONTROL
);
1671 static inline void bcm_sysport_mask_all_intrs(struct bcm_sysport_priv
*priv
)
1673 intrl2_0_mask_set(priv
, 0xffffffff);
1674 intrl2_0_writel(priv
, 0xffffffff, INTRL2_CPU_CLEAR
);
1675 if (!priv
->is_lite
) {
1676 intrl2_1_mask_set(priv
, 0xffffffff);
1677 intrl2_1_writel(priv
, 0xffffffff, INTRL2_CPU_CLEAR
);
1681 static inline void gib_set_pad_extension(struct bcm_sysport_priv
*priv
)
1683 u32 __maybe_unused reg
;
1685 /* Include Broadcom tag in pad extension */
1686 if (netdev_uses_dsa(priv
->netdev
)) {
1687 reg
= gib_readl(priv
, GIB_CONTROL
);
1688 reg
&= ~(GIB_PAD_EXTENSION_MASK
<< GIB_PAD_EXTENSION_SHIFT
);
1689 reg
|= ENET_BRCM_TAG_LEN
<< GIB_PAD_EXTENSION_SHIFT
;
1690 gib_writel(priv
, reg
, GIB_CONTROL
);
1694 static int bcm_sysport_open(struct net_device
*dev
)
1696 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1697 struct phy_device
*phydev
;
1704 /* Flush TX and RX FIFOs at TOPCTRL level */
1705 topctrl_flush(priv
);
1707 /* Disable the UniMAC RX/TX */
1708 umac_enable_set(priv
, CMD_RX_EN
| CMD_TX_EN
, 0);
1710 /* Enable RBUF 2bytes alignment and Receive Status Block */
1713 /* Set maximum frame length */
1715 umac_writel(priv
, UMAC_MAX_MTU_SIZE
, UMAC_MAX_FRAME_LEN
);
1717 gib_set_pad_extension(priv
);
1719 /* Set MAC address */
1720 umac_set_hw_addr(priv
, dev
->dev_addr
);
1722 /* Read CRC forward */
1724 priv
->crc_fwd
= !!(umac_readl(priv
, UMAC_CMD
) & CMD_CRC_FWD
);
1726 priv
->crc_fwd
= !!(gib_readl(priv
, GIB_CONTROL
) &
1729 phydev
= of_phy_connect(dev
, priv
->phy_dn
, bcm_sysport_adj_link
,
1730 0, priv
->phy_interface
);
1732 netdev_err(dev
, "could not attach to PHY\n");
1736 /* Reset house keeping link status */
1737 priv
->old_duplex
= -1;
1738 priv
->old_link
= -1;
1739 priv
->old_pause
= -1;
1741 /* mask all interrupts and request them */
1742 bcm_sysport_mask_all_intrs(priv
);
1744 ret
= request_irq(priv
->irq0
, bcm_sysport_rx_isr
, 0, dev
->name
, dev
);
1746 netdev_err(dev
, "failed to request RX interrupt\n");
1747 goto out_phy_disconnect
;
1750 if (!priv
->is_lite
) {
1751 ret
= request_irq(priv
->irq1
, bcm_sysport_tx_isr
, 0,
1754 netdev_err(dev
, "failed to request TX interrupt\n");
1759 /* Initialize both hardware and software ring */
1760 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
1761 ret
= bcm_sysport_init_tx_ring(priv
, i
);
1763 netdev_err(dev
, "failed to initialize TX ring %d\n",
1765 goto out_free_tx_ring
;
1769 /* Initialize linked-list */
1770 tdma_writel(priv
, TDMA_LL_RAM_INIT_BUSY
, TDMA_STATUS
);
1772 /* Initialize RX ring */
1773 ret
= bcm_sysport_init_rx_ring(priv
);
1775 netdev_err(dev
, "failed to initialize RX ring\n");
1776 goto out_free_rx_ring
;
1780 ret
= rdma_enable_set(priv
, 1);
1782 goto out_free_rx_ring
;
1785 ret
= tdma_enable_set(priv
, 1);
1787 goto out_clear_rx_int
;
1789 /* Turn on UniMAC TX/RX */
1790 umac_enable_set(priv
, CMD_RX_EN
| CMD_TX_EN
, 1);
1792 bcm_sysport_netif_start(dev
);
1797 intrl2_0_mask_set(priv
, INTRL2_0_RDMA_MBDONE
| INTRL2_0_TX_RING_FULL
);
1799 bcm_sysport_fini_rx_ring(priv
);
1801 for (i
= 0; i
< dev
->num_tx_queues
; i
++)
1802 bcm_sysport_fini_tx_ring(priv
, i
);
1804 free_irq(priv
->irq1
, dev
);
1806 free_irq(priv
->irq0
, dev
);
1808 phy_disconnect(phydev
);
1812 static void bcm_sysport_netif_stop(struct net_device
*dev
)
1814 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1816 /* stop all software from updating hardware */
1817 netif_tx_stop_all_queues(dev
);
1818 napi_disable(&priv
->napi
);
1819 phy_stop(dev
->phydev
);
1821 /* mask all interrupts */
1822 bcm_sysport_mask_all_intrs(priv
);
1825 static int bcm_sysport_stop(struct net_device
*dev
)
1827 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
1831 bcm_sysport_netif_stop(dev
);
1833 /* Disable UniMAC RX */
1834 umac_enable_set(priv
, CMD_RX_EN
, 0);
1836 ret
= tdma_enable_set(priv
, 0);
1838 netdev_err(dev
, "timeout disabling RDMA\n");
1842 /* Wait for a maximum packet size to be drained */
1843 usleep_range(2000, 3000);
1845 ret
= rdma_enable_set(priv
, 0);
1847 netdev_err(dev
, "timeout disabling TDMA\n");
1851 /* Disable UniMAC TX */
1852 umac_enable_set(priv
, CMD_TX_EN
, 0);
1854 /* Free RX/TX rings SW structures */
1855 for (i
= 0; i
< dev
->num_tx_queues
; i
++)
1856 bcm_sysport_fini_tx_ring(priv
, i
);
1857 bcm_sysport_fini_rx_ring(priv
);
1859 free_irq(priv
->irq0
, dev
);
1861 free_irq(priv
->irq1
, dev
);
1863 /* Disconnect from PHY */
1864 phy_disconnect(dev
->phydev
);
1869 static const struct ethtool_ops bcm_sysport_ethtool_ops
= {
1870 .get_drvinfo
= bcm_sysport_get_drvinfo
,
1871 .get_msglevel
= bcm_sysport_get_msglvl
,
1872 .set_msglevel
= bcm_sysport_set_msglvl
,
1873 .get_link
= ethtool_op_get_link
,
1874 .get_strings
= bcm_sysport_get_strings
,
1875 .get_ethtool_stats
= bcm_sysport_get_stats
,
1876 .get_sset_count
= bcm_sysport_get_sset_count
,
1877 .get_wol
= bcm_sysport_get_wol
,
1878 .set_wol
= bcm_sysport_set_wol
,
1879 .get_coalesce
= bcm_sysport_get_coalesce
,
1880 .set_coalesce
= bcm_sysport_set_coalesce
,
1881 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
1882 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
1885 static const struct net_device_ops bcm_sysport_netdev_ops
= {
1886 .ndo_start_xmit
= bcm_sysport_xmit
,
1887 .ndo_tx_timeout
= bcm_sysport_tx_timeout
,
1888 .ndo_open
= bcm_sysport_open
,
1889 .ndo_stop
= bcm_sysport_stop
,
1890 .ndo_set_features
= bcm_sysport_set_features
,
1891 .ndo_set_rx_mode
= bcm_sysport_set_rx_mode
,
1892 .ndo_set_mac_address
= bcm_sysport_change_mac
,
1893 #ifdef CONFIG_NET_POLL_CONTROLLER
1894 .ndo_poll_controller
= bcm_sysport_poll_controller
,
1898 #define REV_FMT "v%2x.%02x"
1900 static const struct bcm_sysport_hw_params bcm_sysport_params
[] = {
1903 .num_rx_desc_words
= SP_NUM_HW_RX_DESC_WORDS
,
1905 [SYSTEMPORT_LITE
] = {
1907 .num_rx_desc_words
= SP_LT_NUM_HW_RX_DESC_WORDS
,
1911 static const struct of_device_id bcm_sysport_of_match
[] = {
1912 { .compatible
= "brcm,systemportlite-v1.00",
1913 .data
= &bcm_sysport_params
[SYSTEMPORT_LITE
] },
1914 { .compatible
= "brcm,systemport-v1.00",
1915 .data
= &bcm_sysport_params
[SYSTEMPORT
] },
1916 { .compatible
= "brcm,systemport",
1917 .data
= &bcm_sysport_params
[SYSTEMPORT
] },
1920 MODULE_DEVICE_TABLE(of
, bcm_sysport_of_match
);
1922 static int bcm_sysport_probe(struct platform_device
*pdev
)
1924 const struct bcm_sysport_hw_params
*params
;
1925 const struct of_device_id
*of_id
= NULL
;
1926 struct bcm_sysport_priv
*priv
;
1927 struct device_node
*dn
;
1928 struct net_device
*dev
;
1929 const void *macaddr
;
1934 dn
= pdev
->dev
.of_node
;
1935 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1936 of_id
= of_match_node(bcm_sysport_of_match
, dn
);
1937 if (!of_id
|| !of_id
->data
)
1940 /* Fairly quickly we need to know the type of adapter we have */
1941 params
= of_id
->data
;
1943 /* Read the Transmit/Receive Queue properties */
1944 if (of_property_read_u32(dn
, "systemport,num-txq", &txq
))
1945 txq
= TDMA_NUM_RINGS
;
1946 if (of_property_read_u32(dn
, "systemport,num-rxq", &rxq
))
1949 /* Sanity check the number of transmit queues */
1950 if (!txq
|| txq
> TDMA_NUM_RINGS
)
1953 dev
= alloc_etherdev_mqs(sizeof(*priv
), txq
, rxq
);
1957 /* Initialize private members */
1958 priv
= netdev_priv(dev
);
1960 /* Allocate number of TX rings */
1961 priv
->tx_rings
= devm_kcalloc(&pdev
->dev
, txq
,
1962 sizeof(struct bcm_sysport_tx_ring
),
1964 if (!priv
->tx_rings
)
1967 priv
->is_lite
= params
->is_lite
;
1968 priv
->num_rx_desc_words
= params
->num_rx_desc_words
;
1970 priv
->irq0
= platform_get_irq(pdev
, 0);
1971 if (!priv
->is_lite
) {
1972 priv
->irq1
= platform_get_irq(pdev
, 1);
1973 priv
->wol_irq
= platform_get_irq(pdev
, 2);
1975 priv
->wol_irq
= platform_get_irq(pdev
, 1);
1977 if (priv
->irq0
<= 0 || (priv
->irq1
<= 0 && !priv
->is_lite
)) {
1978 dev_err(&pdev
->dev
, "invalid interrupts\n");
1980 goto err_free_netdev
;
1983 priv
->base
= devm_ioremap_resource(&pdev
->dev
, r
);
1984 if (IS_ERR(priv
->base
)) {
1985 ret
= PTR_ERR(priv
->base
);
1986 goto err_free_netdev
;
1992 priv
->phy_interface
= of_get_phy_mode(dn
);
1993 /* Default to GMII interface mode */
1994 if (priv
->phy_interface
< 0)
1995 priv
->phy_interface
= PHY_INTERFACE_MODE_GMII
;
1997 /* In the case of a fixed PHY, the DT node associated
1998 * to the PHY is the Ethernet MAC DT node.
2000 if (of_phy_is_fixed_link(dn
)) {
2001 ret
= of_phy_register_fixed_link(dn
);
2003 dev_err(&pdev
->dev
, "failed to register fixed PHY\n");
2004 goto err_free_netdev
;
2010 /* Initialize netdevice members */
2011 macaddr
= of_get_mac_address(dn
);
2012 if (!macaddr
|| !is_valid_ether_addr(macaddr
)) {
2013 dev_warn(&pdev
->dev
, "using random Ethernet MAC\n");
2014 eth_hw_addr_random(dev
);
2016 ether_addr_copy(dev
->dev_addr
, macaddr
);
2019 SET_NETDEV_DEV(dev
, &pdev
->dev
);
2020 dev_set_drvdata(&pdev
->dev
, dev
);
2021 dev
->ethtool_ops
= &bcm_sysport_ethtool_ops
;
2022 dev
->netdev_ops
= &bcm_sysport_netdev_ops
;
2023 netif_napi_add(dev
, &priv
->napi
, bcm_sysport_poll
, 64);
2025 /* HW supported features, none enabled by default */
2026 dev
->hw_features
|= NETIF_F_RXCSUM
| NETIF_F_HIGHDMA
|
2027 NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
2029 /* Request the WOL interrupt and advertise suspend if available */
2030 priv
->wol_irq_disabled
= 1;
2031 ret
= devm_request_irq(&pdev
->dev
, priv
->wol_irq
,
2032 bcm_sysport_wol_isr
, 0, dev
->name
, priv
);
2034 device_set_wakeup_capable(&pdev
->dev
, 1);
2036 /* Set the needed headroom once and for all */
2037 BUILD_BUG_ON(sizeof(struct bcm_tsb
) != 8);
2038 dev
->needed_headroom
+= sizeof(struct bcm_tsb
);
2040 /* libphy will adjust the link state accordingly */
2041 netif_carrier_off(dev
);
2043 ret
= register_netdev(dev
);
2045 dev_err(&pdev
->dev
, "failed to register net_device\n");
2046 goto err_deregister_fixed_link
;
2049 priv
->rev
= topctrl_readl(priv
, REV_CNTL
) & REV_MASK
;
2050 dev_info(&pdev
->dev
,
2051 "Broadcom SYSTEMPORT%s" REV_FMT
2052 " at 0x%p (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
2053 priv
->is_lite
? " Lite" : "",
2054 (priv
->rev
>> 8) & 0xff, priv
->rev
& 0xff,
2055 priv
->base
, priv
->irq0
, priv
->irq1
, txq
, rxq
);
2059 err_deregister_fixed_link
:
2060 if (of_phy_is_fixed_link(dn
))
2061 of_phy_deregister_fixed_link(dn
);
2067 static int bcm_sysport_remove(struct platform_device
*pdev
)
2069 struct net_device
*dev
= dev_get_drvdata(&pdev
->dev
);
2070 struct device_node
*dn
= pdev
->dev
.of_node
;
2072 /* Not much to do, ndo_close has been called
2073 * and we use managed allocations
2075 unregister_netdev(dev
);
2076 if (of_phy_is_fixed_link(dn
))
2077 of_phy_deregister_fixed_link(dn
);
2079 dev_set_drvdata(&pdev
->dev
, NULL
);
2084 #ifdef CONFIG_PM_SLEEP
2085 static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv
*priv
)
2087 struct net_device
*ndev
= priv
->netdev
;
2088 unsigned int timeout
= 1000;
2091 /* Password has already been programmed */
2092 reg
= umac_readl(priv
, UMAC_MPD_CTRL
);
2095 if (priv
->wolopts
& WAKE_MAGICSECURE
)
2097 umac_writel(priv
, reg
, UMAC_MPD_CTRL
);
2099 /* Make sure RBUF entered WoL mode as result */
2101 reg
= rbuf_readl(priv
, RBUF_STATUS
);
2102 if (reg
& RBUF_WOL_MODE
)
2106 } while (timeout
-- > 0);
2108 /* Do not leave the UniMAC RBUF matching only MPD packets */
2110 reg
= umac_readl(priv
, UMAC_MPD_CTRL
);
2112 umac_writel(priv
, reg
, UMAC_MPD_CTRL
);
2113 netif_err(priv
, wol
, ndev
, "failed to enter WOL mode\n");
2117 /* UniMAC receive needs to be turned on */
2118 umac_enable_set(priv
, CMD_RX_EN
, 1);
2120 /* Enable the interrupt wake-up source */
2121 intrl2_0_mask_clear(priv
, INTRL2_0_MPD
);
2123 netif_dbg(priv
, wol
, ndev
, "entered WOL mode\n");
2128 static int bcm_sysport_suspend(struct device
*d
)
2130 struct net_device
*dev
= dev_get_drvdata(d
);
2131 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
2136 if (!netif_running(dev
))
2139 bcm_sysport_netif_stop(dev
);
2141 phy_suspend(dev
->phydev
);
2143 netif_device_detach(dev
);
2145 /* Disable UniMAC RX */
2146 umac_enable_set(priv
, CMD_RX_EN
, 0);
2148 ret
= rdma_enable_set(priv
, 0);
2150 netdev_err(dev
, "RDMA timeout!\n");
2154 /* Disable RXCHK if enabled */
2155 if (priv
->rx_chk_en
) {
2156 reg
= rxchk_readl(priv
, RXCHK_CONTROL
);
2158 rxchk_writel(priv
, reg
, RXCHK_CONTROL
);
2163 topctrl_writel(priv
, RX_FLUSH
, RX_FLUSH_CNTL
);
2165 ret
= tdma_enable_set(priv
, 0);
2167 netdev_err(dev
, "TDMA timeout!\n");
2171 /* Wait for a packet boundary */
2172 usleep_range(2000, 3000);
2174 umac_enable_set(priv
, CMD_TX_EN
, 0);
2176 topctrl_writel(priv
, TX_FLUSH
, TX_FLUSH_CNTL
);
2178 /* Free RX/TX rings SW structures */
2179 for (i
= 0; i
< dev
->num_tx_queues
; i
++)
2180 bcm_sysport_fini_tx_ring(priv
, i
);
2181 bcm_sysport_fini_rx_ring(priv
);
2183 /* Get prepared for Wake-on-LAN */
2184 if (device_may_wakeup(d
) && priv
->wolopts
)
2185 ret
= bcm_sysport_suspend_to_wol(priv
);
2190 static int bcm_sysport_resume(struct device
*d
)
2192 struct net_device
*dev
= dev_get_drvdata(d
);
2193 struct bcm_sysport_priv
*priv
= netdev_priv(dev
);
2198 if (!netif_running(dev
))
2203 /* We may have been suspended and never received a WOL event that
2204 * would turn off MPD detection, take care of that now
2206 bcm_sysport_resume_from_wol(priv
);
2208 /* Initialize both hardware and software ring */
2209 for (i
= 0; i
< dev
->num_tx_queues
; i
++) {
2210 ret
= bcm_sysport_init_tx_ring(priv
, i
);
2212 netdev_err(dev
, "failed to initialize TX ring %d\n",
2214 goto out_free_tx_rings
;
2218 /* Initialize linked-list */
2219 tdma_writel(priv
, TDMA_LL_RAM_INIT_BUSY
, TDMA_STATUS
);
2221 /* Initialize RX ring */
2222 ret
= bcm_sysport_init_rx_ring(priv
);
2224 netdev_err(dev
, "failed to initialize RX ring\n");
2225 goto out_free_rx_ring
;
2228 netif_device_attach(dev
);
2230 /* RX pipe enable */
2231 topctrl_writel(priv
, 0, RX_FLUSH_CNTL
);
2233 ret
= rdma_enable_set(priv
, 1);
2235 netdev_err(dev
, "failed to enable RDMA\n");
2236 goto out_free_rx_ring
;
2240 if (priv
->rx_chk_en
) {
2241 reg
= rxchk_readl(priv
, RXCHK_CONTROL
);
2243 rxchk_writel(priv
, reg
, RXCHK_CONTROL
);
2248 /* Set maximum frame length */
2250 umac_writel(priv
, UMAC_MAX_MTU_SIZE
, UMAC_MAX_FRAME_LEN
);
2252 gib_set_pad_extension(priv
);
2254 /* Set MAC address */
2255 umac_set_hw_addr(priv
, dev
->dev_addr
);
2257 umac_enable_set(priv
, CMD_RX_EN
, 1);
2259 /* TX pipe enable */
2260 topctrl_writel(priv
, 0, TX_FLUSH_CNTL
);
2262 umac_enable_set(priv
, CMD_TX_EN
, 1);
2264 ret
= tdma_enable_set(priv
, 1);
2266 netdev_err(dev
, "TDMA timeout!\n");
2267 goto out_free_rx_ring
;
2270 phy_resume(dev
->phydev
);
2272 bcm_sysport_netif_start(dev
);
2277 bcm_sysport_fini_rx_ring(priv
);
2279 for (i
= 0; i
< dev
->num_tx_queues
; i
++)
2280 bcm_sysport_fini_tx_ring(priv
, i
);
2285 static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops
,
2286 bcm_sysport_suspend
, bcm_sysport_resume
);
2288 static struct platform_driver bcm_sysport_driver
= {
2289 .probe
= bcm_sysport_probe
,
2290 .remove
= bcm_sysport_remove
,
2292 .name
= "brcm-systemport",
2293 .of_match_table
= bcm_sysport_of_match
,
2294 .pm
= &bcm_sysport_pm_ops
,
2297 module_platform_driver(bcm_sysport_driver
);
2299 MODULE_AUTHOR("Broadcom Corporation");
2300 MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
2301 MODULE_ALIAS("platform:brcm-systemport");
2302 MODULE_LICENSE("GPL");