blob: 0f65b4a4d4a477114d35c9837668a620ca68cea6 [file] [log] [blame]
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001/*
2 * Broadcom GENET (Gigabit Ethernet) controller driver
3 *
4 * Copyright (c) 2014 Broadcom Corporation
5 *
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.
Florian Fainelli1c1008c2014-02-13 16:08:47 -08009 */
10
11#define pr_fmt(fmt) "bcmgenet: " fmt
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/types.h>
17#include <linux/fcntl.h>
18#include <linux/interrupt.h>
19#include <linux/string.h>
20#include <linux/if_ether.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/delay.h>
24#include <linux/platform_device.h>
25#include <linux/dma-mapping.h>
26#include <linux/pm.h>
27#include <linux/clk.h>
Florian Fainelli1c1008c2014-02-13 16:08:47 -080028#include <linux/of.h>
29#include <linux/of_address.h>
30#include <linux/of_irq.h>
31#include <linux/of_net.h>
32#include <linux/of_platform.h>
33#include <net/arp.h>
34
35#include <linux/mii.h>
36#include <linux/ethtool.h>
37#include <linux/netdevice.h>
38#include <linux/inetdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/skbuff.h>
41#include <linux/in.h>
42#include <linux/ip.h>
43#include <linux/ipv6.h>
44#include <linux/phy.h>
45
46#include <asm/unaligned.h>
47
48#include "bcmgenet.h"
49
50/* Maximum number of hardware queues, downsized if needed */
51#define GENET_MAX_MQ_CNT 4
52
53/* Default highest priority queue for multi queue support */
54#define GENET_Q0_PRIORITY 0
55
56#define GENET_DEFAULT_BD_CNT \
57 (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->bds_cnt)
58
59#define RX_BUF_LENGTH 2048
60#define SKB_ALIGNMENT 32
61
62/* Tx/Rx DMA register offset, skip 256 descriptors */
63#define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
64#define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
65
66#define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
67 TOTAL_DESC * DMA_DESC_SIZE)
68
69#define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
70 TOTAL_DESC * DMA_DESC_SIZE)
71
72static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070073 void __iomem *d, u32 value)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080074{
75 __raw_writel(value, d + DMA_DESC_LENGTH_STATUS);
76}
77
78static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -070079 void __iomem *d)
Florian Fainelli1c1008c2014-02-13 16:08:47 -080080{
81 return __raw_readl(d + DMA_DESC_LENGTH_STATUS);
82}
83
84static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
85 void __iomem *d,
86 dma_addr_t addr)
87{
88 __raw_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
89
90 /* Register writes to GISB bus can take couple hundred nanoseconds
91 * and are done for each packet, save these expensive writes unless
92 * the platform is explicitely configured for 64-bits/LPAE.
93 */
94#ifdef CONFIG_PHYS_ADDR_T_64BIT
95 if (priv->hw_params->flags & GENET_HAS_40BITS)
96 __raw_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
97#endif
98}
99
100/* Combined address + length/status setter */
101static inline void dmadesc_set(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700102 void __iomem *d, dma_addr_t addr, u32 val)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800103{
104 dmadesc_set_length_status(priv, d, val);
105 dmadesc_set_addr(priv, d, addr);
106}
107
108static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
109 void __iomem *d)
110{
111 dma_addr_t addr;
112
113 addr = __raw_readl(d + DMA_DESC_ADDRESS_LO);
114
115 /* Register writes to GISB bus can take couple hundred nanoseconds
116 * and are done for each packet, save these expensive writes unless
117 * the platform is explicitely configured for 64-bits/LPAE.
118 */
119#ifdef CONFIG_PHYS_ADDR_T_64BIT
120 if (priv->hw_params->flags & GENET_HAS_40BITS)
121 addr |= (u64)__raw_readl(d + DMA_DESC_ADDRESS_HI) << 32;
122#endif
123 return addr;
124}
125
126#define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
127
128#define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
129 NETIF_MSG_LINK)
130
131static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
132{
133 if (GENET_IS_V1(priv))
134 return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
135 else
136 return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
137}
138
139static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
140{
141 if (GENET_IS_V1(priv))
142 bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
143 else
144 bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
145}
146
147/* These macros are defined to deal with register map change
148 * between GENET1.1 and GENET2. Only those currently being used
149 * by driver are defined.
150 */
151static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
152{
153 if (GENET_IS_V1(priv))
154 return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
155 else
156 return __raw_readl(priv->base +
157 priv->hw_params->tbuf_offset + TBUF_CTRL);
158}
159
160static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
161{
162 if (GENET_IS_V1(priv))
163 bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
164 else
165 __raw_writel(val, priv->base +
166 priv->hw_params->tbuf_offset + TBUF_CTRL);
167}
168
169static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
170{
171 if (GENET_IS_V1(priv))
172 return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
173 else
174 return __raw_readl(priv->base +
175 priv->hw_params->tbuf_offset + TBUF_BP_MC);
176}
177
178static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
179{
180 if (GENET_IS_V1(priv))
181 bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
182 else
183 __raw_writel(val, priv->base +
184 priv->hw_params->tbuf_offset + TBUF_BP_MC);
185}
186
187/* RX/TX DMA register accessors */
188enum dma_reg {
189 DMA_RING_CFG = 0,
190 DMA_CTRL,
191 DMA_STATUS,
192 DMA_SCB_BURST_SIZE,
193 DMA_ARB_CTRL,
194 DMA_PRIORITY,
195 DMA_RING_PRIORITY,
196};
197
198static const u8 bcmgenet_dma_regs_v3plus[] = {
199 [DMA_RING_CFG] = 0x00,
200 [DMA_CTRL] = 0x04,
201 [DMA_STATUS] = 0x08,
202 [DMA_SCB_BURST_SIZE] = 0x0C,
203 [DMA_ARB_CTRL] = 0x2C,
204 [DMA_PRIORITY] = 0x30,
205 [DMA_RING_PRIORITY] = 0x38,
206};
207
208static const u8 bcmgenet_dma_regs_v2[] = {
209 [DMA_RING_CFG] = 0x00,
210 [DMA_CTRL] = 0x04,
211 [DMA_STATUS] = 0x08,
212 [DMA_SCB_BURST_SIZE] = 0x0C,
213 [DMA_ARB_CTRL] = 0x30,
214 [DMA_PRIORITY] = 0x34,
215 [DMA_RING_PRIORITY] = 0x3C,
216};
217
218static const u8 bcmgenet_dma_regs_v1[] = {
219 [DMA_CTRL] = 0x00,
220 [DMA_STATUS] = 0x04,
221 [DMA_SCB_BURST_SIZE] = 0x0C,
222 [DMA_ARB_CTRL] = 0x30,
223 [DMA_PRIORITY] = 0x34,
224 [DMA_RING_PRIORITY] = 0x3C,
225};
226
227/* Set at runtime once bcmgenet version is known */
228static const u8 *bcmgenet_dma_regs;
229
230static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
231{
232 return netdev_priv(dev_get_drvdata(dev));
233}
234
235static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700236 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800237{
238 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
239 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
240}
241
242static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
243 u32 val, enum dma_reg r)
244{
245 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
246 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
247}
248
249static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700250 enum dma_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800251{
252 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
253 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
254}
255
256static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
257 u32 val, enum dma_reg r)
258{
259 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
260 DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
261}
262
263/* RDMA/TDMA ring registers and accessors
264 * we merge the common fields and just prefix with T/D the registers
265 * having different meaning depending on the direction
266 */
267enum dma_ring_reg {
268 TDMA_READ_PTR = 0,
269 RDMA_WRITE_PTR = TDMA_READ_PTR,
270 TDMA_READ_PTR_HI,
271 RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
272 TDMA_CONS_INDEX,
273 RDMA_PROD_INDEX = TDMA_CONS_INDEX,
274 TDMA_PROD_INDEX,
275 RDMA_CONS_INDEX = TDMA_PROD_INDEX,
276 DMA_RING_BUF_SIZE,
277 DMA_START_ADDR,
278 DMA_START_ADDR_HI,
279 DMA_END_ADDR,
280 DMA_END_ADDR_HI,
281 DMA_MBUF_DONE_THRESH,
282 TDMA_FLOW_PERIOD,
283 RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
284 TDMA_WRITE_PTR,
285 RDMA_READ_PTR = TDMA_WRITE_PTR,
286 TDMA_WRITE_PTR_HI,
287 RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
288};
289
290/* GENET v4 supports 40-bits pointer addressing
291 * for obvious reasons the LO and HI word parts
292 * are contiguous, but this offsets the other
293 * registers.
294 */
295static const u8 genet_dma_ring_regs_v4[] = {
296 [TDMA_READ_PTR] = 0x00,
297 [TDMA_READ_PTR_HI] = 0x04,
298 [TDMA_CONS_INDEX] = 0x08,
299 [TDMA_PROD_INDEX] = 0x0C,
300 [DMA_RING_BUF_SIZE] = 0x10,
301 [DMA_START_ADDR] = 0x14,
302 [DMA_START_ADDR_HI] = 0x18,
303 [DMA_END_ADDR] = 0x1C,
304 [DMA_END_ADDR_HI] = 0x20,
305 [DMA_MBUF_DONE_THRESH] = 0x24,
306 [TDMA_FLOW_PERIOD] = 0x28,
307 [TDMA_WRITE_PTR] = 0x2C,
308 [TDMA_WRITE_PTR_HI] = 0x30,
309};
310
311static const u8 genet_dma_ring_regs_v123[] = {
312 [TDMA_READ_PTR] = 0x00,
313 [TDMA_CONS_INDEX] = 0x04,
314 [TDMA_PROD_INDEX] = 0x08,
315 [DMA_RING_BUF_SIZE] = 0x0C,
316 [DMA_START_ADDR] = 0x10,
317 [DMA_END_ADDR] = 0x14,
318 [DMA_MBUF_DONE_THRESH] = 0x18,
319 [TDMA_FLOW_PERIOD] = 0x1C,
320 [TDMA_WRITE_PTR] = 0x20,
321};
322
323/* Set at runtime once GENET version is known */
324static const u8 *genet_dma_ring_regs;
325
326static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700327 unsigned int ring,
328 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800329{
330 return __raw_readl(priv->base + GENET_TDMA_REG_OFF +
331 (DMA_RING_SIZE * ring) +
332 genet_dma_ring_regs[r]);
333}
334
335static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700336 unsigned int ring, u32 val,
337 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800338{
339 __raw_writel(val, priv->base + GENET_TDMA_REG_OFF +
340 (DMA_RING_SIZE * ring) +
341 genet_dma_ring_regs[r]);
342}
343
344static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700345 unsigned int ring,
346 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800347{
348 return __raw_readl(priv->base + GENET_RDMA_REG_OFF +
349 (DMA_RING_SIZE * ring) +
350 genet_dma_ring_regs[r]);
351}
352
353static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700354 unsigned int ring, u32 val,
355 enum dma_ring_reg r)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800356{
357 __raw_writel(val, priv->base + GENET_RDMA_REG_OFF +
358 (DMA_RING_SIZE * ring) +
359 genet_dma_ring_regs[r]);
360}
361
362static int bcmgenet_get_settings(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700363 struct ethtool_cmd *cmd)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800364{
365 struct bcmgenet_priv *priv = netdev_priv(dev);
366
367 if (!netif_running(dev))
368 return -EINVAL;
369
370 if (!priv->phydev)
371 return -ENODEV;
372
373 return phy_ethtool_gset(priv->phydev, cmd);
374}
375
376static int bcmgenet_set_settings(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700377 struct ethtool_cmd *cmd)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800378{
379 struct bcmgenet_priv *priv = netdev_priv(dev);
380
381 if (!netif_running(dev))
382 return -EINVAL;
383
384 if (!priv->phydev)
385 return -ENODEV;
386
387 return phy_ethtool_sset(priv->phydev, cmd);
388}
389
390static int bcmgenet_set_rx_csum(struct net_device *dev,
391 netdev_features_t wanted)
392{
393 struct bcmgenet_priv *priv = netdev_priv(dev);
394 u32 rbuf_chk_ctrl;
395 bool rx_csum_en;
396
397 rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
398
399 rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
400
401 /* enable rx checksumming */
402 if (rx_csum_en)
403 rbuf_chk_ctrl |= RBUF_RXCHK_EN;
404 else
405 rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
406 priv->desc_rxchk_en = rx_csum_en;
Florian Fainelliebe5e3c2014-03-26 21:18:39 -0700407
408 /* If UniMAC forwards CRC, we need to skip over it to get
409 * a valid CHK bit to be set in the per-packet status word
410 */
411 if (rx_csum_en && priv->crc_fwd_en)
412 rbuf_chk_ctrl |= RBUF_SKIP_FCS;
413 else
414 rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
415
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800416 bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
417
418 return 0;
419}
420
421static int bcmgenet_set_tx_csum(struct net_device *dev,
422 netdev_features_t wanted)
423{
424 struct bcmgenet_priv *priv = netdev_priv(dev);
425 bool desc_64b_en;
426 u32 tbuf_ctrl, rbuf_ctrl;
427
428 tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
429 rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
430
431 desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
432
433 /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
434 if (desc_64b_en) {
435 tbuf_ctrl |= RBUF_64B_EN;
436 rbuf_ctrl |= RBUF_64B_EN;
437 } else {
438 tbuf_ctrl &= ~RBUF_64B_EN;
439 rbuf_ctrl &= ~RBUF_64B_EN;
440 }
441 priv->desc_64b_en = desc_64b_en;
442
443 bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
444 bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
445
446 return 0;
447}
448
449static int bcmgenet_set_features(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700450 netdev_features_t features)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800451{
452 netdev_features_t changed = features ^ dev->features;
453 netdev_features_t wanted = dev->wanted_features;
454 int ret = 0;
455
456 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
457 ret = bcmgenet_set_tx_csum(dev, wanted);
458 if (changed & (NETIF_F_RXCSUM))
459 ret = bcmgenet_set_rx_csum(dev, wanted);
460
461 return ret;
462}
463
464static u32 bcmgenet_get_msglevel(struct net_device *dev)
465{
466 struct bcmgenet_priv *priv = netdev_priv(dev);
467
468 return priv->msg_enable;
469}
470
471static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
472{
473 struct bcmgenet_priv *priv = netdev_priv(dev);
474
475 priv->msg_enable = level;
476}
477
478/* standard ethtool support functions. */
479enum bcmgenet_stat_type {
480 BCMGENET_STAT_NETDEV = -1,
481 BCMGENET_STAT_MIB_RX,
482 BCMGENET_STAT_MIB_TX,
483 BCMGENET_STAT_RUNT,
484 BCMGENET_STAT_MISC,
485};
486
487struct bcmgenet_stats {
488 char stat_string[ETH_GSTRING_LEN];
489 int stat_sizeof;
490 int stat_offset;
491 enum bcmgenet_stat_type type;
492 /* reg offset from UMAC base for misc counters */
493 u16 reg_offset;
494};
495
496#define STAT_NETDEV(m) { \
497 .stat_string = __stringify(m), \
498 .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
499 .stat_offset = offsetof(struct net_device_stats, m), \
500 .type = BCMGENET_STAT_NETDEV, \
501}
502
503#define STAT_GENET_MIB(str, m, _type) { \
504 .stat_string = str, \
505 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
506 .stat_offset = offsetof(struct bcmgenet_priv, m), \
507 .type = _type, \
508}
509
510#define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
511#define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
512#define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
513
514#define STAT_GENET_MISC(str, m, offset) { \
515 .stat_string = str, \
516 .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
517 .stat_offset = offsetof(struct bcmgenet_priv, m), \
518 .type = BCMGENET_STAT_MISC, \
519 .reg_offset = offset, \
520}
521
522
523/* There is a 0xC gap between the end of RX and beginning of TX stats and then
524 * between the end of TX stats and the beginning of the RX RUNT
525 */
526#define BCMGENET_STAT_OFFSET 0xc
527
528/* Hardware counters must be kept in sync because the order/offset
529 * is important here (order in structure declaration = order in hardware)
530 */
531static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
532 /* general stats */
533 STAT_NETDEV(rx_packets),
534 STAT_NETDEV(tx_packets),
535 STAT_NETDEV(rx_bytes),
536 STAT_NETDEV(tx_bytes),
537 STAT_NETDEV(rx_errors),
538 STAT_NETDEV(tx_errors),
539 STAT_NETDEV(rx_dropped),
540 STAT_NETDEV(tx_dropped),
541 STAT_NETDEV(multicast),
542 /* UniMAC RSV counters */
543 STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
544 STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
545 STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
546 STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
547 STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
548 STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
549 STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
550 STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
551 STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
552 STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
553 STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
554 STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
555 STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
556 STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
557 STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
558 STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
559 STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
560 STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
561 STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
562 STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
563 STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
564 STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
565 STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
566 STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
567 STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
568 STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
569 STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
570 STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
571 STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
572 /* UniMAC TSV counters */
573 STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
574 STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
575 STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
576 STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
577 STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
578 STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
579 STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
580 STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
581 STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
582 STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
583 STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
584 STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
585 STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
586 STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
587 STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
588 STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
589 STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
590 STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
591 STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
592 STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
593 STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
594 STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
595 STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
596 STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
597 STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
598 STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
599 STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
600 STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
601 STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
602 /* UniMAC RUNT counters */
603 STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
604 STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
605 STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
606 STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
607 /* Misc UniMAC counters */
608 STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
609 UMAC_RBUF_OVFL_CNT),
610 STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt, UMAC_RBUF_ERR_CNT),
611 STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
612};
613
614#define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
615
616static void bcmgenet_get_drvinfo(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700617 struct ethtool_drvinfo *info)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800618{
619 strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
620 strlcpy(info->version, "v2.0", sizeof(info->version));
621 info->n_stats = BCMGENET_STATS_LEN;
622
623}
624
625static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
626{
627 switch (string_set) {
628 case ETH_SS_STATS:
629 return BCMGENET_STATS_LEN;
630 default:
631 return -EOPNOTSUPP;
632 }
633}
634
Florian Fainellic91b7f62014-07-23 10:42:12 -0700635static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
636 u8 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800637{
638 int i;
639
640 switch (stringset) {
641 case ETH_SS_STATS:
642 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
643 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700644 bcmgenet_gstrings_stats[i].stat_string,
645 ETH_GSTRING_LEN);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800646 }
647 break;
648 }
649}
650
651static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
652{
653 int i, j = 0;
654
655 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
656 const struct bcmgenet_stats *s;
657 u8 offset = 0;
658 u32 val = 0;
659 char *p;
660
661 s = &bcmgenet_gstrings_stats[i];
662 switch (s->type) {
663 case BCMGENET_STAT_NETDEV:
664 continue;
665 case BCMGENET_STAT_MIB_RX:
666 case BCMGENET_STAT_MIB_TX:
667 case BCMGENET_STAT_RUNT:
668 if (s->type != BCMGENET_STAT_MIB_RX)
669 offset = BCMGENET_STAT_OFFSET;
Florian Fainellic91b7f62014-07-23 10:42:12 -0700670 val = bcmgenet_umac_readl(priv,
671 UMAC_MIB_START + j + offset);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800672 break;
673 case BCMGENET_STAT_MISC:
674 val = bcmgenet_umac_readl(priv, s->reg_offset);
675 /* clear if overflowed */
676 if (val == ~0)
677 bcmgenet_umac_writel(priv, 0, s->reg_offset);
678 break;
679 }
680
681 j += s->stat_sizeof;
682 p = (char *)priv + s->stat_offset;
683 *(u32 *)p = val;
684 }
685}
686
687static void bcmgenet_get_ethtool_stats(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700688 struct ethtool_stats *stats,
689 u64 *data)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800690{
691 struct bcmgenet_priv *priv = netdev_priv(dev);
692 int i;
693
694 if (netif_running(dev))
695 bcmgenet_update_mib_counters(priv);
696
697 for (i = 0; i < BCMGENET_STATS_LEN; i++) {
698 const struct bcmgenet_stats *s;
699 char *p;
700
701 s = &bcmgenet_gstrings_stats[i];
702 if (s->type == BCMGENET_STAT_NETDEV)
703 p = (char *)&dev->stats;
704 else
705 p = (char *)priv;
706 p += s->stat_offset;
707 data[i] = *(u32 *)p;
708 }
709}
710
711/* standard ethtool support functions. */
712static struct ethtool_ops bcmgenet_ethtool_ops = {
713 .get_strings = bcmgenet_get_strings,
714 .get_sset_count = bcmgenet_get_sset_count,
715 .get_ethtool_stats = bcmgenet_get_ethtool_stats,
716 .get_settings = bcmgenet_get_settings,
717 .set_settings = bcmgenet_set_settings,
718 .get_drvinfo = bcmgenet_get_drvinfo,
719 .get_link = ethtool_op_get_link,
720 .get_msglevel = bcmgenet_get_msglevel,
721 .set_msglevel = bcmgenet_set_msglevel,
Florian Fainelli06ba8372014-07-21 15:29:29 -0700722 .get_wol = bcmgenet_get_wol,
723 .set_wol = bcmgenet_set_wol,
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800724};
725
726/* Power down the unimac, based on mode. */
727static void bcmgenet_power_down(struct bcmgenet_priv *priv,
728 enum bcmgenet_power_mode mode)
729{
730 u32 reg;
731
732 switch (mode) {
733 case GENET_POWER_CABLE_SENSE:
Florian Fainelli80d8e962014-02-24 16:56:11 -0800734 phy_detach(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800735 break;
736
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700737 case GENET_POWER_WOL_MAGIC:
738 bcmgenet_wol_power_down_cfg(priv, mode);
739 break;
740
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800741 case GENET_POWER_PASSIVE:
742 /* Power down LED */
743 bcmgenet_mii_reset(priv->dev);
744 if (priv->hw_params->flags & GENET_HAS_EXT) {
745 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
746 reg |= (EXT_PWR_DOWN_PHY |
747 EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
748 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
749 }
750 break;
751 default:
752 break;
753 }
754}
755
756static void bcmgenet_power_up(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700757 enum bcmgenet_power_mode mode)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800758{
759 u32 reg;
760
761 if (!(priv->hw_params->flags & GENET_HAS_EXT))
762 return;
763
764 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
765
766 switch (mode) {
767 case GENET_POWER_PASSIVE:
768 reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_PHY |
769 EXT_PWR_DOWN_BIAS);
770 /* fallthrough */
771 case GENET_POWER_CABLE_SENSE:
772 /* enable APD */
773 reg |= EXT_PWR_DN_EN_LD;
774 break;
Florian Fainellic3ae64a2014-07-21 15:29:25 -0700775 case GENET_POWER_WOL_MAGIC:
776 bcmgenet_wol_power_up_cfg(priv, mode);
777 return;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800778 default:
779 break;
780 }
781
782 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
783 bcmgenet_mii_reset(priv->dev);
784}
785
786/* ioctl handle special commands that are not present in ethtool. */
787static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
788{
789 struct bcmgenet_priv *priv = netdev_priv(dev);
790 int val = 0;
791
792 if (!netif_running(dev))
793 return -EINVAL;
794
795 switch (cmd) {
796 case SIOCGMIIPHY:
797 case SIOCGMIIREG:
798 case SIOCSMIIREG:
799 if (!priv->phydev)
800 val = -ENODEV;
801 else
802 val = phy_mii_ioctl(priv->phydev, rq, cmd);
803 break;
804
805 default:
806 val = -EINVAL;
807 break;
808 }
809
810 return val;
811}
812
813static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
814 struct bcmgenet_tx_ring *ring)
815{
816 struct enet_cb *tx_cb_ptr;
817
818 tx_cb_ptr = ring->cbs;
819 tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
820 tx_cb_ptr->bd_addr = priv->tx_bds + ring->write_ptr * DMA_DESC_SIZE;
821 /* Advancing local write pointer */
822 if (ring->write_ptr == ring->end_ptr)
823 ring->write_ptr = ring->cb_ptr;
824 else
825 ring->write_ptr++;
826
827 return tx_cb_ptr;
828}
829
830/* Simple helper to free a control block's resources */
831static void bcmgenet_free_cb(struct enet_cb *cb)
832{
833 dev_kfree_skb_any(cb->skb);
834 cb->skb = NULL;
835 dma_unmap_addr_set(cb, dma_addr, 0);
836}
837
838static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_priv *priv,
839 struct bcmgenet_tx_ring *ring)
840{
841 bcmgenet_intrl2_0_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700842 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
843 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800844}
845
846static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_priv *priv,
847 struct bcmgenet_tx_ring *ring)
848{
849 bcmgenet_intrl2_0_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700850 UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE,
851 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800852}
853
854static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700855 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800856{
Florian Fainellic91b7f62014-07-23 10:42:12 -0700857 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
858 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800859 priv->int1_mask &= ~(1 << ring->index);
860}
861
862static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_priv *priv,
863 struct bcmgenet_tx_ring *ring)
864{
Florian Fainellic91b7f62014-07-23 10:42:12 -0700865 bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
866 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800867 priv->int1_mask |= (1 << ring->index);
868}
869
870/* Unlocked version of the reclaim routine */
871static void __bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700872 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800873{
874 struct bcmgenet_priv *priv = netdev_priv(dev);
875 int last_tx_cn, last_c_index, num_tx_bds;
876 struct enet_cb *tx_cb_ptr;
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700877 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800878 unsigned int c_index;
879
880 /* Compute how many buffers are transmited since last xmit call */
881 c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700882 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800883
884 last_c_index = ring->c_index;
885 num_tx_bds = ring->size;
886
887 c_index &= (num_tx_bds - 1);
888
889 if (c_index >= last_c_index)
890 last_tx_cn = c_index - last_c_index;
891 else
892 last_tx_cn = num_tx_bds - last_c_index + c_index;
893
894 netif_dbg(priv, tx_done, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700895 "%s ring=%d index=%d last_tx_cn=%d last_index=%d\n",
896 __func__, ring->index,
897 c_index, last_tx_cn, last_c_index);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800898
899 /* Reclaim transmitted buffers */
900 while (last_tx_cn-- > 0) {
901 tx_cb_ptr = ring->cbs + last_c_index;
902 if (tx_cb_ptr->skb) {
903 dev->stats.tx_bytes += tx_cb_ptr->skb->len;
904 dma_unmap_single(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700905 dma_unmap_addr(tx_cb_ptr, dma_addr),
906 tx_cb_ptr->skb->len,
907 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800908 bcmgenet_free_cb(tx_cb_ptr);
909 } else if (dma_unmap_addr(tx_cb_ptr, dma_addr)) {
910 dev->stats.tx_bytes +=
911 dma_unmap_len(tx_cb_ptr, dma_len);
912 dma_unmap_page(&dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700913 dma_unmap_addr(tx_cb_ptr, dma_addr),
914 dma_unmap_len(tx_cb_ptr, dma_len),
915 DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800916 dma_unmap_addr_set(tx_cb_ptr, dma_addr, 0);
917 }
918 dev->stats.tx_packets++;
919 ring->free_bds += 1;
920
921 last_c_index++;
922 last_c_index &= (num_tx_bds - 1);
923 }
924
925 if (ring->free_bds > (MAX_SKB_FRAGS + 1))
926 ring->int_disable(priv, ring);
927
Florian Fainellib2cde2c2014-03-20 10:53:23 -0700928 if (netif_tx_queue_stopped(txq))
929 netif_tx_wake_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800930
931 ring->c_index = c_index;
932}
933
934static void bcmgenet_tx_reclaim(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700935 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -0800936{
937 unsigned long flags;
938
939 spin_lock_irqsave(&ring->lock, flags);
940 __bcmgenet_tx_reclaim(dev, ring);
941 spin_unlock_irqrestore(&ring->lock, flags);
942}
943
944static void bcmgenet_tx_reclaim_all(struct net_device *dev)
945{
946 struct bcmgenet_priv *priv = netdev_priv(dev);
947 int i;
948
949 if (netif_is_multiqueue(dev)) {
950 for (i = 0; i < priv->hw_params->tx_queues; i++)
951 bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
952 }
953
954 bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
955}
956
957/* Transmits a single SKB (either head of a fragment or a single SKB)
958 * caller must hold priv->lock
959 */
960static int bcmgenet_xmit_single(struct net_device *dev,
961 struct sk_buff *skb,
962 u16 dma_desc_flags,
963 struct bcmgenet_tx_ring *ring)
964{
965 struct bcmgenet_priv *priv = netdev_priv(dev);
966 struct device *kdev = &priv->pdev->dev;
967 struct enet_cb *tx_cb_ptr;
968 unsigned int skb_len;
969 dma_addr_t mapping;
970 u32 length_status;
971 int ret;
972
973 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
974
975 if (unlikely(!tx_cb_ptr))
976 BUG();
977
978 tx_cb_ptr->skb = skb;
979
980 skb_len = skb_headlen(skb) < ETH_ZLEN ? ETH_ZLEN : skb_headlen(skb);
981
982 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
983 ret = dma_mapping_error(kdev, mapping);
984 if (ret) {
985 netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
986 dev_kfree_skb(skb);
987 return ret;
988 }
989
990 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
991 dma_unmap_len_set(tx_cb_ptr, dma_len, skb->len);
992 length_status = (skb_len << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
993 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT) |
994 DMA_TX_APPEND_CRC;
995
996 if (skb->ip_summed == CHECKSUM_PARTIAL)
997 length_status |= DMA_TX_DO_CSUM;
998
999 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, length_status);
1000
1001 /* Decrement total BD count and advance our write pointer */
1002 ring->free_bds -= 1;
1003 ring->prod_index += 1;
1004 ring->prod_index &= DMA_P_INDEX_MASK;
1005
1006 return 0;
1007}
1008
1009/* Transmit a SKB fragement */
1010static int bcmgenet_xmit_frag(struct net_device *dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001011 skb_frag_t *frag,
1012 u16 dma_desc_flags,
1013 struct bcmgenet_tx_ring *ring)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001014{
1015 struct bcmgenet_priv *priv = netdev_priv(dev);
1016 struct device *kdev = &priv->pdev->dev;
1017 struct enet_cb *tx_cb_ptr;
1018 dma_addr_t mapping;
1019 int ret;
1020
1021 tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
1022
1023 if (unlikely(!tx_cb_ptr))
1024 BUG();
1025 tx_cb_ptr->skb = NULL;
1026
1027 mapping = skb_frag_dma_map(kdev, frag, 0,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001028 skb_frag_size(frag), DMA_TO_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001029 ret = dma_mapping_error(kdev, mapping);
1030 if (ret) {
1031 netif_err(priv, tx_err, dev, "%s: Tx DMA map failed\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001032 __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001033 return ret;
1034 }
1035
1036 dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
1037 dma_unmap_len_set(tx_cb_ptr, dma_len, frag->size);
1038
1039 dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001040 (frag->size << DMA_BUFLENGTH_SHIFT) | dma_desc_flags |
1041 (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001042
1043
1044 ring->free_bds -= 1;
1045 ring->prod_index += 1;
1046 ring->prod_index &= DMA_P_INDEX_MASK;
1047
1048 return 0;
1049}
1050
1051/* Reallocate the SKB to put enough headroom in front of it and insert
1052 * the transmit checksum offsets in the descriptors
1053 */
1054static int bcmgenet_put_tx_csum(struct net_device *dev, struct sk_buff *skb)
1055{
1056 struct status_64 *status = NULL;
1057 struct sk_buff *new_skb;
1058 u16 offset;
1059 u8 ip_proto;
1060 u16 ip_ver;
1061 u32 tx_csum_info;
1062
1063 if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1064 /* If 64 byte status block enabled, must make sure skb has
1065 * enough headroom for us to insert 64B status block.
1066 */
1067 new_skb = skb_realloc_headroom(skb, sizeof(*status));
1068 dev_kfree_skb(skb);
1069 if (!new_skb) {
1070 dev->stats.tx_errors++;
1071 dev->stats.tx_dropped++;
1072 return -ENOMEM;
1073 }
1074 skb = new_skb;
1075 }
1076
1077 skb_push(skb, sizeof(*status));
1078 status = (struct status_64 *)skb->data;
1079
1080 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1081 ip_ver = htons(skb->protocol);
1082 switch (ip_ver) {
1083 case ETH_P_IP:
1084 ip_proto = ip_hdr(skb)->protocol;
1085 break;
1086 case ETH_P_IPV6:
1087 ip_proto = ipv6_hdr(skb)->nexthdr;
1088 break;
1089 default:
1090 return 0;
1091 }
1092
1093 offset = skb_checksum_start_offset(skb) - sizeof(*status);
1094 tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1095 (offset + skb->csum_offset);
1096
1097 /* Set the length valid bit for TCP and UDP and just set
1098 * the special UDP flag for IPv4, else just set to 0.
1099 */
1100 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1101 tx_csum_info |= STATUS_TX_CSUM_LV;
1102 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1103 tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
1104 } else
1105 tx_csum_info = 0;
1106
1107 status->tx_csum_info = tx_csum_info;
1108 }
1109
1110 return 0;
1111}
1112
1113static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1114{
1115 struct bcmgenet_priv *priv = netdev_priv(dev);
1116 struct bcmgenet_tx_ring *ring = NULL;
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001117 struct netdev_queue *txq;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001118 unsigned long flags = 0;
1119 int nr_frags, index;
1120 u16 dma_desc_flags;
1121 int ret;
1122 int i;
1123
1124 index = skb_get_queue_mapping(skb);
1125 /* Mapping strategy:
1126 * queue_mapping = 0, unclassified, packet xmited through ring16
1127 * queue_mapping = 1, goes to ring 0. (highest priority queue
1128 * queue_mapping = 2, goes to ring 1.
1129 * queue_mapping = 3, goes to ring 2.
1130 * queue_mapping = 4, goes to ring 3.
1131 */
1132 if (index == 0)
1133 index = DESC_INDEX;
1134 else
1135 index -= 1;
1136
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001137 nr_frags = skb_shinfo(skb)->nr_frags;
1138 ring = &priv->tx_rings[index];
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001139 txq = netdev_get_tx_queue(dev, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001140
1141 spin_lock_irqsave(&ring->lock, flags);
1142 if (ring->free_bds <= nr_frags + 1) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001143 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001144 netdev_err(dev, "%s: tx ring %d full when queue %d awake\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001145 __func__, index, ring->queue);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001146 ret = NETDEV_TX_BUSY;
1147 goto out;
1148 }
1149
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001150 /* set the SKB transmit checksum */
1151 if (priv->desc_64b_en) {
1152 ret = bcmgenet_put_tx_csum(dev, skb);
1153 if (ret) {
1154 ret = NETDEV_TX_OK;
1155 goto out;
1156 }
1157 }
1158
1159 dma_desc_flags = DMA_SOP;
1160 if (nr_frags == 0)
1161 dma_desc_flags |= DMA_EOP;
1162
1163 /* Transmit single SKB or head of fragment list */
1164 ret = bcmgenet_xmit_single(dev, skb, dma_desc_flags, ring);
1165 if (ret) {
1166 ret = NETDEV_TX_OK;
1167 goto out;
1168 }
1169
1170 /* xmit fragment */
1171 for (i = 0; i < nr_frags; i++) {
1172 ret = bcmgenet_xmit_frag(dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001173 &skb_shinfo(skb)->frags[i],
1174 (i == nr_frags - 1) ? DMA_EOP : 0,
1175 ring);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001176 if (ret) {
1177 ret = NETDEV_TX_OK;
1178 goto out;
1179 }
1180 }
1181
Florian Fainellid03825f2014-03-20 10:53:21 -07001182 skb_tx_timestamp(skb);
1183
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001184 /* we kept a software copy of how much we should advance the TDMA
1185 * producer index, now write it down to the hardware
1186 */
1187 bcmgenet_tdma_ring_writel(priv, ring->index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001188 ring->prod_index, TDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001189
1190 if (ring->free_bds <= (MAX_SKB_FRAGS + 1)) {
Florian Fainellib2cde2c2014-03-20 10:53:23 -07001191 netif_tx_stop_queue(txq);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001192 ring->int_enable(priv, ring);
1193 }
1194
1195out:
1196 spin_unlock_irqrestore(&ring->lock, flags);
1197
1198 return ret;
1199}
1200
1201
Florian Fainellic91b7f62014-07-23 10:42:12 -07001202static int bcmgenet_rx_refill(struct bcmgenet_priv *priv, struct enet_cb *cb)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001203{
1204 struct device *kdev = &priv->pdev->dev;
1205 struct sk_buff *skb;
1206 dma_addr_t mapping;
1207 int ret;
1208
Florian Fainellic91b7f62014-07-23 10:42:12 -07001209 skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001210 if (!skb)
1211 return -ENOMEM;
1212
1213 /* a caller did not release this control block */
1214 WARN_ON(cb->skb != NULL);
1215 cb->skb = skb;
1216 mapping = dma_map_single(kdev, skb->data,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001217 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001218 ret = dma_mapping_error(kdev, mapping);
1219 if (ret) {
1220 bcmgenet_free_cb(cb);
1221 netif_err(priv, rx_err, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001222 "%s DMA map failed\n", __func__);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001223 return ret;
1224 }
1225
1226 dma_unmap_addr_set(cb, dma_addr, mapping);
1227 /* assign packet, prepare descriptor, and advance pointer */
1228
1229 dmadesc_set_addr(priv, priv->rx_bd_assign_ptr, mapping);
1230
1231 /* turn on the newly assigned BD for DMA to use */
1232 priv->rx_bd_assign_index++;
1233 priv->rx_bd_assign_index &= (priv->num_rx_bds - 1);
1234
1235 priv->rx_bd_assign_ptr = priv->rx_bds +
1236 (priv->rx_bd_assign_index * DMA_DESC_SIZE);
1237
1238 return 0;
1239}
1240
1241/* bcmgenet_desc_rx - descriptor based rx process.
1242 * this could be called from bottom half, or from NAPI polling method.
1243 */
1244static unsigned int bcmgenet_desc_rx(struct bcmgenet_priv *priv,
1245 unsigned int budget)
1246{
1247 struct net_device *dev = priv->dev;
1248 struct enet_cb *cb;
1249 struct sk_buff *skb;
1250 u32 dma_length_status;
1251 unsigned long dma_flag;
1252 int len, err;
1253 unsigned int rxpktprocessed = 0, rxpkttoprocess;
1254 unsigned int p_index;
1255 unsigned int chksum_ok = 0;
1256
Florian Fainellic91b7f62014-07-23 10:42:12 -07001257 p_index = bcmgenet_rdma_ring_readl(priv, DESC_INDEX, RDMA_PROD_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001258 p_index &= DMA_P_INDEX_MASK;
1259
1260 if (p_index < priv->rx_c_index)
1261 rxpkttoprocess = (DMA_C_INDEX_MASK + 1) -
1262 priv->rx_c_index + p_index;
1263 else
1264 rxpkttoprocess = p_index - priv->rx_c_index;
1265
1266 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001267 "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001268
1269 while ((rxpktprocessed < rxpkttoprocess) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001270 (rxpktprocessed < budget)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001271
1272 /* Unmap the packet contents such that we can use the
1273 * RSV from the 64 bytes descriptor when enabled and save
1274 * a 32-bits register read
1275 */
1276 cb = &priv->rx_cbs[priv->rx_read_ptr];
1277 skb = cb->skb;
1278 dma_unmap_single(&dev->dev, dma_unmap_addr(cb, dma_addr),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001279 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001280
1281 if (!priv->desc_64b_en) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001282 dma_length_status =
1283 dmadesc_get_length_status(priv,
1284 priv->rx_bds +
1285 (priv->rx_read_ptr *
1286 DMA_DESC_SIZE));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001287 } else {
1288 struct status_64 *status;
1289 status = (struct status_64 *)skb->data;
1290 dma_length_status = status->length_status;
1291 }
1292
1293 /* DMA flags and length are still valid no matter how
1294 * we got the Receive Status Vector (64B RSB or register)
1295 */
1296 dma_flag = dma_length_status & 0xffff;
1297 len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
1298
1299 netif_dbg(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001300 "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
1301 __func__, p_index, priv->rx_c_index,
1302 priv->rx_read_ptr, dma_length_status);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001303
1304 rxpktprocessed++;
1305
1306 priv->rx_read_ptr++;
1307 priv->rx_read_ptr &= (priv->num_rx_bds - 1);
1308
1309 /* out of memory, just drop packets at the hardware level */
1310 if (unlikely(!skb)) {
1311 dev->stats.rx_dropped++;
1312 dev->stats.rx_errors++;
1313 goto refill;
1314 }
1315
1316 if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
1317 netif_err(priv, rx_status, dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001318 "dropping fragmented packet!\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001319 dev->stats.rx_dropped++;
1320 dev->stats.rx_errors++;
1321 dev_kfree_skb_any(cb->skb);
1322 cb->skb = NULL;
1323 goto refill;
1324 }
1325 /* report errors */
1326 if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
1327 DMA_RX_OV |
1328 DMA_RX_NO |
1329 DMA_RX_LG |
1330 DMA_RX_RXER))) {
1331 netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
Florian Fainellic91b7f62014-07-23 10:42:12 -07001332 (unsigned int)dma_flag);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001333 if (dma_flag & DMA_RX_CRC_ERROR)
1334 dev->stats.rx_crc_errors++;
1335 if (dma_flag & DMA_RX_OV)
1336 dev->stats.rx_over_errors++;
1337 if (dma_flag & DMA_RX_NO)
1338 dev->stats.rx_frame_errors++;
1339 if (dma_flag & DMA_RX_LG)
1340 dev->stats.rx_length_errors++;
1341 dev->stats.rx_dropped++;
1342 dev->stats.rx_errors++;
1343
1344 /* discard the packet and advance consumer index.*/
1345 dev_kfree_skb_any(cb->skb);
1346 cb->skb = NULL;
1347 goto refill;
1348 } /* error packet */
1349
1350 chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001351 priv->desc_rxchk_en;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001352
1353 skb_put(skb, len);
1354 if (priv->desc_64b_en) {
1355 skb_pull(skb, 64);
1356 len -= 64;
1357 }
1358
1359 if (likely(chksum_ok))
1360 skb->ip_summed = CHECKSUM_UNNECESSARY;
1361
1362 /* remove hardware 2bytes added for IP alignment */
1363 skb_pull(skb, 2);
1364 len -= 2;
1365
1366 if (priv->crc_fwd_en) {
1367 skb_trim(skb, len - ETH_FCS_LEN);
1368 len -= ETH_FCS_LEN;
1369 }
1370
1371 /*Finish setting up the received SKB and send it to the kernel*/
1372 skb->protocol = eth_type_trans(skb, priv->dev);
1373 dev->stats.rx_packets++;
1374 dev->stats.rx_bytes += len;
1375 if (dma_flag & DMA_RX_MULT)
1376 dev->stats.multicast++;
1377
1378 /* Notify kernel */
1379 napi_gro_receive(&priv->napi, skb);
1380 cb->skb = NULL;
1381 netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
1382
1383 /* refill RX path on the current control block */
1384refill:
1385 err = bcmgenet_rx_refill(priv, cb);
1386 if (err)
1387 netif_err(priv, rx_err, dev, "Rx refill failed\n");
1388 }
1389
1390 return rxpktprocessed;
1391}
1392
1393/* Assign skb to RX DMA descriptor. */
1394static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv)
1395{
1396 struct enet_cb *cb;
1397 int ret = 0;
1398 int i;
1399
1400 netif_dbg(priv, hw, priv->dev, "%s:\n", __func__);
1401
1402 /* loop here for each buffer needing assign */
1403 for (i = 0; i < priv->num_rx_bds; i++) {
1404 cb = &priv->rx_cbs[priv->rx_bd_assign_index];
1405 if (cb->skb)
1406 continue;
1407
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001408 ret = bcmgenet_rx_refill(priv, cb);
1409 if (ret)
1410 break;
1411
1412 }
1413
1414 return ret;
1415}
1416
1417static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
1418{
1419 struct enet_cb *cb;
1420 int i;
1421
1422 for (i = 0; i < priv->num_rx_bds; i++) {
1423 cb = &priv->rx_cbs[i];
1424
1425 if (dma_unmap_addr(cb, dma_addr)) {
1426 dma_unmap_single(&priv->dev->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001427 dma_unmap_addr(cb, dma_addr),
1428 priv->rx_buf_len, DMA_FROM_DEVICE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001429 dma_unmap_addr_set(cb, dma_addr, 0);
1430 }
1431
1432 if (cb->skb)
1433 bcmgenet_free_cb(cb);
1434 }
1435}
1436
Florian Fainellic91b7f62014-07-23 10:42:12 -07001437static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
Florian Fainellie29585b2014-07-21 15:29:20 -07001438{
1439 u32 reg;
1440
1441 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1442 if (enable)
1443 reg |= mask;
1444 else
1445 reg &= ~mask;
1446 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
1447
1448 /* UniMAC stops on a packet boundary, wait for a full-size packet
1449 * to be processed
1450 */
1451 if (enable == 0)
1452 usleep_range(1000, 2000);
1453}
1454
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001455static int reset_umac(struct bcmgenet_priv *priv)
1456{
1457 struct device *kdev = &priv->pdev->dev;
1458 unsigned int timeout = 0;
1459 u32 reg;
1460
1461 /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
1462 bcmgenet_rbuf_ctrl_set(priv, 0);
1463 udelay(10);
1464
1465 /* disable MAC while updating its registers */
1466 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1467
1468 /* issue soft reset, wait for it to complete */
1469 bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
1470 while (timeout++ < 1000) {
1471 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
1472 if (!(reg & CMD_SW_RESET))
1473 return 0;
1474
1475 udelay(1);
1476 }
1477
1478 if (timeout == 1000) {
1479 dev_err(kdev,
1480 "timeout waiting for MAC to come out of resetn\n");
1481 return -ETIMEDOUT;
1482 }
1483
1484 return 0;
1485}
1486
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001487static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
1488{
1489 /* Mask all interrupts.*/
1490 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1491 bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1492 bcmgenet_intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1493 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
1494 bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
1495 bcmgenet_intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1496}
1497
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001498static int init_umac(struct bcmgenet_priv *priv)
1499{
1500 struct device *kdev = &priv->pdev->dev;
1501 int ret;
1502 u32 reg, cpu_mask_clear;
1503
1504 dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
1505
1506 ret = reset_umac(priv);
1507 if (ret)
1508 return ret;
1509
1510 bcmgenet_umac_writel(priv, 0, UMAC_CMD);
1511 /* clear tx/rx counter */
1512 bcmgenet_umac_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001513 MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
1514 UMAC_MIB_CTRL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001515 bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
1516
1517 bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1518
1519 /* init rx registers, enable ip header optimization */
1520 reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
1521 reg |= RBUF_ALIGN_2B;
1522 bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
1523
1524 if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
1525 bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
1526
Florian Fainelli909ff5e2014-07-21 15:29:21 -07001527 bcmgenet_intr_disable(priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001528
1529 cpu_mask_clear = UMAC_IRQ_RXDMA_BDONE;
1530
1531 dev_dbg(kdev, "%s:Enabling RXDMA_BDONE interrupt\n", __func__);
1532
1533 /* Monitor cable plug/unpluged event for internal PHY */
1534 if (phy_is_internal(priv->phydev))
1535 cpu_mask_clear |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
1536 else if (priv->ext_phy)
1537 cpu_mask_clear |= (UMAC_IRQ_LINK_DOWN | UMAC_IRQ_LINK_UP);
1538 else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
1539 reg = bcmgenet_bp_mc_get(priv);
1540 reg |= BIT(priv->hw_params->bp_in_en_shift);
1541
1542 /* bp_mask: back pressure mask */
1543 if (netif_is_multiqueue(priv->dev))
1544 reg |= priv->hw_params->bp_in_mask;
1545 else
1546 reg &= ~priv->hw_params->bp_in_mask;
1547 bcmgenet_bp_mc_set(priv, reg);
1548 }
1549
1550 /* Enable MDIO interrupts on GENET v3+ */
1551 if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
1552 cpu_mask_clear |= UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR;
1553
Florian Fainellic91b7f62014-07-23 10:42:12 -07001554 bcmgenet_intrl2_0_writel(priv, cpu_mask_clear, INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001555
1556 /* Enable rx/tx engine.*/
1557 dev_dbg(kdev, "done init umac\n");
1558
1559 return 0;
1560}
1561
1562/* Initialize all house-keeping variables for a TX ring, along
1563 * with corresponding hardware registers
1564 */
1565static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
1566 unsigned int index, unsigned int size,
1567 unsigned int write_ptr, unsigned int end_ptr)
1568{
1569 struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
1570 u32 words_per_bd = WORDS_PER_BD(priv);
1571 u32 flow_period_val = 0;
1572 unsigned int first_bd;
1573
1574 spin_lock_init(&ring->lock);
1575 ring->index = index;
1576 if (index == DESC_INDEX) {
1577 ring->queue = 0;
1578 ring->int_enable = bcmgenet_tx_ring16_int_enable;
1579 ring->int_disable = bcmgenet_tx_ring16_int_disable;
1580 } else {
1581 ring->queue = index + 1;
1582 ring->int_enable = bcmgenet_tx_ring_int_enable;
1583 ring->int_disable = bcmgenet_tx_ring_int_disable;
1584 }
1585 ring->cbs = priv->tx_cbs + write_ptr;
1586 ring->size = size;
1587 ring->c_index = 0;
1588 ring->free_bds = size;
1589 ring->write_ptr = write_ptr;
1590 ring->cb_ptr = write_ptr;
1591 ring->end_ptr = end_ptr - 1;
1592 ring->prod_index = 0;
1593
1594 /* Set flow period for ring != 16 */
1595 if (index != DESC_INDEX)
1596 flow_period_val = ENET_MAX_MTU_SIZE << 16;
1597
1598 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
1599 bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
1600 bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
1601 /* Disable rate control for now */
1602 bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001603 TDMA_FLOW_PERIOD);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001604 /* Unclassified traffic goes to ring 16 */
1605 bcmgenet_tdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001606 ((size << DMA_RING_SIZE_SHIFT) |
1607 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001608
1609 first_bd = write_ptr;
1610
1611 /* Set start and end address, read and write pointers */
1612 bcmgenet_tdma_ring_writel(priv, index, first_bd * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001613 DMA_START_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001614 bcmgenet_tdma_ring_writel(priv, index, first_bd * words_per_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001615 TDMA_READ_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001616 bcmgenet_tdma_ring_writel(priv, index, first_bd,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001617 TDMA_WRITE_PTR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001618 bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001619 DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001620}
1621
1622/* Initialize a RDMA ring */
1623static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001624 unsigned int index, unsigned int size)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001625{
1626 u32 words_per_bd = WORDS_PER_BD(priv);
1627 int ret;
1628
1629 priv->num_rx_bds = TOTAL_DESC;
1630 priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
1631 priv->rx_bd_assign_ptr = priv->rx_bds;
1632 priv->rx_bd_assign_index = 0;
1633 priv->rx_c_index = 0;
1634 priv->rx_read_ptr = 0;
1635 priv->rx_cbs = kzalloc(priv->num_rx_bds * sizeof(struct enet_cb),
1636 GFP_KERNEL);
1637 if (!priv->rx_cbs)
1638 return -ENOMEM;
1639
1640 ret = bcmgenet_alloc_rx_buffers(priv);
1641 if (ret) {
1642 kfree(priv->rx_cbs);
1643 return ret;
1644 }
1645
1646 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_WRITE_PTR);
1647 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
1648 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
1649 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001650 ((size << DMA_RING_SIZE_SHIFT) |
1651 RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001652 bcmgenet_rdma_ring_writel(priv, index, 0, DMA_START_ADDR);
1653 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001654 words_per_bd * size - 1, DMA_END_ADDR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001655 bcmgenet_rdma_ring_writel(priv, index,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001656 (DMA_FC_THRESH_LO <<
1657 DMA_XOFF_THRESHOLD_SHIFT) |
1658 DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001659 bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_READ_PTR);
1660
1661 return ret;
1662}
1663
1664/* init multi xmit queues, only available for GENET2+
1665 * the queue is partitioned as follows:
1666 *
1667 * queue 0 - 3 is priority based, each one has 32 descriptors,
1668 * with queue 0 being the highest priority queue.
1669 *
1670 * queue 16 is the default tx queue with GENET_DEFAULT_BD_CNT
1671 * descriptors: 256 - (number of tx queues * bds per queues) = 128
1672 * descriptors.
1673 *
1674 * The transmit control block pool is then partitioned as following:
1675 * - tx_cbs[0...127] are for queue 16
1676 * - tx_ring_cbs[0] points to tx_cbs[128..159]
1677 * - tx_ring_cbs[1] points to tx_cbs[160..191]
1678 * - tx_ring_cbs[2] points to tx_cbs[192..223]
1679 * - tx_ring_cbs[3] points to tx_cbs[224..255]
1680 */
1681static void bcmgenet_init_multiq(struct net_device *dev)
1682{
1683 struct bcmgenet_priv *priv = netdev_priv(dev);
1684 unsigned int i, dma_enable;
1685 u32 reg, dma_ctrl, ring_cfg = 0, dma_priority = 0;
1686
1687 if (!netif_is_multiqueue(dev)) {
1688 netdev_warn(dev, "called with non multi queue aware HW\n");
1689 return;
1690 }
1691
1692 dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
1693 dma_enable = dma_ctrl & DMA_EN;
1694 dma_ctrl &= ~DMA_EN;
1695 bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
1696
1697 /* Enable strict priority arbiter mode */
1698 bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
1699
1700 for (i = 0; i < priv->hw_params->tx_queues; i++) {
1701 /* first 64 tx_cbs are reserved for default tx queue
1702 * (ring 16)
1703 */
1704 bcmgenet_init_tx_ring(priv, i, priv->hw_params->bds_cnt,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001705 i * priv->hw_params->bds_cnt,
1706 (i + 1) * priv->hw_params->bds_cnt);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001707
1708 /* Configure ring as decriptor ring and setup priority */
1709 ring_cfg |= 1 << i;
1710 dma_priority |= ((GENET_Q0_PRIORITY + i) <<
1711 (GENET_MAX_MQ_CNT + 1) * i);
1712 dma_ctrl |= 1 << (i + DMA_RING_BUF_EN_SHIFT);
1713 }
1714
1715 /* Enable rings */
1716 reg = bcmgenet_tdma_readl(priv, DMA_RING_CFG);
1717 reg |= ring_cfg;
1718 bcmgenet_tdma_writel(priv, reg, DMA_RING_CFG);
1719
1720 /* Use configured rings priority and set ring #16 priority */
1721 reg = bcmgenet_tdma_readl(priv, DMA_RING_PRIORITY);
1722 reg |= ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) << 20);
1723 reg |= dma_priority;
1724 bcmgenet_tdma_writel(priv, reg, DMA_PRIORITY);
1725
1726 /* Configure ring as descriptor ring and re-enable DMA if enabled */
1727 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
1728 reg |= dma_ctrl;
1729 if (dma_enable)
1730 reg |= DMA_EN;
1731 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
1732}
1733
1734static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
1735{
1736 int i;
1737
1738 /* disable DMA */
1739 bcmgenet_rdma_writel(priv, 0, DMA_CTRL);
1740 bcmgenet_tdma_writel(priv, 0, DMA_CTRL);
1741
1742 for (i = 0; i < priv->num_tx_bds; i++) {
1743 if (priv->tx_cbs[i].skb != NULL) {
1744 dev_kfree_skb(priv->tx_cbs[i].skb);
1745 priv->tx_cbs[i].skb = NULL;
1746 }
1747 }
1748
1749 bcmgenet_free_rx_buffers(priv);
1750 kfree(priv->rx_cbs);
1751 kfree(priv->tx_cbs);
1752}
1753
1754/* init_edma: Initialize DMA control register */
1755static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
1756{
1757 int ret;
1758
1759 netif_dbg(priv, hw, priv->dev, "bcmgenet: init_edma\n");
1760
1761 /* by default, enable ring 16 (descriptor based) */
1762 ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, TOTAL_DESC);
1763 if (ret) {
1764 netdev_err(priv->dev, "failed to initialize RX ring\n");
1765 return ret;
1766 }
1767
1768 /* init rDma */
1769 bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
1770
1771 /* Init tDma */
1772 bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
1773
1774 /* Initialize commont TX ring structures */
1775 priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
1776 priv->num_tx_bds = TOTAL_DESC;
1777 priv->tx_cbs = kzalloc(priv->num_tx_bds * sizeof(struct enet_cb),
Florian Fainellic91b7f62014-07-23 10:42:12 -07001778 GFP_KERNEL);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001779 if (!priv->tx_cbs) {
1780 bcmgenet_fini_dma(priv);
1781 return -ENOMEM;
1782 }
1783
1784 /* initialize multi xmit queue */
1785 bcmgenet_init_multiq(priv->dev);
1786
1787 /* initialize special ring 16 */
1788 bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_DEFAULT_BD_CNT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001789 priv->hw_params->tx_queues *
1790 priv->hw_params->bds_cnt,
1791 TOTAL_DESC);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001792
1793 return 0;
1794}
1795
1796/* NAPI polling method*/
1797static int bcmgenet_poll(struct napi_struct *napi, int budget)
1798{
1799 struct bcmgenet_priv *priv = container_of(napi,
1800 struct bcmgenet_priv, napi);
1801 unsigned int work_done;
1802
1803 /* tx reclaim */
1804 bcmgenet_tx_reclaim(priv->dev, &priv->tx_rings[DESC_INDEX]);
1805
1806 work_done = bcmgenet_desc_rx(priv, budget);
1807
1808 /* Advancing our consumer index*/
1809 priv->rx_c_index += work_done;
1810 priv->rx_c_index &= DMA_C_INDEX_MASK;
1811 bcmgenet_rdma_ring_writel(priv, DESC_INDEX,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001812 priv->rx_c_index, RDMA_CONS_INDEX);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001813 if (work_done < budget) {
1814 napi_complete(napi);
Florian Fainellic91b7f62014-07-23 10:42:12 -07001815 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_BDONE,
1816 INTRL2_CPU_MASK_CLEAR);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001817 }
1818
1819 return work_done;
1820}
1821
1822/* Interrupt bottom half */
1823static void bcmgenet_irq_task(struct work_struct *work)
1824{
1825 struct bcmgenet_priv *priv = container_of(
1826 work, struct bcmgenet_priv, bcmgenet_irq_work);
1827
1828 netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
1829
Florian Fainelli8fdb0e02014-07-21 15:29:26 -07001830 if (priv->irq0_stat & UMAC_IRQ_MPD_R) {
1831 priv->irq0_stat &= ~UMAC_IRQ_MPD_R;
1832 netif_dbg(priv, wol, priv->dev,
1833 "magic packet detected, waking up\n");
1834 bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
1835 }
1836
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001837 /* Link UP/DOWN event */
1838 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001839 (priv->irq0_stat & (UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN))) {
Florian Fainelli80d8e962014-02-24 16:56:11 -08001840 phy_mac_interrupt(priv->phydev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001841 priv->irq0_stat & UMAC_IRQ_LINK_UP);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001842 priv->irq0_stat &= ~(UMAC_IRQ_LINK_UP|UMAC_IRQ_LINK_DOWN);
1843 }
1844}
1845
1846/* bcmgenet_isr1: interrupt handler for ring buffer. */
1847static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
1848{
1849 struct bcmgenet_priv *priv = dev_id;
1850 unsigned int index;
1851
1852 /* Save irq status for bottom-half processing. */
1853 priv->irq1_stat =
1854 bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
1855 ~priv->int1_mask;
1856 /* clear inerrupts*/
1857 bcmgenet_intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
1858
1859 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001860 "%s: IRQ=0x%x\n", __func__, priv->irq1_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001861 /* Check the MBDONE interrupts.
1862 * packet is done, reclaim descriptors
1863 */
1864 if (priv->irq1_stat & 0x0000ffff) {
1865 index = 0;
1866 for (index = 0; index < 16; index++) {
1867 if (priv->irq1_stat & (1 << index))
1868 bcmgenet_tx_reclaim(priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001869 &priv->tx_rings[index]);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001870 }
1871 }
1872 return IRQ_HANDLED;
1873}
1874
1875/* bcmgenet_isr0: Handle various interrupts. */
1876static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
1877{
1878 struct bcmgenet_priv *priv = dev_id;
1879
1880 /* Save irq status for bottom-half processing. */
1881 priv->irq0_stat =
1882 bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
1883 ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
1884 /* clear inerrupts*/
1885 bcmgenet_intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
1886
1887 netif_dbg(priv, intr, priv->dev,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001888 "IRQ=0x%x\n", priv->irq0_stat);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001889
1890 if (priv->irq0_stat & (UMAC_IRQ_RXDMA_BDONE | UMAC_IRQ_RXDMA_PDONE)) {
1891 /* We use NAPI(software interrupt throttling, if
1892 * Rx Descriptor throttling is not used.
1893 * Disable interrupt, will be enabled in the poll method.
1894 */
1895 if (likely(napi_schedule_prep(&priv->napi))) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07001896 bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_BDONE,
1897 INTRL2_CPU_MASK_SET);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001898 __napi_schedule(&priv->napi);
1899 }
1900 }
1901 if (priv->irq0_stat &
1902 (UMAC_IRQ_TXDMA_BDONE | UMAC_IRQ_TXDMA_PDONE)) {
1903 /* Tx reclaim */
1904 bcmgenet_tx_reclaim(priv->dev, &priv->tx_rings[DESC_INDEX]);
1905 }
1906 if (priv->irq0_stat & (UMAC_IRQ_PHY_DET_R |
1907 UMAC_IRQ_PHY_DET_F |
1908 UMAC_IRQ_LINK_UP |
1909 UMAC_IRQ_LINK_DOWN |
1910 UMAC_IRQ_HFB_SM |
1911 UMAC_IRQ_HFB_MM |
1912 UMAC_IRQ_MPD_R)) {
1913 /* all other interested interrupts handled in bottom half */
1914 schedule_work(&priv->bcmgenet_irq_work);
1915 }
1916
1917 if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
Florian Fainellic91b7f62014-07-23 10:42:12 -07001918 priv->irq0_stat & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001919 priv->irq0_stat &= ~(UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
1920 wake_up(&priv->wq);
1921 }
1922
1923 return IRQ_HANDLED;
1924}
1925
Florian Fainelli85620562014-07-21 15:29:23 -07001926static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
1927{
1928 struct bcmgenet_priv *priv = dev_id;
1929
1930 pm_wakeup_event(&priv->pdev->dev, 0);
1931
1932 return IRQ_HANDLED;
1933}
1934
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001935static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
1936{
1937 u32 reg;
1938
1939 reg = bcmgenet_rbuf_ctrl_get(priv);
1940 reg |= BIT(1);
1941 bcmgenet_rbuf_ctrl_set(priv, reg);
1942 udelay(10);
1943
1944 reg &= ~BIT(1);
1945 bcmgenet_rbuf_ctrl_set(priv, reg);
1946 udelay(10);
1947}
1948
1949static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -07001950 unsigned char *addr)
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001951{
1952 bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
1953 (addr[2] << 8) | addr[3], UMAC_MAC0);
1954 bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
1955}
1956
1957static int bcmgenet_wol_resume(struct bcmgenet_priv *priv)
1958{
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001959 /* From WOL-enabled suspend, switch to regular clock */
Florian Fainelli1c3c1e72014-07-21 15:29:27 -07001960 clk_disable_unprepare(priv->clk_wol);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001961
Florian Fainelli80d8e962014-02-24 16:56:11 -08001962 phy_init_hw(priv->phydev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08001963 /* Speed settings must be restored */
1964 bcmgenet_mii_config(priv->dev);
1965
1966 return 0;
1967}
1968
1969/* Returns a reusable dma control register value */
1970static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
1971{
1972 u32 reg;
1973 u32 dma_ctrl;
1974
1975 /* disable DMA */
1976 dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
1977 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
1978 reg &= ~dma_ctrl;
1979 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
1980
1981 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
1982 reg &= ~dma_ctrl;
1983 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
1984
1985 bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
1986 udelay(10);
1987 bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
1988
1989 return dma_ctrl;
1990}
1991
1992static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
1993{
1994 u32 reg;
1995
1996 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
1997 reg |= dma_ctrl;
1998 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
1999
2000 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2001 reg |= dma_ctrl;
2002 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2003}
2004
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002005static void bcmgenet_netif_start(struct net_device *dev)
2006{
2007 struct bcmgenet_priv *priv = netdev_priv(dev);
2008
2009 /* Start the network engine */
2010 napi_enable(&priv->napi);
2011
2012 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
2013
2014 if (phy_is_internal(priv->phydev))
2015 bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
2016
2017 netif_tx_start_all_queues(dev);
2018
2019 phy_start(priv->phydev);
2020}
2021
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002022static int bcmgenet_open(struct net_device *dev)
2023{
2024 struct bcmgenet_priv *priv = netdev_priv(dev);
2025 unsigned long dma_ctrl;
2026 u32 reg;
2027 int ret;
2028
2029 netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
2030
2031 /* Turn on the clock */
2032 if (!IS_ERR(priv->clk))
2033 clk_prepare_enable(priv->clk);
2034
2035 /* take MAC out of reset */
2036 bcmgenet_umac_reset(priv);
2037
2038 ret = init_umac(priv);
2039 if (ret)
2040 goto err_clk_disable;
2041
2042 /* disable ethernet MAC while updating its registers */
Florian Fainellie29585b2014-07-21 15:29:20 -07002043 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002044
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002045 /* Make sure we reflect the value of CRC_CMD_FWD */
2046 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2047 priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
2048
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002049 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2050
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002051 if (phy_is_internal(priv->phydev)) {
2052 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2053 reg |= EXT_ENERGY_DET_MASK;
2054 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2055 }
2056
2057 /* Disable RX/TX DMA and flush TX queues */
2058 dma_ctrl = bcmgenet_dma_disable(priv);
2059
2060 /* Reinitialize TDMA and RDMA and SW housekeeping */
2061 ret = bcmgenet_init_dma(priv);
2062 if (ret) {
2063 netdev_err(dev, "failed to initialize DMA\n");
2064 goto err_fini_dma;
2065 }
2066
2067 /* Always enable ring 16 - descriptor ring */
2068 bcmgenet_enable_dma(priv, dma_ctrl);
2069
2070 ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002071 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002072 if (ret < 0) {
2073 netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
2074 goto err_fini_dma;
2075 }
2076
2077 ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002078 dev->name, priv);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002079 if (ret < 0) {
2080 netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
2081 goto err_irq0;
2082 }
2083
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002084 bcmgenet_netif_start(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002085
2086 return 0;
2087
2088err_irq0:
2089 free_irq(priv->irq0, dev);
2090err_fini_dma:
2091 bcmgenet_fini_dma(priv);
2092err_clk_disable:
2093 if (!IS_ERR(priv->clk))
2094 clk_disable_unprepare(priv->clk);
2095 return ret;
2096}
2097
2098static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2099{
2100 int ret = 0;
2101 int timeout = 0;
2102 u32 reg;
2103
2104 /* Disable TDMA to stop add more frames in TX DMA */
2105 reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2106 reg &= ~DMA_EN;
2107 bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2108
2109 /* Check TDMA status register to confirm TDMA is disabled */
2110 while (timeout++ < DMA_TIMEOUT_VAL) {
2111 reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2112 if (reg & DMA_DISABLED)
2113 break;
2114
2115 udelay(1);
2116 }
2117
2118 if (timeout == DMA_TIMEOUT_VAL) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07002119 netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002120 ret = -ETIMEDOUT;
2121 }
2122
2123 /* Wait 10ms for packet drain in both tx and rx dma */
2124 usleep_range(10000, 20000);
2125
2126 /* Disable RDMA */
2127 reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2128 reg &= ~DMA_EN;
2129 bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2130
2131 timeout = 0;
2132 /* Check RDMA status register to confirm RDMA is disabled */
2133 while (timeout++ < DMA_TIMEOUT_VAL) {
2134 reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2135 if (reg & DMA_DISABLED)
2136 break;
2137
2138 udelay(1);
2139 }
2140
2141 if (timeout == DMA_TIMEOUT_VAL) {
Florian Fainellic91b7f62014-07-23 10:42:12 -07002142 netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2143 ret = -ETIMEDOUT;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002144 }
2145
2146 return ret;
2147}
2148
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002149static void bcmgenet_netif_stop(struct net_device *dev)
2150{
2151 struct bcmgenet_priv *priv = netdev_priv(dev);
2152
2153 netif_tx_stop_all_queues(dev);
2154 napi_disable(&priv->napi);
2155 phy_stop(priv->phydev);
2156
2157 bcmgenet_intr_disable(priv);
2158
2159 /* Wait for pending work items to complete. Since interrupts are
2160 * disabled no new work will be scheduled.
2161 */
2162 cancel_work_sync(&priv->bcmgenet_irq_work);
2163}
2164
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002165static int bcmgenet_close(struct net_device *dev)
2166{
2167 struct bcmgenet_priv *priv = netdev_priv(dev);
2168 int ret;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002169
2170 netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
2171
Florian Fainelli909ff5e2014-07-21 15:29:21 -07002172 bcmgenet_netif_stop(dev);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002173
2174 /* Disable MAC receive */
Florian Fainellie29585b2014-07-21 15:29:20 -07002175 umac_enable_set(priv, CMD_RX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002176
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002177 ret = bcmgenet_dma_teardown(priv);
2178 if (ret)
2179 return ret;
2180
2181 /* Disable MAC transmit. TX DMA disabled have to done before this */
Florian Fainellie29585b2014-07-21 15:29:20 -07002182 umac_enable_set(priv, CMD_TX_EN, false);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002183
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002184 /* tx reclaim */
2185 bcmgenet_tx_reclaim_all(dev);
2186 bcmgenet_fini_dma(priv);
2187
2188 free_irq(priv->irq0, priv);
2189 free_irq(priv->irq1, priv);
2190
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002191 if (phy_is_internal(priv->phydev))
2192 bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
2193
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002194 if (!IS_ERR(priv->clk))
2195 clk_disable_unprepare(priv->clk);
2196
2197 return 0;
2198}
2199
2200static void bcmgenet_timeout(struct net_device *dev)
2201{
2202 struct bcmgenet_priv *priv = netdev_priv(dev);
2203
2204 netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
2205
2206 dev->trans_start = jiffies;
2207
2208 dev->stats.tx_errors++;
2209
2210 netif_tx_wake_all_queues(dev);
2211}
2212
2213#define MAX_MC_COUNT 16
2214
2215static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
2216 unsigned char *addr,
2217 int *i,
2218 int *mc)
2219{
2220 u32 reg;
2221
Florian Fainellic91b7f62014-07-23 10:42:12 -07002222 bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
2223 UMAC_MDF_ADDR + (*i * 4));
2224 bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
2225 addr[4] << 8 | addr[5],
2226 UMAC_MDF_ADDR + ((*i + 1) * 4));
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002227 reg = bcmgenet_umac_readl(priv, UMAC_MDF_CTRL);
2228 reg |= (1 << (MAX_MC_COUNT - *mc));
2229 bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
2230 *i += 2;
2231 (*mc)++;
2232}
2233
2234static void bcmgenet_set_rx_mode(struct net_device *dev)
2235{
2236 struct bcmgenet_priv *priv = netdev_priv(dev);
2237 struct netdev_hw_addr *ha;
2238 int i, mc;
2239 u32 reg;
2240
2241 netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
2242
2243 /* Promiscous mode */
2244 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2245 if (dev->flags & IFF_PROMISC) {
2246 reg |= CMD_PROMISC;
2247 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2248 bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
2249 return;
2250 } else {
2251 reg &= ~CMD_PROMISC;
2252 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2253 }
2254
2255 /* UniMac doesn't support ALLMULTI */
2256 if (dev->flags & IFF_ALLMULTI) {
2257 netdev_warn(dev, "ALLMULTI is not supported\n");
2258 return;
2259 }
2260
2261 /* update MDF filter */
2262 i = 0;
2263 mc = 0;
2264 /* Broadcast */
2265 bcmgenet_set_mdf_addr(priv, dev->broadcast, &i, &mc);
2266 /* my own address.*/
2267 bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i, &mc);
2268 /* Unicast list*/
2269 if (netdev_uc_count(dev) > (MAX_MC_COUNT - mc))
2270 return;
2271
2272 if (!netdev_uc_empty(dev))
2273 netdev_for_each_uc_addr(ha, dev)
2274 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2275 /* Multicast */
2276 if (netdev_mc_empty(dev) || netdev_mc_count(dev) >= (MAX_MC_COUNT - mc))
2277 return;
2278
2279 netdev_for_each_mc_addr(ha, dev)
2280 bcmgenet_set_mdf_addr(priv, ha->addr, &i, &mc);
2281}
2282
2283/* Set the hardware MAC address. */
2284static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
2285{
2286 struct sockaddr *addr = p;
2287
2288 /* Setting the MAC address at the hardware level is not possible
2289 * without disabling the UniMAC RX/TX enable bits.
2290 */
2291 if (netif_running(dev))
2292 return -EBUSY;
2293
2294 ether_addr_copy(dev->dev_addr, addr->sa_data);
2295
2296 return 0;
2297}
2298
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002299static const struct net_device_ops bcmgenet_netdev_ops = {
2300 .ndo_open = bcmgenet_open,
2301 .ndo_stop = bcmgenet_close,
2302 .ndo_start_xmit = bcmgenet_xmit,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002303 .ndo_tx_timeout = bcmgenet_timeout,
2304 .ndo_set_rx_mode = bcmgenet_set_rx_mode,
2305 .ndo_set_mac_address = bcmgenet_set_mac_addr,
2306 .ndo_do_ioctl = bcmgenet_ioctl,
2307 .ndo_set_features = bcmgenet_set_features,
2308};
2309
2310/* Array of GENET hardware parameters/characteristics */
2311static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
2312 [GENET_V1] = {
2313 .tx_queues = 0,
2314 .rx_queues = 0,
2315 .bds_cnt = 0,
2316 .bp_in_en_shift = 16,
2317 .bp_in_mask = 0xffff,
2318 .hfb_filter_cnt = 16,
2319 .qtag_mask = 0x1F,
2320 .hfb_offset = 0x1000,
2321 .rdma_offset = 0x2000,
2322 .tdma_offset = 0x3000,
2323 .words_per_bd = 2,
2324 },
2325 [GENET_V2] = {
2326 .tx_queues = 4,
2327 .rx_queues = 4,
2328 .bds_cnt = 32,
2329 .bp_in_en_shift = 16,
2330 .bp_in_mask = 0xffff,
2331 .hfb_filter_cnt = 16,
2332 .qtag_mask = 0x1F,
2333 .tbuf_offset = 0x0600,
2334 .hfb_offset = 0x1000,
2335 .hfb_reg_offset = 0x2000,
2336 .rdma_offset = 0x3000,
2337 .tdma_offset = 0x4000,
2338 .words_per_bd = 2,
2339 .flags = GENET_HAS_EXT,
2340 },
2341 [GENET_V3] = {
2342 .tx_queues = 4,
2343 .rx_queues = 4,
2344 .bds_cnt = 32,
2345 .bp_in_en_shift = 17,
2346 .bp_in_mask = 0x1ffff,
2347 .hfb_filter_cnt = 48,
2348 .qtag_mask = 0x3F,
2349 .tbuf_offset = 0x0600,
2350 .hfb_offset = 0x8000,
2351 .hfb_reg_offset = 0xfc00,
2352 .rdma_offset = 0x10000,
2353 .tdma_offset = 0x11000,
2354 .words_per_bd = 2,
2355 .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2356 },
2357 [GENET_V4] = {
2358 .tx_queues = 4,
2359 .rx_queues = 4,
2360 .bds_cnt = 32,
2361 .bp_in_en_shift = 17,
2362 .bp_in_mask = 0x1ffff,
2363 .hfb_filter_cnt = 48,
2364 .qtag_mask = 0x3F,
2365 .tbuf_offset = 0x0600,
2366 .hfb_offset = 0x8000,
2367 .hfb_reg_offset = 0xfc00,
2368 .rdma_offset = 0x2000,
2369 .tdma_offset = 0x4000,
2370 .words_per_bd = 3,
2371 .flags = GENET_HAS_40BITS | GENET_HAS_EXT | GENET_HAS_MDIO_INTR,
2372 },
2373};
2374
2375/* Infer hardware parameters from the detected GENET version */
2376static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
2377{
2378 struct bcmgenet_hw_params *params;
2379 u32 reg;
2380 u8 major;
2381
2382 if (GENET_IS_V4(priv)) {
2383 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2384 genet_dma_ring_regs = genet_dma_ring_regs_v4;
2385 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2386 priv->version = GENET_V4;
2387 } else if (GENET_IS_V3(priv)) {
2388 bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
2389 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2390 priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
2391 priv->version = GENET_V3;
2392 } else if (GENET_IS_V2(priv)) {
2393 bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
2394 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2395 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2396 priv->version = GENET_V2;
2397 } else if (GENET_IS_V1(priv)) {
2398 bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
2399 genet_dma_ring_regs = genet_dma_ring_regs_v123;
2400 priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
2401 priv->version = GENET_V1;
2402 }
2403
2404 /* enum genet_version starts at 1 */
2405 priv->hw_params = &bcmgenet_hw_params[priv->version];
2406 params = priv->hw_params;
2407
2408 /* Read GENET HW version */
2409 reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
2410 major = (reg >> 24 & 0x0f);
2411 if (major == 5)
2412 major = 4;
2413 else if (major == 0)
2414 major = 1;
2415 if (major != priv->version) {
2416 dev_err(&priv->pdev->dev,
2417 "GENET version mismatch, got: %d, configured for: %d\n",
2418 major, priv->version);
2419 }
2420
2421 /* Print the GENET core version */
2422 dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
Florian Fainellic91b7f62014-07-23 10:42:12 -07002423 major, (reg >> 16) & 0x0f, reg & 0xffff);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002424
2425#ifdef CONFIG_PHYS_ADDR_T_64BIT
2426 if (!(params->flags & GENET_HAS_40BITS))
2427 pr_warn("GENET does not support 40-bits PA\n");
2428#endif
2429
2430 pr_debug("Configuration for version: %d\n"
2431 "TXq: %1d, RXq: %1d, BDs: %1d\n"
2432 "BP << en: %2d, BP msk: 0x%05x\n"
2433 "HFB count: %2d, QTAQ msk: 0x%05x\n"
2434 "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
2435 "RDMA: 0x%05x, TDMA: 0x%05x\n"
2436 "Words/BD: %d\n",
2437 priv->version,
2438 params->tx_queues, params->rx_queues, params->bds_cnt,
2439 params->bp_in_en_shift, params->bp_in_mask,
2440 params->hfb_filter_cnt, params->qtag_mask,
2441 params->tbuf_offset, params->hfb_offset,
2442 params->hfb_reg_offset,
2443 params->rdma_offset, params->tdma_offset,
2444 params->words_per_bd);
2445}
2446
2447static const struct of_device_id bcmgenet_match[] = {
2448 { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
2449 { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
2450 { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
2451 { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
2452 { },
2453};
2454
2455static int bcmgenet_probe(struct platform_device *pdev)
2456{
2457 struct device_node *dn = pdev->dev.of_node;
2458 const struct of_device_id *of_id;
2459 struct bcmgenet_priv *priv;
2460 struct net_device *dev;
2461 const void *macaddr;
2462 struct resource *r;
2463 int err = -EIO;
2464
2465 /* Up to GENET_MAX_MQ_CNT + 1 TX queues and a single RX queue */
2466 dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1, 1);
2467 if (!dev) {
2468 dev_err(&pdev->dev, "can't allocate net device\n");
2469 return -ENOMEM;
2470 }
2471
2472 of_id = of_match_node(bcmgenet_match, dn);
2473 if (!of_id)
2474 return -EINVAL;
2475
2476 priv = netdev_priv(dev);
2477 priv->irq0 = platform_get_irq(pdev, 0);
2478 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli85620562014-07-21 15:29:23 -07002479 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002480 if (!priv->irq0 || !priv->irq1) {
2481 dev_err(&pdev->dev, "can't find IRQs\n");
2482 err = -EINVAL;
2483 goto err;
2484 }
2485
2486 macaddr = of_get_mac_address(dn);
2487 if (!macaddr) {
2488 dev_err(&pdev->dev, "can't find MAC address\n");
2489 err = -EINVAL;
2490 goto err;
2491 }
2492
2493 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam5343a102014-02-24 00:47:24 -03002494 priv->base = devm_ioremap_resource(&pdev->dev, r);
2495 if (IS_ERR(priv->base)) {
2496 err = PTR_ERR(priv->base);
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002497 goto err;
2498 }
2499
2500 SET_NETDEV_DEV(dev, &pdev->dev);
2501 dev_set_drvdata(&pdev->dev, dev);
2502 ether_addr_copy(dev->dev_addr, macaddr);
2503 dev->watchdog_timeo = 2 * HZ;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002504 dev->ethtool_ops = &bcmgenet_ethtool_ops;
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002505 dev->netdev_ops = &bcmgenet_netdev_ops;
2506 netif_napi_add(dev, &priv->napi, bcmgenet_poll, 64);
2507
2508 priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
2509
2510 /* Set hardware features */
2511 dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
2512 NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
2513
Florian Fainelli85620562014-07-21 15:29:23 -07002514 /* Request the WOL interrupt and advertise suspend if available */
2515 priv->wol_irq_disabled = true;
2516 err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
2517 dev->name, priv);
2518 if (!err)
2519 device_set_wakeup_capable(&pdev->dev, 1);
2520
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002521 /* Set the needed headroom to account for any possible
2522 * features enabling/disabling at runtime
2523 */
2524 dev->needed_headroom += 64;
2525
2526 netdev_boot_setup_check(dev);
2527
2528 priv->dev = dev;
2529 priv->pdev = pdev;
2530 priv->version = (enum bcmgenet_version)of_id->data;
2531
2532 bcmgenet_set_hw_params(priv);
2533
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002534 /* Mii wait queue */
2535 init_waitqueue_head(&priv->wq);
2536 /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
2537 priv->rx_buf_len = RX_BUF_LENGTH;
2538 INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
2539
2540 priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
2541 if (IS_ERR(priv->clk))
2542 dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
2543
2544 priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
2545 if (IS_ERR(priv->clk_wol))
2546 dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
2547
2548 if (!IS_ERR(priv->clk))
2549 clk_prepare_enable(priv->clk);
2550
2551 err = reset_umac(priv);
2552 if (err)
2553 goto err_clk_disable;
2554
2555 err = bcmgenet_mii_init(dev);
2556 if (err)
2557 goto err_clk_disable;
2558
2559 /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
2560 * just the ring 16 descriptor based TX
2561 */
2562 netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
2563 netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
2564
Florian Fainelli219575e2014-06-26 10:26:21 -07002565 /* libphy will determine the link state */
2566 netif_carrier_off(dev);
2567
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002568 /* Turn off the main clock, WOL clock is handled separately */
2569 if (!IS_ERR(priv->clk))
2570 clk_disable_unprepare(priv->clk);
2571
Florian Fainelli0f50ce92014-06-26 10:26:20 -07002572 err = register_netdev(dev);
2573 if (err)
2574 goto err;
2575
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002576 return err;
2577
2578err_clk_disable:
2579 if (!IS_ERR(priv->clk))
2580 clk_disable_unprepare(priv->clk);
2581err:
2582 free_netdev(dev);
2583 return err;
2584}
2585
2586static int bcmgenet_remove(struct platform_device *pdev)
2587{
2588 struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
2589
2590 dev_set_drvdata(&pdev->dev, NULL);
2591 unregister_netdev(priv->dev);
2592 bcmgenet_mii_exit(priv->dev);
2593 free_netdev(priv->dev);
2594
2595 return 0;
2596}
2597
Florian Fainellib6e978e2014-07-21 15:29:22 -07002598#ifdef CONFIG_PM_SLEEP
2599static int bcmgenet_suspend(struct device *d)
2600{
2601 struct net_device *dev = dev_get_drvdata(d);
2602 struct bcmgenet_priv *priv = netdev_priv(dev);
2603 int ret;
2604
2605 if (!netif_running(dev))
2606 return 0;
2607
2608 bcmgenet_netif_stop(dev);
2609
2610 netif_device_detach(dev);
2611
2612 /* Disable MAC receive */
2613 umac_enable_set(priv, CMD_RX_EN, false);
2614
2615 ret = bcmgenet_dma_teardown(priv);
2616 if (ret)
2617 return ret;
2618
2619 /* Disable MAC transmit. TX DMA disabled have to done before this */
2620 umac_enable_set(priv, CMD_TX_EN, false);
2621
2622 /* tx reclaim */
2623 bcmgenet_tx_reclaim_all(dev);
2624 bcmgenet_fini_dma(priv);
2625
Florian Fainelli8c90db72014-07-21 15:29:28 -07002626 /* Prepare the device for Wake-on-LAN and switch to the slow clock */
2627 if (device_may_wakeup(d) && priv->wolopts) {
2628 bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
2629 clk_prepare_enable(priv->clk_wol);
2630 }
2631
Florian Fainellib6e978e2014-07-21 15:29:22 -07002632 /* Turn off the clocks */
2633 clk_disable_unprepare(priv->clk);
2634
2635 return 0;
2636}
2637
2638static int bcmgenet_resume(struct device *d)
2639{
2640 struct net_device *dev = dev_get_drvdata(d);
2641 struct bcmgenet_priv *priv = netdev_priv(dev);
2642 unsigned long dma_ctrl;
2643 int ret;
2644 u32 reg;
2645
2646 if (!netif_running(dev))
2647 return 0;
2648
2649 /* Turn on the clock */
2650 ret = clk_prepare_enable(priv->clk);
2651 if (ret)
2652 return ret;
2653
2654 bcmgenet_umac_reset(priv);
2655
2656 ret = init_umac(priv);
2657 if (ret)
2658 goto out_clk_disable;
2659
Florian Fainelli8c90db72014-07-21 15:29:28 -07002660 if (priv->wolopts)
2661 ret = bcmgenet_wol_resume(priv);
2662
2663 if (ret)
2664 goto out_clk_disable;
2665
Florian Fainellib6e978e2014-07-21 15:29:22 -07002666 /* disable ethernet MAC while updating its registers */
2667 umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, false);
2668
2669 bcmgenet_set_hw_addr(priv, dev->dev_addr);
2670
2671 if (phy_is_internal(priv->phydev)) {
2672 reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
2673 reg |= EXT_ENERGY_DET_MASK;
2674 bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
2675 }
2676
2677 /* Disable RX/TX DMA and flush TX queues */
2678 dma_ctrl = bcmgenet_dma_disable(priv);
2679
2680 /* Reinitialize TDMA and RDMA and SW housekeeping */
2681 ret = bcmgenet_init_dma(priv);
2682 if (ret) {
2683 netdev_err(dev, "failed to initialize DMA\n");
2684 goto out_clk_disable;
2685 }
2686
2687 /* Always enable ring 16 - descriptor ring */
2688 bcmgenet_enable_dma(priv, dma_ctrl);
2689
2690 netif_device_attach(dev);
2691
2692 bcmgenet_netif_start(dev);
2693
2694 return 0;
2695
2696out_clk_disable:
2697 clk_disable_unprepare(priv->clk);
2698 return ret;
2699}
2700#endif /* CONFIG_PM_SLEEP */
2701
2702static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
2703
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002704static struct platform_driver bcmgenet_driver = {
2705 .probe = bcmgenet_probe,
2706 .remove = bcmgenet_remove,
2707 .driver = {
2708 .name = "bcmgenet",
2709 .owner = THIS_MODULE,
2710 .of_match_table = bcmgenet_match,
Florian Fainellib6e978e2014-07-21 15:29:22 -07002711 .pm = &bcmgenet_pm_ops,
Florian Fainelli1c1008c2014-02-13 16:08:47 -08002712 },
2713};
2714module_platform_driver(bcmgenet_driver);
2715
2716MODULE_AUTHOR("Broadcom Corporation");
2717MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
2718MODULE_ALIAS("platform:bcmgenet");
2719MODULE_LICENSE("GPL");