blob: edaf76dc24875d7ead270af3a1b7c02e9f34efd6 [file] [log] [blame]
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001/*
2 * Driver for BCM963xx builtin Ethernet mac
3 *
4 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
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 as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <linux/init.h>
Alexey Dobriyan539d3ee2011-06-10 03:36:43 +000021#include <linux/interrupt.h>
Maxime Bizon9b1fc552009-08-18 13:23:40 +010022#include <linux/module.h>
23#include <linux/clk.h>
24#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Maxime Bizon9b1fc552009-08-18 13:23:40 +010026#include <linux/delay.h>
27#include <linux/ethtool.h>
28#include <linux/crc32.h>
29#include <linux/err.h>
30#include <linux/dma-mapping.h>
31#include <linux/platform_device.h>
32#include <linux/if_vlan.h>
33
34#include <bcm63xx_dev_enet.h>
35#include "bcm63xx_enet.h"
36
37static char bcm_enet_driver_name[] = "bcm63xx_enet";
38static char bcm_enet_driver_version[] = "1.0";
39
40static int copybreak __read_mostly = 128;
41module_param(copybreak, int, 0);
42MODULE_PARM_DESC(copybreak, "Receive copy threshold");
43
Maxime Bizon0ae99b52013-06-04 22:53:34 +010044/* io registers memory shared between all devices */
45static void __iomem *bcm_enet_shared_base[3];
Maxime Bizon9b1fc552009-08-18 13:23:40 +010046
47/*
48 * io helpers to access mac registers
49 */
50static inline u32 enet_readl(struct bcm_enet_priv *priv, u32 off)
51{
52 return bcm_readl(priv->base + off);
53}
54
55static inline void enet_writel(struct bcm_enet_priv *priv,
56 u32 val, u32 off)
57{
58 bcm_writel(val, priv->base + off);
59}
60
61/*
62 * io helpers to access shared registers
63 */
64static inline u32 enet_dma_readl(struct bcm_enet_priv *priv, u32 off)
65{
Maxime Bizon0ae99b52013-06-04 22:53:34 +010066 return bcm_readl(bcm_enet_shared_base[0] + off);
Maxime Bizon9b1fc552009-08-18 13:23:40 +010067}
68
69static inline void enet_dma_writel(struct bcm_enet_priv *priv,
70 u32 val, u32 off)
71{
Maxime Bizon0ae99b52013-06-04 22:53:34 +010072 bcm_writel(val, bcm_enet_shared_base[0] + off);
73}
74
75static inline u32 enet_dmac_readl(struct bcm_enet_priv *priv, u32 off)
76{
77 return bcm_readl(bcm_enet_shared_base[1] + off);
78}
79
80static inline void enet_dmac_writel(struct bcm_enet_priv *priv,
81 u32 val, u32 off)
82{
83 bcm_writel(val, bcm_enet_shared_base[1] + off);
84}
85
86static inline u32 enet_dmas_readl(struct bcm_enet_priv *priv, u32 off)
87{
88 return bcm_readl(bcm_enet_shared_base[2] + off);
89}
90
91static inline void enet_dmas_writel(struct bcm_enet_priv *priv,
92 u32 val, u32 off)
93{
94 bcm_writel(val, bcm_enet_shared_base[2] + off);
Maxime Bizon9b1fc552009-08-18 13:23:40 +010095}
96
97/*
98 * write given data into mii register and wait for transfer to end
99 * with timeout (average measured transfer time is 25us)
100 */
101static int do_mdio_op(struct bcm_enet_priv *priv, unsigned int data)
102{
103 int limit;
104
105 /* make sure mii interrupt status is cleared */
106 enet_writel(priv, ENET_IR_MII, ENET_IR_REG);
107
108 enet_writel(priv, data, ENET_MIIDATA_REG);
109 wmb();
110
111 /* busy wait on mii interrupt bit, with timeout */
112 limit = 1000;
113 do {
114 if (enet_readl(priv, ENET_IR_REG) & ENET_IR_MII)
115 break;
116 udelay(1);
roel kluinec1652a2009-09-21 10:08:48 +0000117 } while (limit-- > 0);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100118
119 return (limit < 0) ? 1 : 0;
120}
121
122/*
123 * MII internal read callback
124 */
125static int bcm_enet_mdio_read(struct bcm_enet_priv *priv, int mii_id,
126 int regnum)
127{
128 u32 tmp, val;
129
130 tmp = regnum << ENET_MIIDATA_REG_SHIFT;
131 tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
132 tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
133 tmp |= ENET_MIIDATA_OP_READ_MASK;
134
135 if (do_mdio_op(priv, tmp))
136 return -1;
137
138 val = enet_readl(priv, ENET_MIIDATA_REG);
139 val &= 0xffff;
140 return val;
141}
142
143/*
144 * MII internal write callback
145 */
146static int bcm_enet_mdio_write(struct bcm_enet_priv *priv, int mii_id,
147 int regnum, u16 value)
148{
149 u32 tmp;
150
151 tmp = (value & 0xffff) << ENET_MIIDATA_DATA_SHIFT;
152 tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
153 tmp |= regnum << ENET_MIIDATA_REG_SHIFT;
154 tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
155 tmp |= ENET_MIIDATA_OP_WRITE_MASK;
156
157 (void)do_mdio_op(priv, tmp);
158 return 0;
159}
160
161/*
162 * MII read callback from phylib
163 */
164static int bcm_enet_mdio_read_phylib(struct mii_bus *bus, int mii_id,
165 int regnum)
166{
167 return bcm_enet_mdio_read(bus->priv, mii_id, regnum);
168}
169
170/*
171 * MII write callback from phylib
172 */
173static int bcm_enet_mdio_write_phylib(struct mii_bus *bus, int mii_id,
174 int regnum, u16 value)
175{
176 return bcm_enet_mdio_write(bus->priv, mii_id, regnum, value);
177}
178
179/*
180 * MII read callback from mii core
181 */
182static int bcm_enet_mdio_read_mii(struct net_device *dev, int mii_id,
183 int regnum)
184{
185 return bcm_enet_mdio_read(netdev_priv(dev), mii_id, regnum);
186}
187
188/*
189 * MII write callback from mii core
190 */
191static void bcm_enet_mdio_write_mii(struct net_device *dev, int mii_id,
192 int regnum, int value)
193{
194 bcm_enet_mdio_write(netdev_priv(dev), mii_id, regnum, value);
195}
196
197/*
198 * refill rx queue
199 */
200static int bcm_enet_refill_rx(struct net_device *dev)
201{
202 struct bcm_enet_priv *priv;
203
204 priv = netdev_priv(dev);
205
206 while (priv->rx_desc_count < priv->rx_ring_size) {
207 struct bcm_enet_desc *desc;
208 struct sk_buff *skb;
209 dma_addr_t p;
210 int desc_idx;
211 u32 len_stat;
212
213 desc_idx = priv->rx_dirty_desc;
214 desc = &priv->rx_desc_cpu[desc_idx];
215
216 if (!priv->rx_skb[desc_idx]) {
217 skb = netdev_alloc_skb(dev, priv->rx_skb_size);
218 if (!skb)
219 break;
220 priv->rx_skb[desc_idx] = skb;
221
222 p = dma_map_single(&priv->pdev->dev, skb->data,
223 priv->rx_skb_size,
224 DMA_FROM_DEVICE);
225 desc->address = p;
226 }
227
228 len_stat = priv->rx_skb_size << DMADESC_LENGTH_SHIFT;
229 len_stat |= DMADESC_OWNER_MASK;
230 if (priv->rx_dirty_desc == priv->rx_ring_size - 1) {
231 len_stat |= DMADESC_WRAP_MASK;
232 priv->rx_dirty_desc = 0;
233 } else {
234 priv->rx_dirty_desc++;
235 }
236 wmb();
237 desc->len_stat = len_stat;
238
239 priv->rx_desc_count++;
240
241 /* tell dma engine we allocated one buffer */
242 enet_dma_writel(priv, 1, ENETDMA_BUFALLOC_REG(priv->rx_chan));
243 }
244
245 /* If rx ring is still empty, set a timer to try allocating
246 * again at a later time. */
247 if (priv->rx_desc_count == 0 && netif_running(dev)) {
248 dev_warn(&priv->pdev->dev, "unable to refill rx ring\n");
249 priv->rx_timeout.expires = jiffies + HZ;
250 add_timer(&priv->rx_timeout);
251 }
252
253 return 0;
254}
255
256/*
257 * timer callback to defer refill rx queue in case we're OOM
258 */
259static void bcm_enet_refill_rx_timer(unsigned long data)
260{
261 struct net_device *dev;
262 struct bcm_enet_priv *priv;
263
264 dev = (struct net_device *)data;
265 priv = netdev_priv(dev);
266
267 spin_lock(&priv->rx_lock);
268 bcm_enet_refill_rx((struct net_device *)data);
269 spin_unlock(&priv->rx_lock);
270}
271
272/*
273 * extract packet from rx queue
274 */
275static int bcm_enet_receive_queue(struct net_device *dev, int budget)
276{
277 struct bcm_enet_priv *priv;
278 struct device *kdev;
279 int processed;
280
281 priv = netdev_priv(dev);
282 kdev = &priv->pdev->dev;
283 processed = 0;
284
285 /* don't scan ring further than number of refilled
286 * descriptor */
287 if (budget > priv->rx_desc_count)
288 budget = priv->rx_desc_count;
289
290 do {
291 struct bcm_enet_desc *desc;
292 struct sk_buff *skb;
293 int desc_idx;
294 u32 len_stat;
295 unsigned int len;
296
297 desc_idx = priv->rx_curr_desc;
298 desc = &priv->rx_desc_cpu[desc_idx];
299
300 /* make sure we actually read the descriptor status at
301 * each loop */
302 rmb();
303
304 len_stat = desc->len_stat;
305
306 /* break if dma ownership belongs to hw */
307 if (len_stat & DMADESC_OWNER_MASK)
308 break;
309
310 processed++;
311 priv->rx_curr_desc++;
312 if (priv->rx_curr_desc == priv->rx_ring_size)
313 priv->rx_curr_desc = 0;
314 priv->rx_desc_count--;
315
316 /* if the packet does not have start of packet _and_
317 * end of packet flag set, then just recycle it */
318 if ((len_stat & DMADESC_ESOP_MASK) != DMADESC_ESOP_MASK) {
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700319 dev->stats.rx_dropped++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100320 continue;
321 }
322
323 /* recycle packet if it's marked as bad */
324 if (unlikely(len_stat & DMADESC_ERR_MASK)) {
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700325 dev->stats.rx_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100326
327 if (len_stat & DMADESC_OVSIZE_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700328 dev->stats.rx_length_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100329 if (len_stat & DMADESC_CRC_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700330 dev->stats.rx_crc_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100331 if (len_stat & DMADESC_UNDER_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700332 dev->stats.rx_frame_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100333 if (len_stat & DMADESC_OV_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700334 dev->stats.rx_fifo_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100335 continue;
336 }
337
338 /* valid packet */
339 skb = priv->rx_skb[desc_idx];
340 len = (len_stat & DMADESC_LENGTH_MASK) >> DMADESC_LENGTH_SHIFT;
341 /* don't include FCS */
342 len -= 4;
343
344 if (len < copybreak) {
345 struct sk_buff *nskb;
346
Eric Dumazet89d71a62009-10-13 05:34:20 +0000347 nskb = netdev_alloc_skb_ip_align(dev, len);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100348 if (!nskb) {
349 /* forget packet, just rearm desc */
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700350 dev->stats.rx_dropped++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100351 continue;
352 }
353
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100354 dma_sync_single_for_cpu(kdev, desc->address,
355 len, DMA_FROM_DEVICE);
356 memcpy(nskb->data, skb->data, len);
357 dma_sync_single_for_device(kdev, desc->address,
358 len, DMA_FROM_DEVICE);
359 skb = nskb;
360 } else {
361 dma_unmap_single(&priv->pdev->dev, desc->address,
362 priv->rx_skb_size, DMA_FROM_DEVICE);
363 priv->rx_skb[desc_idx] = NULL;
364 }
365
366 skb_put(skb, len);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100367 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700368 dev->stats.rx_packets++;
369 dev->stats.rx_bytes += len;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100370 netif_receive_skb(skb);
371
372 } while (--budget > 0);
373
374 if (processed || !priv->rx_desc_count) {
375 bcm_enet_refill_rx(dev);
376
377 /* kick rx dma */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100378 enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
379 ENETDMAC_CHANCFG_REG(priv->rx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100380 }
381
382 return processed;
383}
384
385
386/*
387 * try to or force reclaim of transmitted buffers
388 */
389static int bcm_enet_tx_reclaim(struct net_device *dev, int force)
390{
391 struct bcm_enet_priv *priv;
392 int released;
393
394 priv = netdev_priv(dev);
395 released = 0;
396
397 while (priv->tx_desc_count < priv->tx_ring_size) {
398 struct bcm_enet_desc *desc;
399 struct sk_buff *skb;
400
401 /* We run in a bh and fight against start_xmit, which
402 * is called with bh disabled */
403 spin_lock(&priv->tx_lock);
404
405 desc = &priv->tx_desc_cpu[priv->tx_dirty_desc];
406
407 if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
408 spin_unlock(&priv->tx_lock);
409 break;
410 }
411
412 /* ensure other field of the descriptor were not read
413 * before we checked ownership */
414 rmb();
415
416 skb = priv->tx_skb[priv->tx_dirty_desc];
417 priv->tx_skb[priv->tx_dirty_desc] = NULL;
418 dma_unmap_single(&priv->pdev->dev, desc->address, skb->len,
419 DMA_TO_DEVICE);
420
421 priv->tx_dirty_desc++;
422 if (priv->tx_dirty_desc == priv->tx_ring_size)
423 priv->tx_dirty_desc = 0;
424 priv->tx_desc_count++;
425
426 spin_unlock(&priv->tx_lock);
427
428 if (desc->len_stat & DMADESC_UNDER_MASK)
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700429 dev->stats.tx_errors++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100430
431 dev_kfree_skb(skb);
432 released++;
433 }
434
435 if (netif_queue_stopped(dev) && released)
436 netif_wake_queue(dev);
437
438 return released;
439}
440
441/*
442 * poll func, called by network core
443 */
444static int bcm_enet_poll(struct napi_struct *napi, int budget)
445{
446 struct bcm_enet_priv *priv;
447 struct net_device *dev;
448 int tx_work_done, rx_work_done;
449
450 priv = container_of(napi, struct bcm_enet_priv, napi);
451 dev = priv->net_dev;
452
453 /* ack interrupts */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100454 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
455 ENETDMAC_IR_REG(priv->rx_chan));
456 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
457 ENETDMAC_IR_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100458
459 /* reclaim sent skb */
460 tx_work_done = bcm_enet_tx_reclaim(dev, 0);
461
462 spin_lock(&priv->rx_lock);
463 rx_work_done = bcm_enet_receive_queue(dev, budget);
464 spin_unlock(&priv->rx_lock);
465
466 if (rx_work_done >= budget || tx_work_done > 0) {
467 /* rx/tx queue is not yet empty/clean */
468 return rx_work_done;
469 }
470
471 /* no more packet in rx/tx queue, remove device from poll
472 * queue */
473 napi_complete(napi);
474
475 /* restore rx/tx interrupt */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100476 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
477 ENETDMAC_IRMASK_REG(priv->rx_chan));
478 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
479 ENETDMAC_IRMASK_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100480
481 return rx_work_done;
482}
483
484/*
485 * mac interrupt handler
486 */
487static irqreturn_t bcm_enet_isr_mac(int irq, void *dev_id)
488{
489 struct net_device *dev;
490 struct bcm_enet_priv *priv;
491 u32 stat;
492
493 dev = dev_id;
494 priv = netdev_priv(dev);
495
496 stat = enet_readl(priv, ENET_IR_REG);
497 if (!(stat & ENET_IR_MIB))
498 return IRQ_NONE;
499
500 /* clear & mask interrupt */
501 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
502 enet_writel(priv, 0, ENET_IRMASK_REG);
503
504 /* read mib registers in workqueue */
505 schedule_work(&priv->mib_update_task);
506
507 return IRQ_HANDLED;
508}
509
510/*
511 * rx/tx dma interrupt handler
512 */
513static irqreturn_t bcm_enet_isr_dma(int irq, void *dev_id)
514{
515 struct net_device *dev;
516 struct bcm_enet_priv *priv;
517
518 dev = dev_id;
519 priv = netdev_priv(dev);
520
521 /* mask rx/tx interrupts */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100522 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->rx_chan));
523 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100524
525 napi_schedule(&priv->napi);
526
527 return IRQ_HANDLED;
528}
529
530/*
531 * tx request callback
532 */
533static int bcm_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
534{
535 struct bcm_enet_priv *priv;
536 struct bcm_enet_desc *desc;
537 u32 len_stat;
538 int ret;
539
540 priv = netdev_priv(dev);
541
542 /* lock against tx reclaim */
543 spin_lock(&priv->tx_lock);
544
545 /* make sure the tx hw queue is not full, should not happen
546 * since we stop queue before it's the case */
547 if (unlikely(!priv->tx_desc_count)) {
548 netif_stop_queue(dev);
549 dev_err(&priv->pdev->dev, "xmit called with no tx desc "
550 "available?\n");
551 ret = NETDEV_TX_BUSY;
552 goto out_unlock;
553 }
554
555 /* point to the next available desc */
556 desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
557 priv->tx_skb[priv->tx_curr_desc] = skb;
558
559 /* fill descriptor */
560 desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
561 DMA_TO_DEVICE);
562
563 len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
564 len_stat |= DMADESC_ESOP_MASK |
565 DMADESC_APPEND_CRC |
566 DMADESC_OWNER_MASK;
567
568 priv->tx_curr_desc++;
569 if (priv->tx_curr_desc == priv->tx_ring_size) {
570 priv->tx_curr_desc = 0;
571 len_stat |= DMADESC_WRAP_MASK;
572 }
573 priv->tx_desc_count--;
574
575 /* dma might be already polling, make sure we update desc
576 * fields in correct order */
577 wmb();
578 desc->len_stat = len_stat;
579 wmb();
580
581 /* kick tx dma */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100582 enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
583 ENETDMAC_CHANCFG_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100584
585 /* stop queue if no more desc available */
586 if (!priv->tx_desc_count)
587 netif_stop_queue(dev);
588
Eric Dumazetc32d83c2010-08-24 12:24:07 -0700589 dev->stats.tx_bytes += skb->len;
590 dev->stats.tx_packets++;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100591 ret = NETDEV_TX_OK;
592
593out_unlock:
594 spin_unlock(&priv->tx_lock);
595 return ret;
596}
597
598/*
599 * Change the interface's mac address.
600 */
601static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
602{
603 struct bcm_enet_priv *priv;
604 struct sockaddr *addr = p;
605 u32 val;
606
607 priv = netdev_priv(dev);
608 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
609
610 /* use perfect match register 0 to store my mac address */
611 val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
612 (dev->dev_addr[4] << 8) | dev->dev_addr[5];
613 enet_writel(priv, val, ENET_PML_REG(0));
614
615 val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
616 val |= ENET_PMH_DATAVALID_MASK;
617 enet_writel(priv, val, ENET_PMH_REG(0));
618
619 return 0;
620}
621
622/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300623 * Change rx mode (promiscuous/allmulti) and update multicast list
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100624 */
625static void bcm_enet_set_multicast_list(struct net_device *dev)
626{
627 struct bcm_enet_priv *priv;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000628 struct netdev_hw_addr *ha;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100629 u32 val;
630 int i;
631
632 priv = netdev_priv(dev);
633
634 val = enet_readl(priv, ENET_RXCFG_REG);
635
636 if (dev->flags & IFF_PROMISC)
637 val |= ENET_RXCFG_PROMISC_MASK;
638 else
639 val &= ~ENET_RXCFG_PROMISC_MASK;
640
641 /* only 3 perfect match registers left, first one is used for
642 * own mac address */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000643 if ((dev->flags & IFF_ALLMULTI) || netdev_mc_count(dev) > 3)
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100644 val |= ENET_RXCFG_ALLMCAST_MASK;
645 else
646 val &= ~ENET_RXCFG_ALLMCAST_MASK;
647
648 /* no need to set perfect match registers if we catch all
649 * multicast */
650 if (val & ENET_RXCFG_ALLMCAST_MASK) {
651 enet_writel(priv, val, ENET_RXCFG_REG);
652 return;
653 }
654
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000655 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000656 netdev_for_each_mc_addr(ha, dev) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100657 u8 *dmi_addr;
658 u32 tmp;
659
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000660 if (i == 3)
661 break;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100662 /* update perfect match registers */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000663 dmi_addr = ha->addr;
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100664 tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
665 (dmi_addr[4] << 8) | dmi_addr[5];
666 enet_writel(priv, tmp, ENET_PML_REG(i + 1));
667
668 tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
669 tmp |= ENET_PMH_DATAVALID_MASK;
Jiri Pirko0ddf4772010-02-20 00:13:58 +0000670 enet_writel(priv, tmp, ENET_PMH_REG(i++ + 1));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100671 }
672
673 for (; i < 3; i++) {
674 enet_writel(priv, 0, ENET_PML_REG(i + 1));
675 enet_writel(priv, 0, ENET_PMH_REG(i + 1));
676 }
677
678 enet_writel(priv, val, ENET_RXCFG_REG);
679}
680
681/*
682 * set mac duplex parameters
683 */
684static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
685{
686 u32 val;
687
688 val = enet_readl(priv, ENET_TXCTL_REG);
689 if (fullduplex)
690 val |= ENET_TXCTL_FD_MASK;
691 else
692 val &= ~ENET_TXCTL_FD_MASK;
693 enet_writel(priv, val, ENET_TXCTL_REG);
694}
695
696/*
697 * set mac flow control parameters
698 */
699static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
700{
701 u32 val;
702
703 /* rx flow control (pause frame handling) */
704 val = enet_readl(priv, ENET_RXCFG_REG);
705 if (rx_en)
706 val |= ENET_RXCFG_ENFLOW_MASK;
707 else
708 val &= ~ENET_RXCFG_ENFLOW_MASK;
709 enet_writel(priv, val, ENET_RXCFG_REG);
710
711 /* tx flow control (pause frame generation) */
712 val = enet_dma_readl(priv, ENETDMA_CFG_REG);
713 if (tx_en)
714 val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
715 else
716 val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
717 enet_dma_writel(priv, val, ENETDMA_CFG_REG);
718}
719
720/*
721 * link changed callback (from phylib)
722 */
723static void bcm_enet_adjust_phy_link(struct net_device *dev)
724{
725 struct bcm_enet_priv *priv;
726 struct phy_device *phydev;
727 int status_changed;
728
729 priv = netdev_priv(dev);
730 phydev = priv->phydev;
731 status_changed = 0;
732
733 if (priv->old_link != phydev->link) {
734 status_changed = 1;
735 priv->old_link = phydev->link;
736 }
737
738 /* reflect duplex change in mac configuration */
739 if (phydev->link && phydev->duplex != priv->old_duplex) {
740 bcm_enet_set_duplex(priv,
741 (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
742 status_changed = 1;
743 priv->old_duplex = phydev->duplex;
744 }
745
746 /* enable flow control if remote advertise it (trust phylib to
747 * check that duplex is full */
748 if (phydev->link && phydev->pause != priv->old_pause) {
749 int rx_pause_en, tx_pause_en;
750
751 if (phydev->pause) {
752 /* pause was advertised by lpa and us */
753 rx_pause_en = 1;
754 tx_pause_en = 1;
755 } else if (!priv->pause_auto) {
756 /* pause setting overrided by user */
757 rx_pause_en = priv->pause_rx;
758 tx_pause_en = priv->pause_tx;
759 } else {
760 rx_pause_en = 0;
761 tx_pause_en = 0;
762 }
763
764 bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
765 status_changed = 1;
766 priv->old_pause = phydev->pause;
767 }
768
769 if (status_changed) {
770 pr_info("%s: link %s", dev->name, phydev->link ?
771 "UP" : "DOWN");
772 if (phydev->link)
773 pr_cont(" - %d/%s - flow control %s", phydev->speed,
774 DUPLEX_FULL == phydev->duplex ? "full" : "half",
775 phydev->pause == 1 ? "rx&tx" : "off");
776
777 pr_cont("\n");
778 }
779}
780
781/*
782 * link changed callback (if phylib is not used)
783 */
784static void bcm_enet_adjust_link(struct net_device *dev)
785{
786 struct bcm_enet_priv *priv;
787
788 priv = netdev_priv(dev);
789 bcm_enet_set_duplex(priv, priv->force_duplex_full);
790 bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
791 netif_carrier_on(dev);
792
793 pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
794 dev->name,
795 priv->force_speed_100 ? 100 : 10,
796 priv->force_duplex_full ? "full" : "half",
797 priv->pause_rx ? "rx" : "off",
798 priv->pause_tx ? "tx" : "off");
799}
800
801/*
802 * open callback, allocate dma rings & buffers and start rx operation
803 */
804static int bcm_enet_open(struct net_device *dev)
805{
806 struct bcm_enet_priv *priv;
807 struct sockaddr addr;
808 struct device *kdev;
809 struct phy_device *phydev;
810 int i, ret;
811 unsigned int size;
812 char phy_id[MII_BUS_ID_SIZE + 3];
813 void *p;
814 u32 val;
815
816 priv = netdev_priv(dev);
817 kdev = &priv->pdev->dev;
818
819 if (priv->has_phy) {
820 /* connect to PHY */
821 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
Florian Fainellic56e9e22012-02-13 01:23:21 +0000822 priv->mii_bus->id, priv->phy_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100823
Florian Fainellif9a8f832013-01-14 00:52:52 +0000824 phydev = phy_connect(dev, phy_id, bcm_enet_adjust_phy_link,
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100825 PHY_INTERFACE_MODE_MII);
826
827 if (IS_ERR(phydev)) {
828 dev_err(kdev, "could not attach to PHY\n");
829 return PTR_ERR(phydev);
830 }
831
832 /* mask with MAC supported features */
833 phydev->supported &= (SUPPORTED_10baseT_Half |
834 SUPPORTED_10baseT_Full |
835 SUPPORTED_100baseT_Half |
836 SUPPORTED_100baseT_Full |
837 SUPPORTED_Autoneg |
838 SUPPORTED_Pause |
839 SUPPORTED_MII);
840 phydev->advertising = phydev->supported;
841
842 if (priv->pause_auto && priv->pause_rx && priv->pause_tx)
843 phydev->advertising |= SUPPORTED_Pause;
844 else
845 phydev->advertising &= ~SUPPORTED_Pause;
846
847 dev_info(kdev, "attached PHY at address %d [%s]\n",
848 phydev->addr, phydev->drv->name);
849
850 priv->old_link = 0;
851 priv->old_duplex = -1;
852 priv->old_pause = -1;
853 priv->phydev = phydev;
854 }
855
856 /* mask all interrupts and request them */
857 enet_writel(priv, 0, ENET_IRMASK_REG);
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100858 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->rx_chan));
859 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100860
861 ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
862 if (ret)
863 goto out_phy_disconnect;
864
Javier Martinez Canillasab392d22011-03-28 16:27:31 +0000865 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma, IRQF_DISABLED,
866 dev->name, dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100867 if (ret)
868 goto out_freeirq;
869
870 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
871 IRQF_DISABLED, dev->name, dev);
872 if (ret)
873 goto out_freeirq_rx;
874
875 /* initialize perfect match registers */
876 for (i = 0; i < 4; i++) {
877 enet_writel(priv, 0, ENET_PML_REG(i));
878 enet_writel(priv, 0, ENET_PMH_REG(i));
879 }
880
881 /* write device mac address */
882 memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
883 bcm_enet_set_mac_address(dev, &addr);
884
885 /* allocate rx dma ring */
886 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perches1f9061d22013-03-15 07:23:58 +0000887 p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma,
888 GFP_KERNEL | __GFP_ZERO);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100889 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100890 ret = -ENOMEM;
891 goto out_freeirq_tx;
892 }
893
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100894 priv->rx_desc_alloc_size = size;
895 priv->rx_desc_cpu = p;
896
897 /* allocate tx dma ring */
898 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
Joe Perches1f9061d22013-03-15 07:23:58 +0000899 p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma,
900 GFP_KERNEL | __GFP_ZERO);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100901 if (!p) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100902 ret = -ENOMEM;
903 goto out_free_rx_ring;
904 }
905
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100906 priv->tx_desc_alloc_size = size;
907 priv->tx_desc_cpu = p;
908
Joe Perchesb2adaca2013-02-03 17:43:58 +0000909 priv->tx_skb = kcalloc(priv->tx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100910 GFP_KERNEL);
911 if (!priv->tx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100912 ret = -ENOMEM;
913 goto out_free_tx_ring;
914 }
915
916 priv->tx_desc_count = priv->tx_ring_size;
917 priv->tx_dirty_desc = 0;
918 priv->tx_curr_desc = 0;
919 spin_lock_init(&priv->tx_lock);
920
921 /* init & fill rx ring with skbs */
Joe Perchesb2adaca2013-02-03 17:43:58 +0000922 priv->rx_skb = kcalloc(priv->rx_ring_size, sizeof(struct sk_buff *),
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100923 GFP_KERNEL);
924 if (!priv->rx_skb) {
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100925 ret = -ENOMEM;
926 goto out_free_tx_skb;
927 }
928
929 priv->rx_desc_count = 0;
930 priv->rx_dirty_desc = 0;
931 priv->rx_curr_desc = 0;
932
933 /* initialize flow control buffer allocation */
934 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
935 ENETDMA_BUFALLOC_REG(priv->rx_chan));
936
937 if (bcm_enet_refill_rx(dev)) {
938 dev_err(kdev, "cannot allocate rx skb queue\n");
939 ret = -ENOMEM;
940 goto out;
941 }
942
943 /* write rx & tx ring addresses */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100944 enet_dmas_writel(priv, priv->rx_desc_dma,
945 ENETDMAS_RSTART_REG(priv->rx_chan));
946 enet_dmas_writel(priv, priv->tx_desc_dma,
947 ENETDMAS_RSTART_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100948
949 /* clear remaining state ram for rx & tx channel */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100950 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG(priv->rx_chan));
951 enet_dmas_writel(priv, 0, ENETDMAS_SRAM2_REG(priv->tx_chan));
952 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG(priv->rx_chan));
953 enet_dmas_writel(priv, 0, ENETDMAS_SRAM3_REG(priv->tx_chan));
954 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG(priv->rx_chan));
955 enet_dmas_writel(priv, 0, ENETDMAS_SRAM4_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100956
957 /* set max rx/tx length */
958 enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
959 enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
960
961 /* set dma maximum burst len */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100962 enet_dmac_writel(priv, BCMENET_DMA_MAXBURST,
963 ENETDMAC_MAXBURST_REG(priv->rx_chan));
964 enet_dmac_writel(priv, BCMENET_DMA_MAXBURST,
965 ENETDMAC_MAXBURST_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100966
967 /* set correct transmit fifo watermark */
968 enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
969
970 /* set flow control low/high threshold to 1/3 / 2/3 */
971 val = priv->rx_ring_size / 3;
972 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
973 val = (priv->rx_ring_size * 2) / 3;
974 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
975
976 /* all set, enable mac and interrupts, start dma engine and
977 * kick rx dma channel */
978 wmb();
Florian Fainelli5e10d4a2010-04-09 01:04:52 +0000979 val = enet_readl(priv, ENET_CTL_REG);
980 val |= ENET_CTL_ENABLE_MASK;
981 enet_writel(priv, val, ENET_CTL_REG);
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100982 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100983 enet_dmac_writel(priv, ENETDMAC_CHANCFG_EN_MASK,
984 ENETDMAC_CHANCFG_REG(priv->rx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100985
986 /* watch "mib counters about to overflow" interrupt */
987 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
988 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
989
990 /* watch "packet transferred" interrupt in rx and tx */
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100991 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
992 ENETDMAC_IR_REG(priv->rx_chan));
993 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
994 ENETDMAC_IR_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +0100995
996 /* make sure we enable napi before rx interrupt */
997 napi_enable(&priv->napi);
998
Maxime Bizon0ae99b52013-06-04 22:53:34 +0100999 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
1000 ENETDMAC_IRMASK_REG(priv->rx_chan));
1001 enet_dmac_writel(priv, ENETDMAC_IR_PKTDONE_MASK,
1002 ENETDMAC_IRMASK_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001003
1004 if (priv->has_phy)
1005 phy_start(priv->phydev);
1006 else
1007 bcm_enet_adjust_link(dev);
1008
1009 netif_start_queue(dev);
1010 return 0;
1011
1012out:
1013 for (i = 0; i < priv->rx_ring_size; i++) {
1014 struct bcm_enet_desc *desc;
1015
1016 if (!priv->rx_skb[i])
1017 continue;
1018
1019 desc = &priv->rx_desc_cpu[i];
1020 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1021 DMA_FROM_DEVICE);
1022 kfree_skb(priv->rx_skb[i]);
1023 }
1024 kfree(priv->rx_skb);
1025
1026out_free_tx_skb:
1027 kfree(priv->tx_skb);
1028
1029out_free_tx_ring:
1030 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1031 priv->tx_desc_cpu, priv->tx_desc_dma);
1032
1033out_free_rx_ring:
1034 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1035 priv->rx_desc_cpu, priv->rx_desc_dma);
1036
1037out_freeirq_tx:
1038 free_irq(priv->irq_tx, dev);
1039
1040out_freeirq_rx:
1041 free_irq(priv->irq_rx, dev);
1042
1043out_freeirq:
1044 free_irq(dev->irq, dev);
1045
1046out_phy_disconnect:
1047 phy_disconnect(priv->phydev);
1048
1049 return ret;
1050}
1051
1052/*
1053 * disable mac
1054 */
1055static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
1056{
1057 int limit;
1058 u32 val;
1059
1060 val = enet_readl(priv, ENET_CTL_REG);
1061 val |= ENET_CTL_DISABLE_MASK;
1062 enet_writel(priv, val, ENET_CTL_REG);
1063
1064 limit = 1000;
1065 do {
1066 u32 val;
1067
1068 val = enet_readl(priv, ENET_CTL_REG);
1069 if (!(val & ENET_CTL_DISABLE_MASK))
1070 break;
1071 udelay(1);
1072 } while (limit--);
1073}
1074
1075/*
1076 * disable dma in given channel
1077 */
1078static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
1079{
1080 int limit;
1081
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001082 enet_dmac_writel(priv, 0, ENETDMAC_CHANCFG_REG(chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001083
1084 limit = 1000;
1085 do {
1086 u32 val;
1087
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001088 val = enet_dmac_readl(priv, ENETDMAC_CHANCFG_REG(chan));
1089 if (!(val & ENETDMAC_CHANCFG_EN_MASK))
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001090 break;
1091 udelay(1);
1092 } while (limit--);
1093}
1094
1095/*
1096 * stop callback
1097 */
1098static int bcm_enet_stop(struct net_device *dev)
1099{
1100 struct bcm_enet_priv *priv;
1101 struct device *kdev;
1102 int i;
1103
1104 priv = netdev_priv(dev);
1105 kdev = &priv->pdev->dev;
1106
1107 netif_stop_queue(dev);
1108 napi_disable(&priv->napi);
1109 if (priv->has_phy)
1110 phy_stop(priv->phydev);
1111 del_timer_sync(&priv->rx_timeout);
1112
1113 /* mask all interrupts */
1114 enet_writel(priv, 0, ENET_IRMASK_REG);
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001115 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->rx_chan));
1116 enet_dmac_writel(priv, 0, ENETDMAC_IRMASK_REG(priv->tx_chan));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001117
1118 /* make sure no mib update is scheduled */
Tejun Heo23f333a2010-12-12 16:45:14 +01001119 cancel_work_sync(&priv->mib_update_task);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001120
1121 /* disable dma & mac */
1122 bcm_enet_disable_dma(priv, priv->tx_chan);
1123 bcm_enet_disable_dma(priv, priv->rx_chan);
1124 bcm_enet_disable_mac(priv);
1125
1126 /* force reclaim of all tx buffers */
1127 bcm_enet_tx_reclaim(dev, 1);
1128
1129 /* free the rx skb ring */
1130 for (i = 0; i < priv->rx_ring_size; i++) {
1131 struct bcm_enet_desc *desc;
1132
1133 if (!priv->rx_skb[i])
1134 continue;
1135
1136 desc = &priv->rx_desc_cpu[i];
1137 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1138 DMA_FROM_DEVICE);
1139 kfree_skb(priv->rx_skb[i]);
1140 }
1141
1142 /* free remaining allocated memory */
1143 kfree(priv->rx_skb);
1144 kfree(priv->tx_skb);
1145 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1146 priv->rx_desc_cpu, priv->rx_desc_dma);
1147 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1148 priv->tx_desc_cpu, priv->tx_desc_dma);
1149 free_irq(priv->irq_tx, dev);
1150 free_irq(priv->irq_rx, dev);
1151 free_irq(dev->irq, dev);
1152
1153 /* release phy */
1154 if (priv->has_phy) {
1155 phy_disconnect(priv->phydev);
1156 priv->phydev = NULL;
1157 }
1158
1159 return 0;
1160}
1161
1162/*
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001163 * ethtool callbacks
1164 */
1165struct bcm_enet_stats {
1166 char stat_string[ETH_GSTRING_LEN];
1167 int sizeof_stat;
1168 int stat_offset;
1169 int mib_reg;
1170};
1171
1172#define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
1173 offsetof(struct bcm_enet_priv, m)
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001174#define DEV_STAT(m) sizeof(((struct net_device_stats *)0)->m), \
1175 offsetof(struct net_device_stats, m)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001176
1177static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001178 { "rx_packets", DEV_STAT(rx_packets), -1 },
1179 { "tx_packets", DEV_STAT(tx_packets), -1 },
1180 { "rx_bytes", DEV_STAT(rx_bytes), -1 },
1181 { "tx_bytes", DEV_STAT(tx_bytes), -1 },
1182 { "rx_errors", DEV_STAT(rx_errors), -1 },
1183 { "tx_errors", DEV_STAT(tx_errors), -1 },
1184 { "rx_dropped", DEV_STAT(rx_dropped), -1 },
1185 { "tx_dropped", DEV_STAT(tx_dropped), -1 },
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001186
1187 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
1188 { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
1189 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
1190 { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
1191 { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
1192 { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
1193 { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
1194 { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
1195 { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
1196 { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
1197 { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
1198 { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
1199 { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
1200 { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
1201 { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
1202 { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
1203 { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
1204 { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
1205 { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
1206 { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
1207 { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
1208
1209 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
1210 { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
1211 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
1212 { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
1213 { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
1214 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
1215 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
1216 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
1217 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
1218 { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
1219 { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
1220 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
1221 { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
1222 { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
1223 { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
1224 { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
1225 { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
1226 { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
1227 { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
1228 { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
1229 { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
1230 { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
1231
1232};
1233
1234#define BCM_ENET_STATS_LEN \
1235 (sizeof(bcm_enet_gstrings_stats) / sizeof(struct bcm_enet_stats))
1236
1237static const u32 unused_mib_regs[] = {
1238 ETH_MIB_TX_ALL_OCTETS,
1239 ETH_MIB_TX_ALL_PKTS,
1240 ETH_MIB_RX_ALL_OCTETS,
1241 ETH_MIB_RX_ALL_PKTS,
1242};
1243
1244
1245static void bcm_enet_get_drvinfo(struct net_device *netdev,
1246 struct ethtool_drvinfo *drvinfo)
1247{
Jiri Pirko7826d432013-01-06 00:44:26 +00001248 strlcpy(drvinfo->driver, bcm_enet_driver_name, sizeof(drvinfo->driver));
1249 strlcpy(drvinfo->version, bcm_enet_driver_version,
1250 sizeof(drvinfo->version));
1251 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1252 strlcpy(drvinfo->bus_info, "bcm63xx", sizeof(drvinfo->bus_info));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001253 drvinfo->n_stats = BCM_ENET_STATS_LEN;
1254}
1255
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001256static int bcm_enet_get_sset_count(struct net_device *netdev,
1257 int string_set)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001258{
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001259 switch (string_set) {
1260 case ETH_SS_STATS:
1261 return BCM_ENET_STATS_LEN;
1262 default:
1263 return -EINVAL;
1264 }
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001265}
1266
1267static void bcm_enet_get_strings(struct net_device *netdev,
1268 u32 stringset, u8 *data)
1269{
1270 int i;
1271
1272 switch (stringset) {
1273 case ETH_SS_STATS:
1274 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1275 memcpy(data + i * ETH_GSTRING_LEN,
1276 bcm_enet_gstrings_stats[i].stat_string,
1277 ETH_GSTRING_LEN);
1278 }
1279 break;
1280 }
1281}
1282
1283static void update_mib_counters(struct bcm_enet_priv *priv)
1284{
1285 int i;
1286
1287 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1288 const struct bcm_enet_stats *s;
1289 u32 val;
1290 char *p;
1291
1292 s = &bcm_enet_gstrings_stats[i];
1293 if (s->mib_reg == -1)
1294 continue;
1295
1296 val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
1297 p = (char *)priv + s->stat_offset;
1298
1299 if (s->sizeof_stat == sizeof(u64))
1300 *(u64 *)p += val;
1301 else
1302 *(u32 *)p += val;
1303 }
1304
1305 /* also empty unused mib counters to make sure mib counter
1306 * overflow interrupt is cleared */
1307 for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
1308 (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
1309}
1310
1311static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
1312{
1313 struct bcm_enet_priv *priv;
1314
1315 priv = container_of(t, struct bcm_enet_priv, mib_update_task);
1316 mutex_lock(&priv->mib_update_lock);
1317 update_mib_counters(priv);
1318 mutex_unlock(&priv->mib_update_lock);
1319
1320 /* reenable mib interrupt */
1321 if (netif_running(priv->net_dev))
1322 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1323}
1324
1325static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
1326 struct ethtool_stats *stats,
1327 u64 *data)
1328{
1329 struct bcm_enet_priv *priv;
1330 int i;
1331
1332 priv = netdev_priv(netdev);
1333
1334 mutex_lock(&priv->mib_update_lock);
1335 update_mib_counters(priv);
1336
1337 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1338 const struct bcm_enet_stats *s;
1339 char *p;
1340
1341 s = &bcm_enet_gstrings_stats[i];
Eric Dumazetc32d83c2010-08-24 12:24:07 -07001342 if (s->mib_reg == -1)
1343 p = (char *)&netdev->stats;
1344 else
1345 p = (char *)priv;
1346 p += s->stat_offset;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001347 data[i] = (s->sizeof_stat == sizeof(u64)) ?
1348 *(u64 *)p : *(u32 *)p;
1349 }
1350 mutex_unlock(&priv->mib_update_lock);
1351}
1352
Maxime Bizon7260aac2013-06-04 22:53:33 +01001353static int bcm_enet_nway_reset(struct net_device *dev)
1354{
1355 struct bcm_enet_priv *priv;
1356
1357 priv = netdev_priv(dev);
1358 if (priv->has_phy) {
1359 if (!priv->phydev)
1360 return -ENODEV;
1361 return genphy_restart_aneg(priv->phydev);
1362 }
1363
1364 return -EOPNOTSUPP;
1365}
1366
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001367static int bcm_enet_get_settings(struct net_device *dev,
1368 struct ethtool_cmd *cmd)
1369{
1370 struct bcm_enet_priv *priv;
1371
1372 priv = netdev_priv(dev);
1373
1374 cmd->maxrxpkt = 0;
1375 cmd->maxtxpkt = 0;
1376
1377 if (priv->has_phy) {
1378 if (!priv->phydev)
1379 return -ENODEV;
1380 return phy_ethtool_gset(priv->phydev, cmd);
1381 } else {
1382 cmd->autoneg = 0;
David Decotigny70739492011-04-27 18:32:40 +00001383 ethtool_cmd_speed_set(cmd, ((priv->force_speed_100)
1384 ? SPEED_100 : SPEED_10));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001385 cmd->duplex = (priv->force_duplex_full) ?
1386 DUPLEX_FULL : DUPLEX_HALF;
1387 cmd->supported = ADVERTISED_10baseT_Half |
1388 ADVERTISED_10baseT_Full |
1389 ADVERTISED_100baseT_Half |
1390 ADVERTISED_100baseT_Full;
1391 cmd->advertising = 0;
1392 cmd->port = PORT_MII;
1393 cmd->transceiver = XCVR_EXTERNAL;
1394 }
1395 return 0;
1396}
1397
1398static int bcm_enet_set_settings(struct net_device *dev,
1399 struct ethtool_cmd *cmd)
1400{
1401 struct bcm_enet_priv *priv;
1402
1403 priv = netdev_priv(dev);
1404 if (priv->has_phy) {
1405 if (!priv->phydev)
1406 return -ENODEV;
1407 return phy_ethtool_sset(priv->phydev, cmd);
1408 } else {
1409
1410 if (cmd->autoneg ||
1411 (cmd->speed != SPEED_100 && cmd->speed != SPEED_10) ||
1412 cmd->port != PORT_MII)
1413 return -EINVAL;
1414
1415 priv->force_speed_100 = (cmd->speed == SPEED_100) ? 1 : 0;
1416 priv->force_duplex_full = (cmd->duplex == DUPLEX_FULL) ? 1 : 0;
1417
1418 if (netif_running(dev))
1419 bcm_enet_adjust_link(dev);
1420 return 0;
1421 }
1422}
1423
1424static void bcm_enet_get_ringparam(struct net_device *dev,
1425 struct ethtool_ringparam *ering)
1426{
1427 struct bcm_enet_priv *priv;
1428
1429 priv = netdev_priv(dev);
1430
1431 /* rx/tx ring is actually only limited by memory */
1432 ering->rx_max_pending = 8192;
1433 ering->tx_max_pending = 8192;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001434 ering->rx_pending = priv->rx_ring_size;
1435 ering->tx_pending = priv->tx_ring_size;
1436}
1437
1438static int bcm_enet_set_ringparam(struct net_device *dev,
1439 struct ethtool_ringparam *ering)
1440{
1441 struct bcm_enet_priv *priv;
1442 int was_running;
1443
1444 priv = netdev_priv(dev);
1445
1446 was_running = 0;
1447 if (netif_running(dev)) {
1448 bcm_enet_stop(dev);
1449 was_running = 1;
1450 }
1451
1452 priv->rx_ring_size = ering->rx_pending;
1453 priv->tx_ring_size = ering->tx_pending;
1454
1455 if (was_running) {
1456 int err;
1457
1458 err = bcm_enet_open(dev);
1459 if (err)
1460 dev_close(dev);
1461 else
1462 bcm_enet_set_multicast_list(dev);
1463 }
1464 return 0;
1465}
1466
1467static void bcm_enet_get_pauseparam(struct net_device *dev,
1468 struct ethtool_pauseparam *ecmd)
1469{
1470 struct bcm_enet_priv *priv;
1471
1472 priv = netdev_priv(dev);
1473 ecmd->autoneg = priv->pause_auto;
1474 ecmd->rx_pause = priv->pause_rx;
1475 ecmd->tx_pause = priv->pause_tx;
1476}
1477
1478static int bcm_enet_set_pauseparam(struct net_device *dev,
1479 struct ethtool_pauseparam *ecmd)
1480{
1481 struct bcm_enet_priv *priv;
1482
1483 priv = netdev_priv(dev);
1484
1485 if (priv->has_phy) {
1486 if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
1487 /* asymetric pause mode not supported,
1488 * actually possible but integrated PHY has RO
1489 * asym_pause bit */
1490 return -EINVAL;
1491 }
1492 } else {
1493 /* no pause autoneg on direct mii connection */
1494 if (ecmd->autoneg)
1495 return -EINVAL;
1496 }
1497
1498 priv->pause_auto = ecmd->autoneg;
1499 priv->pause_rx = ecmd->rx_pause;
1500 priv->pause_tx = ecmd->tx_pause;
1501
1502 return 0;
1503}
1504
stephen hemminger1aff0cb2012-01-05 19:10:24 +00001505static const struct ethtool_ops bcm_enet_ethtool_ops = {
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001506 .get_strings = bcm_enet_get_strings,
Florian Fainellia3f92ee2009-12-15 06:45:06 +00001507 .get_sset_count = bcm_enet_get_sset_count,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001508 .get_ethtool_stats = bcm_enet_get_ethtool_stats,
Maxime Bizon7260aac2013-06-04 22:53:33 +01001509 .nway_reset = bcm_enet_nway_reset,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001510 .get_settings = bcm_enet_get_settings,
1511 .set_settings = bcm_enet_set_settings,
1512 .get_drvinfo = bcm_enet_get_drvinfo,
1513 .get_link = ethtool_op_get_link,
1514 .get_ringparam = bcm_enet_get_ringparam,
1515 .set_ringparam = bcm_enet_set_ringparam,
1516 .get_pauseparam = bcm_enet_get_pauseparam,
1517 .set_pauseparam = bcm_enet_set_pauseparam,
1518};
1519
1520static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1521{
1522 struct bcm_enet_priv *priv;
1523
1524 priv = netdev_priv(dev);
1525 if (priv->has_phy) {
1526 if (!priv->phydev)
1527 return -ENODEV;
Richard Cochran28b04112010-07-17 08:48:55 +00001528 return phy_mii_ioctl(priv->phydev, rq, cmd);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001529 } else {
1530 struct mii_if_info mii;
1531
1532 mii.dev = dev;
1533 mii.mdio_read = bcm_enet_mdio_read_mii;
1534 mii.mdio_write = bcm_enet_mdio_write_mii;
1535 mii.phy_id = 0;
1536 mii.phy_id_mask = 0x3f;
1537 mii.reg_num_mask = 0x1f;
1538 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
1539 }
1540}
1541
1542/*
1543 * calculate actual hardware mtu
1544 */
1545static int compute_hw_mtu(struct bcm_enet_priv *priv, int mtu)
1546{
1547 int actual_mtu;
1548
1549 actual_mtu = mtu;
1550
1551 /* add ethernet header + vlan tag size */
1552 actual_mtu += VLAN_ETH_HLEN;
1553
1554 if (actual_mtu < 64 || actual_mtu > BCMENET_MAX_MTU)
1555 return -EINVAL;
1556
1557 /*
1558 * setup maximum size before we get overflow mark in
1559 * descriptor, note that this will not prevent reception of
1560 * big frames, they will be split into multiple buffers
1561 * anyway
1562 */
1563 priv->hw_mtu = actual_mtu;
1564
1565 /*
1566 * align rx buffer size to dma burst len, account FCS since
1567 * it's appended
1568 */
1569 priv->rx_skb_size = ALIGN(actual_mtu + ETH_FCS_LEN,
1570 BCMENET_DMA_MAXBURST * 4);
1571 return 0;
1572}
1573
1574/*
1575 * adjust mtu, can't be called while device is running
1576 */
1577static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
1578{
1579 int ret;
1580
1581 if (netif_running(dev))
1582 return -EBUSY;
1583
1584 ret = compute_hw_mtu(netdev_priv(dev), new_mtu);
1585 if (ret)
1586 return ret;
1587 dev->mtu = new_mtu;
1588 return 0;
1589}
1590
1591/*
1592 * preinit hardware to allow mii operation while device is down
1593 */
1594static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
1595{
1596 u32 val;
1597 int limit;
1598
1599 /* make sure mac is disabled */
1600 bcm_enet_disable_mac(priv);
1601
1602 /* soft reset mac */
1603 val = ENET_CTL_SRESET_MASK;
1604 enet_writel(priv, val, ENET_CTL_REG);
1605 wmb();
1606
1607 limit = 1000;
1608 do {
1609 val = enet_readl(priv, ENET_CTL_REG);
1610 if (!(val & ENET_CTL_SRESET_MASK))
1611 break;
1612 udelay(1);
1613 } while (limit--);
1614
1615 /* select correct mii interface */
1616 val = enet_readl(priv, ENET_CTL_REG);
1617 if (priv->use_external_mii)
1618 val |= ENET_CTL_EPHYSEL_MASK;
1619 else
1620 val &= ~ENET_CTL_EPHYSEL_MASK;
1621 enet_writel(priv, val, ENET_CTL_REG);
1622
1623 /* turn on mdc clock */
1624 enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
1625 ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
1626
1627 /* set mib counters to self-clear when read */
1628 val = enet_readl(priv, ENET_MIBCTL_REG);
1629 val |= ENET_MIBCTL_RDCLEAR_MASK;
1630 enet_writel(priv, val, ENET_MIBCTL_REG);
1631}
1632
1633static const struct net_device_ops bcm_enet_ops = {
1634 .ndo_open = bcm_enet_open,
1635 .ndo_stop = bcm_enet_stop,
1636 .ndo_start_xmit = bcm_enet_start_xmit,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001637 .ndo_set_mac_address = bcm_enet_set_mac_address,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001638 .ndo_set_rx_mode = bcm_enet_set_multicast_list,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001639 .ndo_do_ioctl = bcm_enet_ioctl,
1640 .ndo_change_mtu = bcm_enet_change_mtu,
1641#ifdef CONFIG_NET_POLL_CONTROLLER
1642 .ndo_poll_controller = bcm_enet_netpoll,
1643#endif
1644};
1645
1646/*
1647 * allocate netdevice, request register memory and register device.
1648 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001649static int bcm_enet_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001650{
1651 struct bcm_enet_priv *priv;
1652 struct net_device *dev;
1653 struct bcm63xx_enet_platform_data *pd;
1654 struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
1655 struct mii_bus *bus;
1656 const char *clk_name;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001657 int i, ret;
1658
1659 /* stop if shared driver failed, assume driver->probe will be
1660 * called in the same order we register devices (correct ?) */
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001661 if (!bcm_enet_shared_base[0])
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001662 return -ENODEV;
1663
1664 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1665 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1666 res_irq_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1667 res_irq_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
1668 if (!res_mem || !res_irq || !res_irq_rx || !res_irq_tx)
1669 return -ENODEV;
1670
1671 ret = 0;
1672 dev = alloc_etherdev(sizeof(*priv));
1673 if (!dev)
1674 return -ENOMEM;
1675 priv = netdev_priv(dev);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001676
1677 ret = compute_hw_mtu(priv, dev->mtu);
1678 if (ret)
1679 goto out;
1680
Jonas Gorski1c03da02013-03-10 03:57:47 +00001681 priv->base = devm_request_and_ioremap(&pdev->dev, res_mem);
1682 if (priv->base == NULL) {
1683 ret = -ENOMEM;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001684 goto out;
1685 }
1686
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001687 dev->irq = priv->irq = res_irq->start;
1688 priv->irq_rx = res_irq_rx->start;
1689 priv->irq_tx = res_irq_tx->start;
1690 priv->mac_id = pdev->id;
1691
1692 /* get rx & tx dma channel id for this mac */
1693 if (priv->mac_id == 0) {
1694 priv->rx_chan = 0;
1695 priv->tx_chan = 1;
1696 clk_name = "enet0";
1697 } else {
1698 priv->rx_chan = 2;
1699 priv->tx_chan = 3;
1700 clk_name = "enet1";
1701 }
1702
1703 priv->mac_clk = clk_get(&pdev->dev, clk_name);
1704 if (IS_ERR(priv->mac_clk)) {
1705 ret = PTR_ERR(priv->mac_clk);
Jonas Gorski1c03da02013-03-10 03:57:47 +00001706 goto out;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001707 }
Jonas Gorski624e2d22013-03-10 03:57:49 +00001708 clk_prepare_enable(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001709
1710 /* initialize default and fetch platform data */
1711 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
1712 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
1713
1714 pd = pdev->dev.platform_data;
1715 if (pd) {
1716 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
1717 priv->has_phy = pd->has_phy;
1718 priv->phy_id = pd->phy_id;
1719 priv->has_phy_interrupt = pd->has_phy_interrupt;
1720 priv->phy_interrupt = pd->phy_interrupt;
1721 priv->use_external_mii = !pd->use_internal_phy;
1722 priv->pause_auto = pd->pause_auto;
1723 priv->pause_rx = pd->pause_rx;
1724 priv->pause_tx = pd->pause_tx;
1725 priv->force_duplex_full = pd->force_duplex_full;
1726 priv->force_speed_100 = pd->force_speed_100;
1727 }
1728
1729 if (priv->mac_id == 0 && priv->has_phy && !priv->use_external_mii) {
1730 /* using internal PHY, enable clock */
1731 priv->phy_clk = clk_get(&pdev->dev, "ephy");
1732 if (IS_ERR(priv->phy_clk)) {
1733 ret = PTR_ERR(priv->phy_clk);
1734 priv->phy_clk = NULL;
1735 goto out_put_clk_mac;
1736 }
Jonas Gorski624e2d22013-03-10 03:57:49 +00001737 clk_prepare_enable(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001738 }
1739
1740 /* do minimal hardware init to be able to probe mii bus */
1741 bcm_enet_hw_preinit(priv);
1742
1743 /* MII bus registration */
1744 if (priv->has_phy) {
1745
1746 priv->mii_bus = mdiobus_alloc();
1747 if (!priv->mii_bus) {
1748 ret = -ENOMEM;
1749 goto out_uninit_hw;
1750 }
1751
1752 bus = priv->mii_bus;
1753 bus->name = "bcm63xx_enet MII bus";
1754 bus->parent = &pdev->dev;
1755 bus->priv = priv;
1756 bus->read = bcm_enet_mdio_read_phylib;
1757 bus->write = bcm_enet_mdio_write_phylib;
Florian Fainelli3e617502012-01-09 23:59:24 +00001758 sprintf(bus->id, "%s-%d", pdev->name, priv->mac_id);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001759
1760 /* only probe bus where we think the PHY is, because
1761 * the mdio read operation return 0 instead of 0xffff
1762 * if a slave is not present on hw */
1763 bus->phy_mask = ~(1 << priv->phy_id);
1764
Jonas Gorski2a80b5e2013-03-10 03:57:48 +00001765 bus->irq = devm_kzalloc(&pdev->dev, sizeof(int) * PHY_MAX_ADDR,
1766 GFP_KERNEL);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001767 if (!bus->irq) {
1768 ret = -ENOMEM;
1769 goto out_free_mdio;
1770 }
1771
1772 if (priv->has_phy_interrupt)
1773 bus->irq[priv->phy_id] = priv->phy_interrupt;
1774 else
1775 bus->irq[priv->phy_id] = PHY_POLL;
1776
1777 ret = mdiobus_register(bus);
1778 if (ret) {
1779 dev_err(&pdev->dev, "unable to register mdio bus\n");
1780 goto out_free_mdio;
1781 }
1782 } else {
1783
1784 /* run platform code to initialize PHY device */
1785 if (pd->mii_config &&
1786 pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
1787 bcm_enet_mdio_write_mii)) {
1788 dev_err(&pdev->dev, "unable to configure mdio bus\n");
1789 goto out_uninit_hw;
1790 }
1791 }
1792
1793 spin_lock_init(&priv->rx_lock);
1794
1795 /* init rx timeout (used for oom) */
1796 init_timer(&priv->rx_timeout);
1797 priv->rx_timeout.function = bcm_enet_refill_rx_timer;
1798 priv->rx_timeout.data = (unsigned long)dev;
1799
1800 /* init the mib update lock&work */
1801 mutex_init(&priv->mib_update_lock);
1802 INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
1803
1804 /* zero mib counters */
1805 for (i = 0; i < ENET_MIB_REG_COUNT; i++)
1806 enet_writel(priv, 0, ENET_MIB_REG(i));
1807
1808 /* register netdevice */
1809 dev->netdev_ops = &bcm_enet_ops;
1810 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
1811
1812 SET_ETHTOOL_OPS(dev, &bcm_enet_ethtool_ops);
1813 SET_NETDEV_DEV(dev, &pdev->dev);
1814
1815 ret = register_netdev(dev);
1816 if (ret)
1817 goto out_unregister_mdio;
1818
1819 netif_carrier_off(dev);
1820 platform_set_drvdata(pdev, dev);
1821 priv->pdev = pdev;
1822 priv->net_dev = dev;
1823
1824 return 0;
1825
1826out_unregister_mdio:
Jonas Gorski2a80b5e2013-03-10 03:57:48 +00001827 if (priv->mii_bus)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001828 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001829
1830out_free_mdio:
1831 if (priv->mii_bus)
1832 mdiobus_free(priv->mii_bus);
1833
1834out_uninit_hw:
1835 /* turn off mdc clock */
1836 enet_writel(priv, 0, ENET_MIISC_REG);
1837 if (priv->phy_clk) {
Jonas Gorski624e2d22013-03-10 03:57:49 +00001838 clk_disable_unprepare(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001839 clk_put(priv->phy_clk);
1840 }
1841
1842out_put_clk_mac:
Jonas Gorski624e2d22013-03-10 03:57:49 +00001843 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001844 clk_put(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001845out:
1846 free_netdev(dev);
1847 return ret;
1848}
1849
1850
1851/*
1852 * exit func, stops hardware and unregisters netdevice
1853 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001854static int bcm_enet_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001855{
1856 struct bcm_enet_priv *priv;
1857 struct net_device *dev;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001858
1859 /* stop netdevice */
1860 dev = platform_get_drvdata(pdev);
1861 priv = netdev_priv(dev);
1862 unregister_netdev(dev);
1863
1864 /* turn off mdc clock */
1865 enet_writel(priv, 0, ENET_MIISC_REG);
1866
1867 if (priv->has_phy) {
1868 mdiobus_unregister(priv->mii_bus);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001869 mdiobus_free(priv->mii_bus);
1870 } else {
1871 struct bcm63xx_enet_platform_data *pd;
1872
1873 pd = pdev->dev.platform_data;
1874 if (pd && pd->mii_config)
1875 pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
1876 bcm_enet_mdio_write_mii);
1877 }
1878
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001879 /* disable hw block clocks */
1880 if (priv->phy_clk) {
Jonas Gorski624e2d22013-03-10 03:57:49 +00001881 clk_disable_unprepare(priv->phy_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001882 clk_put(priv->phy_clk);
1883 }
Jonas Gorski624e2d22013-03-10 03:57:49 +00001884 clk_disable_unprepare(priv->mac_clk);
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001885 clk_put(priv->mac_clk);
1886
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001887 free_netdev(dev);
1888 return 0;
1889}
1890
1891struct platform_driver bcm63xx_enet_driver = {
1892 .probe = bcm_enet_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05001893 .remove = bcm_enet_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001894 .driver = {
1895 .name = "bcm63xx_enet",
1896 .owner = THIS_MODULE,
1897 },
1898};
1899
1900/*
1901 * reserve & remap memory space shared between all macs
1902 */
Bill Pemberton047fc562012-12-03 09:24:23 -05001903static int bcm_enet_shared_probe(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001904{
1905 struct resource *res;
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001906 void __iomem *p[3];
1907 unsigned int i;
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001908
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001909 memset(bcm_enet_shared_base, 0, sizeof(bcm_enet_shared_base));
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001910
Maxime Bizon0ae99b52013-06-04 22:53:34 +01001911 for (i = 0; i < 3; i++) {
1912 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1913 p[i] = devm_ioremap_resource(&pdev->dev, res);
1914 if (!p[i])
1915 return -ENOMEM;
1916 }
1917
1918 memcpy(bcm_enet_shared_base, p, sizeof(bcm_enet_shared_base));
Jonas Gorski1c03da02013-03-10 03:57:47 +00001919
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001920 return 0;
1921}
1922
Bill Pemberton047fc562012-12-03 09:24:23 -05001923static int bcm_enet_shared_remove(struct platform_device *pdev)
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001924{
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001925 return 0;
1926}
1927
1928/*
1929 * this "shared" driver is needed because both macs share a single
1930 * address space
1931 */
1932struct platform_driver bcm63xx_enet_shared_driver = {
1933 .probe = bcm_enet_shared_probe,
Bill Pemberton047fc562012-12-03 09:24:23 -05001934 .remove = bcm_enet_shared_remove,
Maxime Bizon9b1fc552009-08-18 13:23:40 +01001935 .driver = {
1936 .name = "bcm63xx_enet_shared",
1937 .owner = THIS_MODULE,
1938 },
1939};
1940
1941/*
1942 * entry point
1943 */
1944static int __init bcm_enet_init(void)
1945{
1946 int ret;
1947
1948 ret = platform_driver_register(&bcm63xx_enet_shared_driver);
1949 if (ret)
1950 return ret;
1951
1952 ret = platform_driver_register(&bcm63xx_enet_driver);
1953 if (ret)
1954 platform_driver_unregister(&bcm63xx_enet_shared_driver);
1955
1956 return ret;
1957}
1958
1959static void __exit bcm_enet_exit(void)
1960{
1961 platform_driver_unregister(&bcm63xx_enet_driver);
1962 platform_driver_unregister(&bcm63xx_enet_shared_driver);
1963}
1964
1965
1966module_init(bcm_enet_init);
1967module_exit(bcm_enet_exit);
1968
1969MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
1970MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
1971MODULE_LICENSE("GPL");