ALSA: snd-usb-us122l: Delete calls to preempt_disable
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / bcm63xx_enet.c
blobd110c1b978a5e0cd3e66b03df5cf85137676cba8
1 /*
2 * Driver for BCM963xx builtin Ethernet mac
4 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
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.
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.
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.
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/clk.h>
23 #include <linux/etherdevice.h>
24 #include <linux/delay.h>
25 #include <linux/ethtool.h>
26 #include <linux/crc32.h>
27 #include <linux/err.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/platform_device.h>
30 #include <linux/if_vlan.h>
32 #include <bcm63xx_dev_enet.h>
33 #include "bcm63xx_enet.h"
35 static char bcm_enet_driver_name[] = "bcm63xx_enet";
36 static char bcm_enet_driver_version[] = "1.0";
38 static int copybreak __read_mostly = 128;
39 module_param(copybreak, int, 0);
40 MODULE_PARM_DESC(copybreak, "Receive copy threshold");
42 /* io memory shared between all devices */
43 static void __iomem *bcm_enet_shared_base;
46 * io helpers to access mac registers
48 static inline u32 enet_readl(struct bcm_enet_priv *priv, u32 off)
50 return bcm_readl(priv->base + off);
53 static inline void enet_writel(struct bcm_enet_priv *priv,
54 u32 val, u32 off)
56 bcm_writel(val, priv->base + off);
60 * io helpers to access shared registers
62 static inline u32 enet_dma_readl(struct bcm_enet_priv *priv, u32 off)
64 return bcm_readl(bcm_enet_shared_base + off);
67 static inline void enet_dma_writel(struct bcm_enet_priv *priv,
68 u32 val, u32 off)
70 bcm_writel(val, bcm_enet_shared_base + off);
74 * write given data into mii register and wait for transfer to end
75 * with timeout (average measured transfer time is 25us)
77 static int do_mdio_op(struct bcm_enet_priv *priv, unsigned int data)
79 int limit;
81 /* make sure mii interrupt status is cleared */
82 enet_writel(priv, ENET_IR_MII, ENET_IR_REG);
84 enet_writel(priv, data, ENET_MIIDATA_REG);
85 wmb();
87 /* busy wait on mii interrupt bit, with timeout */
88 limit = 1000;
89 do {
90 if (enet_readl(priv, ENET_IR_REG) & ENET_IR_MII)
91 break;
92 udelay(1);
93 } while (limit-- > 0);
95 return (limit < 0) ? 1 : 0;
99 * MII internal read callback
101 static int bcm_enet_mdio_read(struct bcm_enet_priv *priv, int mii_id,
102 int regnum)
104 u32 tmp, val;
106 tmp = regnum << ENET_MIIDATA_REG_SHIFT;
107 tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
108 tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
109 tmp |= ENET_MIIDATA_OP_READ_MASK;
111 if (do_mdio_op(priv, tmp))
112 return -1;
114 val = enet_readl(priv, ENET_MIIDATA_REG);
115 val &= 0xffff;
116 return val;
120 * MII internal write callback
122 static int bcm_enet_mdio_write(struct bcm_enet_priv *priv, int mii_id,
123 int regnum, u16 value)
125 u32 tmp;
127 tmp = (value & 0xffff) << ENET_MIIDATA_DATA_SHIFT;
128 tmp |= 0x2 << ENET_MIIDATA_TA_SHIFT;
129 tmp |= regnum << ENET_MIIDATA_REG_SHIFT;
130 tmp |= mii_id << ENET_MIIDATA_PHYID_SHIFT;
131 tmp |= ENET_MIIDATA_OP_WRITE_MASK;
133 (void)do_mdio_op(priv, tmp);
134 return 0;
138 * MII read callback from phylib
140 static int bcm_enet_mdio_read_phylib(struct mii_bus *bus, int mii_id,
141 int regnum)
143 return bcm_enet_mdio_read(bus->priv, mii_id, regnum);
147 * MII write callback from phylib
149 static int bcm_enet_mdio_write_phylib(struct mii_bus *bus, int mii_id,
150 int regnum, u16 value)
152 return bcm_enet_mdio_write(bus->priv, mii_id, regnum, value);
156 * MII read callback from mii core
158 static int bcm_enet_mdio_read_mii(struct net_device *dev, int mii_id,
159 int regnum)
161 return bcm_enet_mdio_read(netdev_priv(dev), mii_id, regnum);
165 * MII write callback from mii core
167 static void bcm_enet_mdio_write_mii(struct net_device *dev, int mii_id,
168 int regnum, int value)
170 bcm_enet_mdio_write(netdev_priv(dev), mii_id, regnum, value);
174 * refill rx queue
176 static int bcm_enet_refill_rx(struct net_device *dev)
178 struct bcm_enet_priv *priv;
180 priv = netdev_priv(dev);
182 while (priv->rx_desc_count < priv->rx_ring_size) {
183 struct bcm_enet_desc *desc;
184 struct sk_buff *skb;
185 dma_addr_t p;
186 int desc_idx;
187 u32 len_stat;
189 desc_idx = priv->rx_dirty_desc;
190 desc = &priv->rx_desc_cpu[desc_idx];
192 if (!priv->rx_skb[desc_idx]) {
193 skb = netdev_alloc_skb(dev, priv->rx_skb_size);
194 if (!skb)
195 break;
196 priv->rx_skb[desc_idx] = skb;
198 p = dma_map_single(&priv->pdev->dev, skb->data,
199 priv->rx_skb_size,
200 DMA_FROM_DEVICE);
201 desc->address = p;
204 len_stat = priv->rx_skb_size << DMADESC_LENGTH_SHIFT;
205 len_stat |= DMADESC_OWNER_MASK;
206 if (priv->rx_dirty_desc == priv->rx_ring_size - 1) {
207 len_stat |= DMADESC_WRAP_MASK;
208 priv->rx_dirty_desc = 0;
209 } else {
210 priv->rx_dirty_desc++;
212 wmb();
213 desc->len_stat = len_stat;
215 priv->rx_desc_count++;
217 /* tell dma engine we allocated one buffer */
218 enet_dma_writel(priv, 1, ENETDMA_BUFALLOC_REG(priv->rx_chan));
221 /* If rx ring is still empty, set a timer to try allocating
222 * again at a later time. */
223 if (priv->rx_desc_count == 0 && netif_running(dev)) {
224 dev_warn(&priv->pdev->dev, "unable to refill rx ring\n");
225 priv->rx_timeout.expires = jiffies + HZ;
226 add_timer(&priv->rx_timeout);
229 return 0;
233 * timer callback to defer refill rx queue in case we're OOM
235 static void bcm_enet_refill_rx_timer(unsigned long data)
237 struct net_device *dev;
238 struct bcm_enet_priv *priv;
240 dev = (struct net_device *)data;
241 priv = netdev_priv(dev);
243 spin_lock(&priv->rx_lock);
244 bcm_enet_refill_rx((struct net_device *)data);
245 spin_unlock(&priv->rx_lock);
249 * extract packet from rx queue
251 static int bcm_enet_receive_queue(struct net_device *dev, int budget)
253 struct bcm_enet_priv *priv;
254 struct device *kdev;
255 int processed;
257 priv = netdev_priv(dev);
258 kdev = &priv->pdev->dev;
259 processed = 0;
261 /* don't scan ring further than number of refilled
262 * descriptor */
263 if (budget > priv->rx_desc_count)
264 budget = priv->rx_desc_count;
266 do {
267 struct bcm_enet_desc *desc;
268 struct sk_buff *skb;
269 int desc_idx;
270 u32 len_stat;
271 unsigned int len;
273 desc_idx = priv->rx_curr_desc;
274 desc = &priv->rx_desc_cpu[desc_idx];
276 /* make sure we actually read the descriptor status at
277 * each loop */
278 rmb();
280 len_stat = desc->len_stat;
282 /* break if dma ownership belongs to hw */
283 if (len_stat & DMADESC_OWNER_MASK)
284 break;
286 processed++;
287 priv->rx_curr_desc++;
288 if (priv->rx_curr_desc == priv->rx_ring_size)
289 priv->rx_curr_desc = 0;
290 priv->rx_desc_count--;
292 /* if the packet does not have start of packet _and_
293 * end of packet flag set, then just recycle it */
294 if ((len_stat & DMADESC_ESOP_MASK) != DMADESC_ESOP_MASK) {
295 priv->stats.rx_dropped++;
296 continue;
299 /* recycle packet if it's marked as bad */
300 if (unlikely(len_stat & DMADESC_ERR_MASK)) {
301 priv->stats.rx_errors++;
303 if (len_stat & DMADESC_OVSIZE_MASK)
304 priv->stats.rx_length_errors++;
305 if (len_stat & DMADESC_CRC_MASK)
306 priv->stats.rx_crc_errors++;
307 if (len_stat & DMADESC_UNDER_MASK)
308 priv->stats.rx_frame_errors++;
309 if (len_stat & DMADESC_OV_MASK)
310 priv->stats.rx_fifo_errors++;
311 continue;
314 /* valid packet */
315 skb = priv->rx_skb[desc_idx];
316 len = (len_stat & DMADESC_LENGTH_MASK) >> DMADESC_LENGTH_SHIFT;
317 /* don't include FCS */
318 len -= 4;
320 if (len < copybreak) {
321 struct sk_buff *nskb;
323 nskb = netdev_alloc_skb(dev, len + NET_IP_ALIGN);
324 if (!nskb) {
325 /* forget packet, just rearm desc */
326 priv->stats.rx_dropped++;
327 continue;
330 /* since we're copying the data, we can align
331 * them properly */
332 skb_reserve(nskb, NET_IP_ALIGN);
333 dma_sync_single_for_cpu(kdev, desc->address,
334 len, DMA_FROM_DEVICE);
335 memcpy(nskb->data, skb->data, len);
336 dma_sync_single_for_device(kdev, desc->address,
337 len, DMA_FROM_DEVICE);
338 skb = nskb;
339 } else {
340 dma_unmap_single(&priv->pdev->dev, desc->address,
341 priv->rx_skb_size, DMA_FROM_DEVICE);
342 priv->rx_skb[desc_idx] = NULL;
345 skb_put(skb, len);
346 skb->dev = dev;
347 skb->protocol = eth_type_trans(skb, dev);
348 priv->stats.rx_packets++;
349 priv->stats.rx_bytes += len;
350 dev->last_rx = jiffies;
351 netif_receive_skb(skb);
353 } while (--budget > 0);
355 if (processed || !priv->rx_desc_count) {
356 bcm_enet_refill_rx(dev);
358 /* kick rx dma */
359 enet_dma_writel(priv, ENETDMA_CHANCFG_EN_MASK,
360 ENETDMA_CHANCFG_REG(priv->rx_chan));
363 return processed;
368 * try to or force reclaim of transmitted buffers
370 static int bcm_enet_tx_reclaim(struct net_device *dev, int force)
372 struct bcm_enet_priv *priv;
373 int released;
375 priv = netdev_priv(dev);
376 released = 0;
378 while (priv->tx_desc_count < priv->tx_ring_size) {
379 struct bcm_enet_desc *desc;
380 struct sk_buff *skb;
382 /* We run in a bh and fight against start_xmit, which
383 * is called with bh disabled */
384 spin_lock(&priv->tx_lock);
386 desc = &priv->tx_desc_cpu[priv->tx_dirty_desc];
388 if (!force && (desc->len_stat & DMADESC_OWNER_MASK)) {
389 spin_unlock(&priv->tx_lock);
390 break;
393 /* ensure other field of the descriptor were not read
394 * before we checked ownership */
395 rmb();
397 skb = priv->tx_skb[priv->tx_dirty_desc];
398 priv->tx_skb[priv->tx_dirty_desc] = NULL;
399 dma_unmap_single(&priv->pdev->dev, desc->address, skb->len,
400 DMA_TO_DEVICE);
402 priv->tx_dirty_desc++;
403 if (priv->tx_dirty_desc == priv->tx_ring_size)
404 priv->tx_dirty_desc = 0;
405 priv->tx_desc_count++;
407 spin_unlock(&priv->tx_lock);
409 if (desc->len_stat & DMADESC_UNDER_MASK)
410 priv->stats.tx_errors++;
412 dev_kfree_skb(skb);
413 released++;
416 if (netif_queue_stopped(dev) && released)
417 netif_wake_queue(dev);
419 return released;
423 * poll func, called by network core
425 static int bcm_enet_poll(struct napi_struct *napi, int budget)
427 struct bcm_enet_priv *priv;
428 struct net_device *dev;
429 int tx_work_done, rx_work_done;
431 priv = container_of(napi, struct bcm_enet_priv, napi);
432 dev = priv->net_dev;
434 /* ack interrupts */
435 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
436 ENETDMA_IR_REG(priv->rx_chan));
437 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
438 ENETDMA_IR_REG(priv->tx_chan));
440 /* reclaim sent skb */
441 tx_work_done = bcm_enet_tx_reclaim(dev, 0);
443 spin_lock(&priv->rx_lock);
444 rx_work_done = bcm_enet_receive_queue(dev, budget);
445 spin_unlock(&priv->rx_lock);
447 if (rx_work_done >= budget || tx_work_done > 0) {
448 /* rx/tx queue is not yet empty/clean */
449 return rx_work_done;
452 /* no more packet in rx/tx queue, remove device from poll
453 * queue */
454 napi_complete(napi);
456 /* restore rx/tx interrupt */
457 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
458 ENETDMA_IRMASK_REG(priv->rx_chan));
459 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
460 ENETDMA_IRMASK_REG(priv->tx_chan));
462 return rx_work_done;
466 * mac interrupt handler
468 static irqreturn_t bcm_enet_isr_mac(int irq, void *dev_id)
470 struct net_device *dev;
471 struct bcm_enet_priv *priv;
472 u32 stat;
474 dev = dev_id;
475 priv = netdev_priv(dev);
477 stat = enet_readl(priv, ENET_IR_REG);
478 if (!(stat & ENET_IR_MIB))
479 return IRQ_NONE;
481 /* clear & mask interrupt */
482 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
483 enet_writel(priv, 0, ENET_IRMASK_REG);
485 /* read mib registers in workqueue */
486 schedule_work(&priv->mib_update_task);
488 return IRQ_HANDLED;
492 * rx/tx dma interrupt handler
494 static irqreturn_t bcm_enet_isr_dma(int irq, void *dev_id)
496 struct net_device *dev;
497 struct bcm_enet_priv *priv;
499 dev = dev_id;
500 priv = netdev_priv(dev);
502 /* mask rx/tx interrupts */
503 enet_dma_writel(priv, 0, ENETDMA_IRMASK_REG(priv->rx_chan));
504 enet_dma_writel(priv, 0, ENETDMA_IRMASK_REG(priv->tx_chan));
506 napi_schedule(&priv->napi);
508 return IRQ_HANDLED;
512 * tx request callback
514 static int bcm_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
516 struct bcm_enet_priv *priv;
517 struct bcm_enet_desc *desc;
518 u32 len_stat;
519 int ret;
521 priv = netdev_priv(dev);
523 /* lock against tx reclaim */
524 spin_lock(&priv->tx_lock);
526 /* make sure the tx hw queue is not full, should not happen
527 * since we stop queue before it's the case */
528 if (unlikely(!priv->tx_desc_count)) {
529 netif_stop_queue(dev);
530 dev_err(&priv->pdev->dev, "xmit called with no tx desc "
531 "available?\n");
532 ret = NETDEV_TX_BUSY;
533 goto out_unlock;
536 /* point to the next available desc */
537 desc = &priv->tx_desc_cpu[priv->tx_curr_desc];
538 priv->tx_skb[priv->tx_curr_desc] = skb;
540 /* fill descriptor */
541 desc->address = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
542 DMA_TO_DEVICE);
544 len_stat = (skb->len << DMADESC_LENGTH_SHIFT) & DMADESC_LENGTH_MASK;
545 len_stat |= DMADESC_ESOP_MASK |
546 DMADESC_APPEND_CRC |
547 DMADESC_OWNER_MASK;
549 priv->tx_curr_desc++;
550 if (priv->tx_curr_desc == priv->tx_ring_size) {
551 priv->tx_curr_desc = 0;
552 len_stat |= DMADESC_WRAP_MASK;
554 priv->tx_desc_count--;
556 /* dma might be already polling, make sure we update desc
557 * fields in correct order */
558 wmb();
559 desc->len_stat = len_stat;
560 wmb();
562 /* kick tx dma */
563 enet_dma_writel(priv, ENETDMA_CHANCFG_EN_MASK,
564 ENETDMA_CHANCFG_REG(priv->tx_chan));
566 /* stop queue if no more desc available */
567 if (!priv->tx_desc_count)
568 netif_stop_queue(dev);
570 priv->stats.tx_bytes += skb->len;
571 priv->stats.tx_packets++;
572 dev->trans_start = jiffies;
573 ret = NETDEV_TX_OK;
575 out_unlock:
576 spin_unlock(&priv->tx_lock);
577 return ret;
581 * Change the interface's mac address.
583 static int bcm_enet_set_mac_address(struct net_device *dev, void *p)
585 struct bcm_enet_priv *priv;
586 struct sockaddr *addr = p;
587 u32 val;
589 priv = netdev_priv(dev);
590 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
592 /* use perfect match register 0 to store my mac address */
593 val = (dev->dev_addr[2] << 24) | (dev->dev_addr[3] << 16) |
594 (dev->dev_addr[4] << 8) | dev->dev_addr[5];
595 enet_writel(priv, val, ENET_PML_REG(0));
597 val = (dev->dev_addr[0] << 8 | dev->dev_addr[1]);
598 val |= ENET_PMH_DATAVALID_MASK;
599 enet_writel(priv, val, ENET_PMH_REG(0));
601 return 0;
605 * Change rx mode (promiscous/allmulti) and update multicast list
607 static void bcm_enet_set_multicast_list(struct net_device *dev)
609 struct bcm_enet_priv *priv;
610 struct dev_mc_list *mc_list;
611 u32 val;
612 int i;
614 priv = netdev_priv(dev);
616 val = enet_readl(priv, ENET_RXCFG_REG);
618 if (dev->flags & IFF_PROMISC)
619 val |= ENET_RXCFG_PROMISC_MASK;
620 else
621 val &= ~ENET_RXCFG_PROMISC_MASK;
623 /* only 3 perfect match registers left, first one is used for
624 * own mac address */
625 if ((dev->flags & IFF_ALLMULTI) || dev->mc_count > 3)
626 val |= ENET_RXCFG_ALLMCAST_MASK;
627 else
628 val &= ~ENET_RXCFG_ALLMCAST_MASK;
630 /* no need to set perfect match registers if we catch all
631 * multicast */
632 if (val & ENET_RXCFG_ALLMCAST_MASK) {
633 enet_writel(priv, val, ENET_RXCFG_REG);
634 return;
637 for (i = 0, mc_list = dev->mc_list;
638 (mc_list != NULL) && (i < dev->mc_count) && (i < 3);
639 i++, mc_list = mc_list->next) {
640 u8 *dmi_addr;
641 u32 tmp;
643 /* filter non ethernet address */
644 if (mc_list->dmi_addrlen != 6)
645 continue;
647 /* update perfect match registers */
648 dmi_addr = mc_list->dmi_addr;
649 tmp = (dmi_addr[2] << 24) | (dmi_addr[3] << 16) |
650 (dmi_addr[4] << 8) | dmi_addr[5];
651 enet_writel(priv, tmp, ENET_PML_REG(i + 1));
653 tmp = (dmi_addr[0] << 8 | dmi_addr[1]);
654 tmp |= ENET_PMH_DATAVALID_MASK;
655 enet_writel(priv, tmp, ENET_PMH_REG(i + 1));
658 for (; i < 3; i++) {
659 enet_writel(priv, 0, ENET_PML_REG(i + 1));
660 enet_writel(priv, 0, ENET_PMH_REG(i + 1));
663 enet_writel(priv, val, ENET_RXCFG_REG);
667 * set mac duplex parameters
669 static void bcm_enet_set_duplex(struct bcm_enet_priv *priv, int fullduplex)
671 u32 val;
673 val = enet_readl(priv, ENET_TXCTL_REG);
674 if (fullduplex)
675 val |= ENET_TXCTL_FD_MASK;
676 else
677 val &= ~ENET_TXCTL_FD_MASK;
678 enet_writel(priv, val, ENET_TXCTL_REG);
682 * set mac flow control parameters
684 static void bcm_enet_set_flow(struct bcm_enet_priv *priv, int rx_en, int tx_en)
686 u32 val;
688 /* rx flow control (pause frame handling) */
689 val = enet_readl(priv, ENET_RXCFG_REG);
690 if (rx_en)
691 val |= ENET_RXCFG_ENFLOW_MASK;
692 else
693 val &= ~ENET_RXCFG_ENFLOW_MASK;
694 enet_writel(priv, val, ENET_RXCFG_REG);
696 /* tx flow control (pause frame generation) */
697 val = enet_dma_readl(priv, ENETDMA_CFG_REG);
698 if (tx_en)
699 val |= ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
700 else
701 val &= ~ENETDMA_CFG_FLOWCH_MASK(priv->rx_chan);
702 enet_dma_writel(priv, val, ENETDMA_CFG_REG);
706 * link changed callback (from phylib)
708 static void bcm_enet_adjust_phy_link(struct net_device *dev)
710 struct bcm_enet_priv *priv;
711 struct phy_device *phydev;
712 int status_changed;
714 priv = netdev_priv(dev);
715 phydev = priv->phydev;
716 status_changed = 0;
718 if (priv->old_link != phydev->link) {
719 status_changed = 1;
720 priv->old_link = phydev->link;
723 /* reflect duplex change in mac configuration */
724 if (phydev->link && phydev->duplex != priv->old_duplex) {
725 bcm_enet_set_duplex(priv,
726 (phydev->duplex == DUPLEX_FULL) ? 1 : 0);
727 status_changed = 1;
728 priv->old_duplex = phydev->duplex;
731 /* enable flow control if remote advertise it (trust phylib to
732 * check that duplex is full */
733 if (phydev->link && phydev->pause != priv->old_pause) {
734 int rx_pause_en, tx_pause_en;
736 if (phydev->pause) {
737 /* pause was advertised by lpa and us */
738 rx_pause_en = 1;
739 tx_pause_en = 1;
740 } else if (!priv->pause_auto) {
741 /* pause setting overrided by user */
742 rx_pause_en = priv->pause_rx;
743 tx_pause_en = priv->pause_tx;
744 } else {
745 rx_pause_en = 0;
746 tx_pause_en = 0;
749 bcm_enet_set_flow(priv, rx_pause_en, tx_pause_en);
750 status_changed = 1;
751 priv->old_pause = phydev->pause;
754 if (status_changed) {
755 pr_info("%s: link %s", dev->name, phydev->link ?
756 "UP" : "DOWN");
757 if (phydev->link)
758 pr_cont(" - %d/%s - flow control %s", phydev->speed,
759 DUPLEX_FULL == phydev->duplex ? "full" : "half",
760 phydev->pause == 1 ? "rx&tx" : "off");
762 pr_cont("\n");
767 * link changed callback (if phylib is not used)
769 static void bcm_enet_adjust_link(struct net_device *dev)
771 struct bcm_enet_priv *priv;
773 priv = netdev_priv(dev);
774 bcm_enet_set_duplex(priv, priv->force_duplex_full);
775 bcm_enet_set_flow(priv, priv->pause_rx, priv->pause_tx);
776 netif_carrier_on(dev);
778 pr_info("%s: link forced UP - %d/%s - flow control %s/%s\n",
779 dev->name,
780 priv->force_speed_100 ? 100 : 10,
781 priv->force_duplex_full ? "full" : "half",
782 priv->pause_rx ? "rx" : "off",
783 priv->pause_tx ? "tx" : "off");
787 * open callback, allocate dma rings & buffers and start rx operation
789 static int bcm_enet_open(struct net_device *dev)
791 struct bcm_enet_priv *priv;
792 struct sockaddr addr;
793 struct device *kdev;
794 struct phy_device *phydev;
795 int i, ret;
796 unsigned int size;
797 char phy_id[MII_BUS_ID_SIZE + 3];
798 void *p;
799 u32 val;
801 priv = netdev_priv(dev);
802 kdev = &priv->pdev->dev;
804 if (priv->has_phy) {
805 /* connect to PHY */
806 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
807 priv->mac_id ? "1" : "0", priv->phy_id);
809 phydev = phy_connect(dev, phy_id, &bcm_enet_adjust_phy_link, 0,
810 PHY_INTERFACE_MODE_MII);
812 if (IS_ERR(phydev)) {
813 dev_err(kdev, "could not attach to PHY\n");
814 return PTR_ERR(phydev);
817 /* mask with MAC supported features */
818 phydev->supported &= (SUPPORTED_10baseT_Half |
819 SUPPORTED_10baseT_Full |
820 SUPPORTED_100baseT_Half |
821 SUPPORTED_100baseT_Full |
822 SUPPORTED_Autoneg |
823 SUPPORTED_Pause |
824 SUPPORTED_MII);
825 phydev->advertising = phydev->supported;
827 if (priv->pause_auto && priv->pause_rx && priv->pause_tx)
828 phydev->advertising |= SUPPORTED_Pause;
829 else
830 phydev->advertising &= ~SUPPORTED_Pause;
832 dev_info(kdev, "attached PHY at address %d [%s]\n",
833 phydev->addr, phydev->drv->name);
835 priv->old_link = 0;
836 priv->old_duplex = -1;
837 priv->old_pause = -1;
838 priv->phydev = phydev;
841 /* mask all interrupts and request them */
842 enet_writel(priv, 0, ENET_IRMASK_REG);
843 enet_dma_writel(priv, 0, ENETDMA_IRMASK_REG(priv->rx_chan));
844 enet_dma_writel(priv, 0, ENETDMA_IRMASK_REG(priv->tx_chan));
846 ret = request_irq(dev->irq, bcm_enet_isr_mac, 0, dev->name, dev);
847 if (ret)
848 goto out_phy_disconnect;
850 ret = request_irq(priv->irq_rx, bcm_enet_isr_dma,
851 IRQF_SAMPLE_RANDOM | IRQF_DISABLED, dev->name, dev);
852 if (ret)
853 goto out_freeirq;
855 ret = request_irq(priv->irq_tx, bcm_enet_isr_dma,
856 IRQF_DISABLED, dev->name, dev);
857 if (ret)
858 goto out_freeirq_rx;
860 /* initialize perfect match registers */
861 for (i = 0; i < 4; i++) {
862 enet_writel(priv, 0, ENET_PML_REG(i));
863 enet_writel(priv, 0, ENET_PMH_REG(i));
866 /* write device mac address */
867 memcpy(addr.sa_data, dev->dev_addr, ETH_ALEN);
868 bcm_enet_set_mac_address(dev, &addr);
870 /* allocate rx dma ring */
871 size = priv->rx_ring_size * sizeof(struct bcm_enet_desc);
872 p = dma_alloc_coherent(kdev, size, &priv->rx_desc_dma, GFP_KERNEL);
873 if (!p) {
874 dev_err(kdev, "cannot allocate rx ring %u\n", size);
875 ret = -ENOMEM;
876 goto out_freeirq_tx;
879 memset(p, 0, size);
880 priv->rx_desc_alloc_size = size;
881 priv->rx_desc_cpu = p;
883 /* allocate tx dma ring */
884 size = priv->tx_ring_size * sizeof(struct bcm_enet_desc);
885 p = dma_alloc_coherent(kdev, size, &priv->tx_desc_dma, GFP_KERNEL);
886 if (!p) {
887 dev_err(kdev, "cannot allocate tx ring\n");
888 ret = -ENOMEM;
889 goto out_free_rx_ring;
892 memset(p, 0, size);
893 priv->tx_desc_alloc_size = size;
894 priv->tx_desc_cpu = p;
896 priv->tx_skb = kzalloc(sizeof(struct sk_buff *) * priv->tx_ring_size,
897 GFP_KERNEL);
898 if (!priv->tx_skb) {
899 dev_err(kdev, "cannot allocate rx skb queue\n");
900 ret = -ENOMEM;
901 goto out_free_tx_ring;
904 priv->tx_desc_count = priv->tx_ring_size;
905 priv->tx_dirty_desc = 0;
906 priv->tx_curr_desc = 0;
907 spin_lock_init(&priv->tx_lock);
909 /* init & fill rx ring with skbs */
910 priv->rx_skb = kzalloc(sizeof(struct sk_buff *) * priv->rx_ring_size,
911 GFP_KERNEL);
912 if (!priv->rx_skb) {
913 dev_err(kdev, "cannot allocate rx skb queue\n");
914 ret = -ENOMEM;
915 goto out_free_tx_skb;
918 priv->rx_desc_count = 0;
919 priv->rx_dirty_desc = 0;
920 priv->rx_curr_desc = 0;
922 /* initialize flow control buffer allocation */
923 enet_dma_writel(priv, ENETDMA_BUFALLOC_FORCE_MASK | 0,
924 ENETDMA_BUFALLOC_REG(priv->rx_chan));
926 if (bcm_enet_refill_rx(dev)) {
927 dev_err(kdev, "cannot allocate rx skb queue\n");
928 ret = -ENOMEM;
929 goto out;
932 /* write rx & tx ring addresses */
933 enet_dma_writel(priv, priv->rx_desc_dma,
934 ENETDMA_RSTART_REG(priv->rx_chan));
935 enet_dma_writel(priv, priv->tx_desc_dma,
936 ENETDMA_RSTART_REG(priv->tx_chan));
938 /* clear remaining state ram for rx & tx channel */
939 enet_dma_writel(priv, 0, ENETDMA_SRAM2_REG(priv->rx_chan));
940 enet_dma_writel(priv, 0, ENETDMA_SRAM2_REG(priv->tx_chan));
941 enet_dma_writel(priv, 0, ENETDMA_SRAM3_REG(priv->rx_chan));
942 enet_dma_writel(priv, 0, ENETDMA_SRAM3_REG(priv->tx_chan));
943 enet_dma_writel(priv, 0, ENETDMA_SRAM4_REG(priv->rx_chan));
944 enet_dma_writel(priv, 0, ENETDMA_SRAM4_REG(priv->tx_chan));
946 /* set max rx/tx length */
947 enet_writel(priv, priv->hw_mtu, ENET_RXMAXLEN_REG);
948 enet_writel(priv, priv->hw_mtu, ENET_TXMAXLEN_REG);
950 /* set dma maximum burst len */
951 enet_dma_writel(priv, BCMENET_DMA_MAXBURST,
952 ENETDMA_MAXBURST_REG(priv->rx_chan));
953 enet_dma_writel(priv, BCMENET_DMA_MAXBURST,
954 ENETDMA_MAXBURST_REG(priv->tx_chan));
956 /* set correct transmit fifo watermark */
957 enet_writel(priv, BCMENET_TX_FIFO_TRESH, ENET_TXWMARK_REG);
959 /* set flow control low/high threshold to 1/3 / 2/3 */
960 val = priv->rx_ring_size / 3;
961 enet_dma_writel(priv, val, ENETDMA_FLOWCL_REG(priv->rx_chan));
962 val = (priv->rx_ring_size * 2) / 3;
963 enet_dma_writel(priv, val, ENETDMA_FLOWCH_REG(priv->rx_chan));
965 /* all set, enable mac and interrupts, start dma engine and
966 * kick rx dma channel */
967 wmb();
968 enet_writel(priv, ENET_CTL_ENABLE_MASK, ENET_CTL_REG);
969 enet_dma_writel(priv, ENETDMA_CFG_EN_MASK, ENETDMA_CFG_REG);
970 enet_dma_writel(priv, ENETDMA_CHANCFG_EN_MASK,
971 ENETDMA_CHANCFG_REG(priv->rx_chan));
973 /* watch "mib counters about to overflow" interrupt */
974 enet_writel(priv, ENET_IR_MIB, ENET_IR_REG);
975 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
977 /* watch "packet transferred" interrupt in rx and tx */
978 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
979 ENETDMA_IR_REG(priv->rx_chan));
980 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
981 ENETDMA_IR_REG(priv->tx_chan));
983 /* make sure we enable napi before rx interrupt */
984 napi_enable(&priv->napi);
986 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
987 ENETDMA_IRMASK_REG(priv->rx_chan));
988 enet_dma_writel(priv, ENETDMA_IR_PKTDONE_MASK,
989 ENETDMA_IRMASK_REG(priv->tx_chan));
991 if (priv->has_phy)
992 phy_start(priv->phydev);
993 else
994 bcm_enet_adjust_link(dev);
996 netif_start_queue(dev);
997 return 0;
999 out:
1000 for (i = 0; i < priv->rx_ring_size; i++) {
1001 struct bcm_enet_desc *desc;
1003 if (!priv->rx_skb[i])
1004 continue;
1006 desc = &priv->rx_desc_cpu[i];
1007 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1008 DMA_FROM_DEVICE);
1009 kfree_skb(priv->rx_skb[i]);
1011 kfree(priv->rx_skb);
1013 out_free_tx_skb:
1014 kfree(priv->tx_skb);
1016 out_free_tx_ring:
1017 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1018 priv->tx_desc_cpu, priv->tx_desc_dma);
1020 out_free_rx_ring:
1021 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1022 priv->rx_desc_cpu, priv->rx_desc_dma);
1024 out_freeirq_tx:
1025 free_irq(priv->irq_tx, dev);
1027 out_freeirq_rx:
1028 free_irq(priv->irq_rx, dev);
1030 out_freeirq:
1031 free_irq(dev->irq, dev);
1033 out_phy_disconnect:
1034 phy_disconnect(priv->phydev);
1036 return ret;
1040 * disable mac
1042 static void bcm_enet_disable_mac(struct bcm_enet_priv *priv)
1044 int limit;
1045 u32 val;
1047 val = enet_readl(priv, ENET_CTL_REG);
1048 val |= ENET_CTL_DISABLE_MASK;
1049 enet_writel(priv, val, ENET_CTL_REG);
1051 limit = 1000;
1052 do {
1053 u32 val;
1055 val = enet_readl(priv, ENET_CTL_REG);
1056 if (!(val & ENET_CTL_DISABLE_MASK))
1057 break;
1058 udelay(1);
1059 } while (limit--);
1063 * disable dma in given channel
1065 static void bcm_enet_disable_dma(struct bcm_enet_priv *priv, int chan)
1067 int limit;
1069 enet_dma_writel(priv, 0, ENETDMA_CHANCFG_REG(chan));
1071 limit = 1000;
1072 do {
1073 u32 val;
1075 val = enet_dma_readl(priv, ENETDMA_CHANCFG_REG(chan));
1076 if (!(val & ENETDMA_CHANCFG_EN_MASK))
1077 break;
1078 udelay(1);
1079 } while (limit--);
1083 * stop callback
1085 static int bcm_enet_stop(struct net_device *dev)
1087 struct bcm_enet_priv *priv;
1088 struct device *kdev;
1089 int i;
1091 priv = netdev_priv(dev);
1092 kdev = &priv->pdev->dev;
1094 netif_stop_queue(dev);
1095 napi_disable(&priv->napi);
1096 if (priv->has_phy)
1097 phy_stop(priv->phydev);
1098 del_timer_sync(&priv->rx_timeout);
1100 /* mask all interrupts */
1101 enet_writel(priv, 0, ENET_IRMASK_REG);
1102 enet_dma_writel(priv, 0, ENETDMA_IRMASK_REG(priv->rx_chan));
1103 enet_dma_writel(priv, 0, ENETDMA_IRMASK_REG(priv->tx_chan));
1105 /* make sure no mib update is scheduled */
1106 flush_scheduled_work();
1108 /* disable dma & mac */
1109 bcm_enet_disable_dma(priv, priv->tx_chan);
1110 bcm_enet_disable_dma(priv, priv->rx_chan);
1111 bcm_enet_disable_mac(priv);
1113 /* force reclaim of all tx buffers */
1114 bcm_enet_tx_reclaim(dev, 1);
1116 /* free the rx skb ring */
1117 for (i = 0; i < priv->rx_ring_size; i++) {
1118 struct bcm_enet_desc *desc;
1120 if (!priv->rx_skb[i])
1121 continue;
1123 desc = &priv->rx_desc_cpu[i];
1124 dma_unmap_single(kdev, desc->address, priv->rx_skb_size,
1125 DMA_FROM_DEVICE);
1126 kfree_skb(priv->rx_skb[i]);
1129 /* free remaining allocated memory */
1130 kfree(priv->rx_skb);
1131 kfree(priv->tx_skb);
1132 dma_free_coherent(kdev, priv->rx_desc_alloc_size,
1133 priv->rx_desc_cpu, priv->rx_desc_dma);
1134 dma_free_coherent(kdev, priv->tx_desc_alloc_size,
1135 priv->tx_desc_cpu, priv->tx_desc_dma);
1136 free_irq(priv->irq_tx, dev);
1137 free_irq(priv->irq_rx, dev);
1138 free_irq(dev->irq, dev);
1140 /* release phy */
1141 if (priv->has_phy) {
1142 phy_disconnect(priv->phydev);
1143 priv->phydev = NULL;
1146 return 0;
1150 * core request to return device rx/tx stats
1152 static struct net_device_stats *bcm_enet_get_stats(struct net_device *dev)
1154 struct bcm_enet_priv *priv;
1156 priv = netdev_priv(dev);
1157 return &priv->stats;
1161 * ethtool callbacks
1163 struct bcm_enet_stats {
1164 char stat_string[ETH_GSTRING_LEN];
1165 int sizeof_stat;
1166 int stat_offset;
1167 int mib_reg;
1170 #define GEN_STAT(m) sizeof(((struct bcm_enet_priv *)0)->m), \
1171 offsetof(struct bcm_enet_priv, m)
1173 static const struct bcm_enet_stats bcm_enet_gstrings_stats[] = {
1174 { "rx_packets", GEN_STAT(stats.rx_packets), -1 },
1175 { "tx_packets", GEN_STAT(stats.tx_packets), -1 },
1176 { "rx_bytes", GEN_STAT(stats.rx_bytes), -1 },
1177 { "tx_bytes", GEN_STAT(stats.tx_bytes), -1 },
1178 { "rx_errors", GEN_STAT(stats.rx_errors), -1 },
1179 { "tx_errors", GEN_STAT(stats.tx_errors), -1 },
1180 { "rx_dropped", GEN_STAT(stats.rx_dropped), -1 },
1181 { "tx_dropped", GEN_STAT(stats.tx_dropped), -1 },
1183 { "rx_good_octets", GEN_STAT(mib.rx_gd_octets), ETH_MIB_RX_GD_OCTETS},
1184 { "rx_good_pkts", GEN_STAT(mib.rx_gd_pkts), ETH_MIB_RX_GD_PKTS },
1185 { "rx_broadcast", GEN_STAT(mib.rx_brdcast), ETH_MIB_RX_BRDCAST },
1186 { "rx_multicast", GEN_STAT(mib.rx_mult), ETH_MIB_RX_MULT },
1187 { "rx_64_octets", GEN_STAT(mib.rx_64), ETH_MIB_RX_64 },
1188 { "rx_65_127_oct", GEN_STAT(mib.rx_65_127), ETH_MIB_RX_65_127 },
1189 { "rx_128_255_oct", GEN_STAT(mib.rx_128_255), ETH_MIB_RX_128_255 },
1190 { "rx_256_511_oct", GEN_STAT(mib.rx_256_511), ETH_MIB_RX_256_511 },
1191 { "rx_512_1023_oct", GEN_STAT(mib.rx_512_1023), ETH_MIB_RX_512_1023 },
1192 { "rx_1024_max_oct", GEN_STAT(mib.rx_1024_max), ETH_MIB_RX_1024_MAX },
1193 { "rx_jabber", GEN_STAT(mib.rx_jab), ETH_MIB_RX_JAB },
1194 { "rx_oversize", GEN_STAT(mib.rx_ovr), ETH_MIB_RX_OVR },
1195 { "rx_fragment", GEN_STAT(mib.rx_frag), ETH_MIB_RX_FRAG },
1196 { "rx_dropped", GEN_STAT(mib.rx_drop), ETH_MIB_RX_DROP },
1197 { "rx_crc_align", GEN_STAT(mib.rx_crc_align), ETH_MIB_RX_CRC_ALIGN },
1198 { "rx_undersize", GEN_STAT(mib.rx_und), ETH_MIB_RX_UND },
1199 { "rx_crc", GEN_STAT(mib.rx_crc), ETH_MIB_RX_CRC },
1200 { "rx_align", GEN_STAT(mib.rx_align), ETH_MIB_RX_ALIGN },
1201 { "rx_symbol_error", GEN_STAT(mib.rx_sym), ETH_MIB_RX_SYM },
1202 { "rx_pause", GEN_STAT(mib.rx_pause), ETH_MIB_RX_PAUSE },
1203 { "rx_control", GEN_STAT(mib.rx_cntrl), ETH_MIB_RX_CNTRL },
1205 { "tx_good_octets", GEN_STAT(mib.tx_gd_octets), ETH_MIB_TX_GD_OCTETS },
1206 { "tx_good_pkts", GEN_STAT(mib.tx_gd_pkts), ETH_MIB_TX_GD_PKTS },
1207 { "tx_broadcast", GEN_STAT(mib.tx_brdcast), ETH_MIB_TX_BRDCAST },
1208 { "tx_multicast", GEN_STAT(mib.tx_mult), ETH_MIB_TX_MULT },
1209 { "tx_64_oct", GEN_STAT(mib.tx_64), ETH_MIB_TX_64 },
1210 { "tx_65_127_oct", GEN_STAT(mib.tx_65_127), ETH_MIB_TX_65_127 },
1211 { "tx_128_255_oct", GEN_STAT(mib.tx_128_255), ETH_MIB_TX_128_255 },
1212 { "tx_256_511_oct", GEN_STAT(mib.tx_256_511), ETH_MIB_TX_256_511 },
1213 { "tx_512_1023_oct", GEN_STAT(mib.tx_512_1023), ETH_MIB_TX_512_1023},
1214 { "tx_1024_max_oct", GEN_STAT(mib.tx_1024_max), ETH_MIB_TX_1024_MAX },
1215 { "tx_jabber", GEN_STAT(mib.tx_jab), ETH_MIB_TX_JAB },
1216 { "tx_oversize", GEN_STAT(mib.tx_ovr), ETH_MIB_TX_OVR },
1217 { "tx_fragment", GEN_STAT(mib.tx_frag), ETH_MIB_TX_FRAG },
1218 { "tx_underrun", GEN_STAT(mib.tx_underrun), ETH_MIB_TX_UNDERRUN },
1219 { "tx_collisions", GEN_STAT(mib.tx_col), ETH_MIB_TX_COL },
1220 { "tx_single_collision", GEN_STAT(mib.tx_1_col), ETH_MIB_TX_1_COL },
1221 { "tx_multiple_collision", GEN_STAT(mib.tx_m_col), ETH_MIB_TX_M_COL },
1222 { "tx_excess_collision", GEN_STAT(mib.tx_ex_col), ETH_MIB_TX_EX_COL },
1223 { "tx_late_collision", GEN_STAT(mib.tx_late), ETH_MIB_TX_LATE },
1224 { "tx_deferred", GEN_STAT(mib.tx_def), ETH_MIB_TX_DEF },
1225 { "tx_carrier_sense", GEN_STAT(mib.tx_crs), ETH_MIB_TX_CRS },
1226 { "tx_pause", GEN_STAT(mib.tx_pause), ETH_MIB_TX_PAUSE },
1230 #define BCM_ENET_STATS_LEN \
1231 (sizeof(bcm_enet_gstrings_stats) / sizeof(struct bcm_enet_stats))
1233 static const u32 unused_mib_regs[] = {
1234 ETH_MIB_TX_ALL_OCTETS,
1235 ETH_MIB_TX_ALL_PKTS,
1236 ETH_MIB_RX_ALL_OCTETS,
1237 ETH_MIB_RX_ALL_PKTS,
1241 static void bcm_enet_get_drvinfo(struct net_device *netdev,
1242 struct ethtool_drvinfo *drvinfo)
1244 strncpy(drvinfo->driver, bcm_enet_driver_name, 32);
1245 strncpy(drvinfo->version, bcm_enet_driver_version, 32);
1246 strncpy(drvinfo->fw_version, "N/A", 32);
1247 strncpy(drvinfo->bus_info, "bcm63xx", 32);
1248 drvinfo->n_stats = BCM_ENET_STATS_LEN;
1251 static int bcm_enet_get_sset_count(struct net_device *netdev,
1252 int string_set)
1254 switch (string_set) {
1255 case ETH_SS_STATS:
1256 return BCM_ENET_STATS_LEN;
1257 default:
1258 return -EINVAL;
1262 static void bcm_enet_get_strings(struct net_device *netdev,
1263 u32 stringset, u8 *data)
1265 int i;
1267 switch (stringset) {
1268 case ETH_SS_STATS:
1269 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1270 memcpy(data + i * ETH_GSTRING_LEN,
1271 bcm_enet_gstrings_stats[i].stat_string,
1272 ETH_GSTRING_LEN);
1274 break;
1278 static void update_mib_counters(struct bcm_enet_priv *priv)
1280 int i;
1282 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1283 const struct bcm_enet_stats *s;
1284 u32 val;
1285 char *p;
1287 s = &bcm_enet_gstrings_stats[i];
1288 if (s->mib_reg == -1)
1289 continue;
1291 val = enet_readl(priv, ENET_MIB_REG(s->mib_reg));
1292 p = (char *)priv + s->stat_offset;
1294 if (s->sizeof_stat == sizeof(u64))
1295 *(u64 *)p += val;
1296 else
1297 *(u32 *)p += val;
1300 /* also empty unused mib counters to make sure mib counter
1301 * overflow interrupt is cleared */
1302 for (i = 0; i < ARRAY_SIZE(unused_mib_regs); i++)
1303 (void)enet_readl(priv, ENET_MIB_REG(unused_mib_regs[i]));
1306 static void bcm_enet_update_mib_counters_defer(struct work_struct *t)
1308 struct bcm_enet_priv *priv;
1310 priv = container_of(t, struct bcm_enet_priv, mib_update_task);
1311 mutex_lock(&priv->mib_update_lock);
1312 update_mib_counters(priv);
1313 mutex_unlock(&priv->mib_update_lock);
1315 /* reenable mib interrupt */
1316 if (netif_running(priv->net_dev))
1317 enet_writel(priv, ENET_IR_MIB, ENET_IRMASK_REG);
1320 static void bcm_enet_get_ethtool_stats(struct net_device *netdev,
1321 struct ethtool_stats *stats,
1322 u64 *data)
1324 struct bcm_enet_priv *priv;
1325 int i;
1327 priv = netdev_priv(netdev);
1329 mutex_lock(&priv->mib_update_lock);
1330 update_mib_counters(priv);
1332 for (i = 0; i < BCM_ENET_STATS_LEN; i++) {
1333 const struct bcm_enet_stats *s;
1334 char *p;
1336 s = &bcm_enet_gstrings_stats[i];
1337 p = (char *)priv + s->stat_offset;
1338 data[i] = (s->sizeof_stat == sizeof(u64)) ?
1339 *(u64 *)p : *(u32 *)p;
1341 mutex_unlock(&priv->mib_update_lock);
1344 static int bcm_enet_get_settings(struct net_device *dev,
1345 struct ethtool_cmd *cmd)
1347 struct bcm_enet_priv *priv;
1349 priv = netdev_priv(dev);
1351 cmd->maxrxpkt = 0;
1352 cmd->maxtxpkt = 0;
1354 if (priv->has_phy) {
1355 if (!priv->phydev)
1356 return -ENODEV;
1357 return phy_ethtool_gset(priv->phydev, cmd);
1358 } else {
1359 cmd->autoneg = 0;
1360 cmd->speed = (priv->force_speed_100) ? SPEED_100 : SPEED_10;
1361 cmd->duplex = (priv->force_duplex_full) ?
1362 DUPLEX_FULL : DUPLEX_HALF;
1363 cmd->supported = ADVERTISED_10baseT_Half |
1364 ADVERTISED_10baseT_Full |
1365 ADVERTISED_100baseT_Half |
1366 ADVERTISED_100baseT_Full;
1367 cmd->advertising = 0;
1368 cmd->port = PORT_MII;
1369 cmd->transceiver = XCVR_EXTERNAL;
1371 return 0;
1374 static int bcm_enet_set_settings(struct net_device *dev,
1375 struct ethtool_cmd *cmd)
1377 struct bcm_enet_priv *priv;
1379 priv = netdev_priv(dev);
1380 if (priv->has_phy) {
1381 if (!priv->phydev)
1382 return -ENODEV;
1383 return phy_ethtool_sset(priv->phydev, cmd);
1384 } else {
1386 if (cmd->autoneg ||
1387 (cmd->speed != SPEED_100 && cmd->speed != SPEED_10) ||
1388 cmd->port != PORT_MII)
1389 return -EINVAL;
1391 priv->force_speed_100 = (cmd->speed == SPEED_100) ? 1 : 0;
1392 priv->force_duplex_full = (cmd->duplex == DUPLEX_FULL) ? 1 : 0;
1394 if (netif_running(dev))
1395 bcm_enet_adjust_link(dev);
1396 return 0;
1400 static void bcm_enet_get_ringparam(struct net_device *dev,
1401 struct ethtool_ringparam *ering)
1403 struct bcm_enet_priv *priv;
1405 priv = netdev_priv(dev);
1407 /* rx/tx ring is actually only limited by memory */
1408 ering->rx_max_pending = 8192;
1409 ering->tx_max_pending = 8192;
1410 ering->rx_mini_max_pending = 0;
1411 ering->rx_jumbo_max_pending = 0;
1412 ering->rx_pending = priv->rx_ring_size;
1413 ering->tx_pending = priv->tx_ring_size;
1416 static int bcm_enet_set_ringparam(struct net_device *dev,
1417 struct ethtool_ringparam *ering)
1419 struct bcm_enet_priv *priv;
1420 int was_running;
1422 priv = netdev_priv(dev);
1424 was_running = 0;
1425 if (netif_running(dev)) {
1426 bcm_enet_stop(dev);
1427 was_running = 1;
1430 priv->rx_ring_size = ering->rx_pending;
1431 priv->tx_ring_size = ering->tx_pending;
1433 if (was_running) {
1434 int err;
1436 err = bcm_enet_open(dev);
1437 if (err)
1438 dev_close(dev);
1439 else
1440 bcm_enet_set_multicast_list(dev);
1442 return 0;
1445 static void bcm_enet_get_pauseparam(struct net_device *dev,
1446 struct ethtool_pauseparam *ecmd)
1448 struct bcm_enet_priv *priv;
1450 priv = netdev_priv(dev);
1451 ecmd->autoneg = priv->pause_auto;
1452 ecmd->rx_pause = priv->pause_rx;
1453 ecmd->tx_pause = priv->pause_tx;
1456 static int bcm_enet_set_pauseparam(struct net_device *dev,
1457 struct ethtool_pauseparam *ecmd)
1459 struct bcm_enet_priv *priv;
1461 priv = netdev_priv(dev);
1463 if (priv->has_phy) {
1464 if (ecmd->autoneg && (ecmd->rx_pause != ecmd->tx_pause)) {
1465 /* asymetric pause mode not supported,
1466 * actually possible but integrated PHY has RO
1467 * asym_pause bit */
1468 return -EINVAL;
1470 } else {
1471 /* no pause autoneg on direct mii connection */
1472 if (ecmd->autoneg)
1473 return -EINVAL;
1476 priv->pause_auto = ecmd->autoneg;
1477 priv->pause_rx = ecmd->rx_pause;
1478 priv->pause_tx = ecmd->tx_pause;
1480 return 0;
1483 static struct ethtool_ops bcm_enet_ethtool_ops = {
1484 .get_strings = bcm_enet_get_strings,
1485 .get_sset_count = bcm_enet_get_sset_count,
1486 .get_ethtool_stats = bcm_enet_get_ethtool_stats,
1487 .get_settings = bcm_enet_get_settings,
1488 .set_settings = bcm_enet_set_settings,
1489 .get_drvinfo = bcm_enet_get_drvinfo,
1490 .get_link = ethtool_op_get_link,
1491 .get_ringparam = bcm_enet_get_ringparam,
1492 .set_ringparam = bcm_enet_set_ringparam,
1493 .get_pauseparam = bcm_enet_get_pauseparam,
1494 .set_pauseparam = bcm_enet_set_pauseparam,
1497 static int bcm_enet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1499 struct bcm_enet_priv *priv;
1501 priv = netdev_priv(dev);
1502 if (priv->has_phy) {
1503 if (!priv->phydev)
1504 return -ENODEV;
1505 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
1506 } else {
1507 struct mii_if_info mii;
1509 mii.dev = dev;
1510 mii.mdio_read = bcm_enet_mdio_read_mii;
1511 mii.mdio_write = bcm_enet_mdio_write_mii;
1512 mii.phy_id = 0;
1513 mii.phy_id_mask = 0x3f;
1514 mii.reg_num_mask = 0x1f;
1515 return generic_mii_ioctl(&mii, if_mii(rq), cmd, NULL);
1520 * calculate actual hardware mtu
1522 static int compute_hw_mtu(struct bcm_enet_priv *priv, int mtu)
1524 int actual_mtu;
1526 actual_mtu = mtu;
1528 /* add ethernet header + vlan tag size */
1529 actual_mtu += VLAN_ETH_HLEN;
1531 if (actual_mtu < 64 || actual_mtu > BCMENET_MAX_MTU)
1532 return -EINVAL;
1535 * setup maximum size before we get overflow mark in
1536 * descriptor, note that this will not prevent reception of
1537 * big frames, they will be split into multiple buffers
1538 * anyway
1540 priv->hw_mtu = actual_mtu;
1543 * align rx buffer size to dma burst len, account FCS since
1544 * it's appended
1546 priv->rx_skb_size = ALIGN(actual_mtu + ETH_FCS_LEN,
1547 BCMENET_DMA_MAXBURST * 4);
1548 return 0;
1552 * adjust mtu, can't be called while device is running
1554 static int bcm_enet_change_mtu(struct net_device *dev, int new_mtu)
1556 int ret;
1558 if (netif_running(dev))
1559 return -EBUSY;
1561 ret = compute_hw_mtu(netdev_priv(dev), new_mtu);
1562 if (ret)
1563 return ret;
1564 dev->mtu = new_mtu;
1565 return 0;
1569 * preinit hardware to allow mii operation while device is down
1571 static void bcm_enet_hw_preinit(struct bcm_enet_priv *priv)
1573 u32 val;
1574 int limit;
1576 /* make sure mac is disabled */
1577 bcm_enet_disable_mac(priv);
1579 /* soft reset mac */
1580 val = ENET_CTL_SRESET_MASK;
1581 enet_writel(priv, val, ENET_CTL_REG);
1582 wmb();
1584 limit = 1000;
1585 do {
1586 val = enet_readl(priv, ENET_CTL_REG);
1587 if (!(val & ENET_CTL_SRESET_MASK))
1588 break;
1589 udelay(1);
1590 } while (limit--);
1592 /* select correct mii interface */
1593 val = enet_readl(priv, ENET_CTL_REG);
1594 if (priv->use_external_mii)
1595 val |= ENET_CTL_EPHYSEL_MASK;
1596 else
1597 val &= ~ENET_CTL_EPHYSEL_MASK;
1598 enet_writel(priv, val, ENET_CTL_REG);
1600 /* turn on mdc clock */
1601 enet_writel(priv, (0x1f << ENET_MIISC_MDCFREQDIV_SHIFT) |
1602 ENET_MIISC_PREAMBLEEN_MASK, ENET_MIISC_REG);
1604 /* set mib counters to self-clear when read */
1605 val = enet_readl(priv, ENET_MIBCTL_REG);
1606 val |= ENET_MIBCTL_RDCLEAR_MASK;
1607 enet_writel(priv, val, ENET_MIBCTL_REG);
1610 static const struct net_device_ops bcm_enet_ops = {
1611 .ndo_open = bcm_enet_open,
1612 .ndo_stop = bcm_enet_stop,
1613 .ndo_start_xmit = bcm_enet_start_xmit,
1614 .ndo_get_stats = bcm_enet_get_stats,
1615 .ndo_set_mac_address = bcm_enet_set_mac_address,
1616 .ndo_set_multicast_list = bcm_enet_set_multicast_list,
1617 .ndo_do_ioctl = bcm_enet_ioctl,
1618 .ndo_change_mtu = bcm_enet_change_mtu,
1619 #ifdef CONFIG_NET_POLL_CONTROLLER
1620 .ndo_poll_controller = bcm_enet_netpoll,
1621 #endif
1625 * allocate netdevice, request register memory and register device.
1627 static int __devinit bcm_enet_probe(struct platform_device *pdev)
1629 struct bcm_enet_priv *priv;
1630 struct net_device *dev;
1631 struct bcm63xx_enet_platform_data *pd;
1632 struct resource *res_mem, *res_irq, *res_irq_rx, *res_irq_tx;
1633 struct mii_bus *bus;
1634 const char *clk_name;
1635 unsigned int iomem_size;
1636 int i, ret;
1638 /* stop if shared driver failed, assume driver->probe will be
1639 * called in the same order we register devices (correct ?) */
1640 if (!bcm_enet_shared_base)
1641 return -ENODEV;
1643 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1644 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1645 res_irq_rx = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1646 res_irq_tx = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
1647 if (!res_mem || !res_irq || !res_irq_rx || !res_irq_tx)
1648 return -ENODEV;
1650 ret = 0;
1651 dev = alloc_etherdev(sizeof(*priv));
1652 if (!dev)
1653 return -ENOMEM;
1654 priv = netdev_priv(dev);
1655 memset(priv, 0, sizeof(*priv));
1657 ret = compute_hw_mtu(priv, dev->mtu);
1658 if (ret)
1659 goto out;
1661 iomem_size = res_mem->end - res_mem->start + 1;
1662 if (!request_mem_region(res_mem->start, iomem_size, "bcm63xx_enet")) {
1663 ret = -EBUSY;
1664 goto out;
1667 priv->base = ioremap(res_mem->start, iomem_size);
1668 if (priv->base == NULL) {
1669 ret = -ENOMEM;
1670 goto out_release_mem;
1672 dev->irq = priv->irq = res_irq->start;
1673 priv->irq_rx = res_irq_rx->start;
1674 priv->irq_tx = res_irq_tx->start;
1675 priv->mac_id = pdev->id;
1677 /* get rx & tx dma channel id for this mac */
1678 if (priv->mac_id == 0) {
1679 priv->rx_chan = 0;
1680 priv->tx_chan = 1;
1681 clk_name = "enet0";
1682 } else {
1683 priv->rx_chan = 2;
1684 priv->tx_chan = 3;
1685 clk_name = "enet1";
1688 priv->mac_clk = clk_get(&pdev->dev, clk_name);
1689 if (IS_ERR(priv->mac_clk)) {
1690 ret = PTR_ERR(priv->mac_clk);
1691 goto out_unmap;
1693 clk_enable(priv->mac_clk);
1695 /* initialize default and fetch platform data */
1696 priv->rx_ring_size = BCMENET_DEF_RX_DESC;
1697 priv->tx_ring_size = BCMENET_DEF_TX_DESC;
1699 pd = pdev->dev.platform_data;
1700 if (pd) {
1701 memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
1702 priv->has_phy = pd->has_phy;
1703 priv->phy_id = pd->phy_id;
1704 priv->has_phy_interrupt = pd->has_phy_interrupt;
1705 priv->phy_interrupt = pd->phy_interrupt;
1706 priv->use_external_mii = !pd->use_internal_phy;
1707 priv->pause_auto = pd->pause_auto;
1708 priv->pause_rx = pd->pause_rx;
1709 priv->pause_tx = pd->pause_tx;
1710 priv->force_duplex_full = pd->force_duplex_full;
1711 priv->force_speed_100 = pd->force_speed_100;
1714 if (priv->mac_id == 0 && priv->has_phy && !priv->use_external_mii) {
1715 /* using internal PHY, enable clock */
1716 priv->phy_clk = clk_get(&pdev->dev, "ephy");
1717 if (IS_ERR(priv->phy_clk)) {
1718 ret = PTR_ERR(priv->phy_clk);
1719 priv->phy_clk = NULL;
1720 goto out_put_clk_mac;
1722 clk_enable(priv->phy_clk);
1725 /* do minimal hardware init to be able to probe mii bus */
1726 bcm_enet_hw_preinit(priv);
1728 /* MII bus registration */
1729 if (priv->has_phy) {
1731 priv->mii_bus = mdiobus_alloc();
1732 if (!priv->mii_bus) {
1733 ret = -ENOMEM;
1734 goto out_uninit_hw;
1737 bus = priv->mii_bus;
1738 bus->name = "bcm63xx_enet MII bus";
1739 bus->parent = &pdev->dev;
1740 bus->priv = priv;
1741 bus->read = bcm_enet_mdio_read_phylib;
1742 bus->write = bcm_enet_mdio_write_phylib;
1743 sprintf(bus->id, "%d", priv->mac_id);
1745 /* only probe bus where we think the PHY is, because
1746 * the mdio read operation return 0 instead of 0xffff
1747 * if a slave is not present on hw */
1748 bus->phy_mask = ~(1 << priv->phy_id);
1750 bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1751 if (!bus->irq) {
1752 ret = -ENOMEM;
1753 goto out_free_mdio;
1756 if (priv->has_phy_interrupt)
1757 bus->irq[priv->phy_id] = priv->phy_interrupt;
1758 else
1759 bus->irq[priv->phy_id] = PHY_POLL;
1761 ret = mdiobus_register(bus);
1762 if (ret) {
1763 dev_err(&pdev->dev, "unable to register mdio bus\n");
1764 goto out_free_mdio;
1766 } else {
1768 /* run platform code to initialize PHY device */
1769 if (pd->mii_config &&
1770 pd->mii_config(dev, 1, bcm_enet_mdio_read_mii,
1771 bcm_enet_mdio_write_mii)) {
1772 dev_err(&pdev->dev, "unable to configure mdio bus\n");
1773 goto out_uninit_hw;
1777 spin_lock_init(&priv->rx_lock);
1779 /* init rx timeout (used for oom) */
1780 init_timer(&priv->rx_timeout);
1781 priv->rx_timeout.function = bcm_enet_refill_rx_timer;
1782 priv->rx_timeout.data = (unsigned long)dev;
1784 /* init the mib update lock&work */
1785 mutex_init(&priv->mib_update_lock);
1786 INIT_WORK(&priv->mib_update_task, bcm_enet_update_mib_counters_defer);
1788 /* zero mib counters */
1789 for (i = 0; i < ENET_MIB_REG_COUNT; i++)
1790 enet_writel(priv, 0, ENET_MIB_REG(i));
1792 /* register netdevice */
1793 dev->netdev_ops = &bcm_enet_ops;
1794 netif_napi_add(dev, &priv->napi, bcm_enet_poll, 16);
1796 SET_ETHTOOL_OPS(dev, &bcm_enet_ethtool_ops);
1797 SET_NETDEV_DEV(dev, &pdev->dev);
1799 ret = register_netdev(dev);
1800 if (ret)
1801 goto out_unregister_mdio;
1803 netif_carrier_off(dev);
1804 platform_set_drvdata(pdev, dev);
1805 priv->pdev = pdev;
1806 priv->net_dev = dev;
1808 return 0;
1810 out_unregister_mdio:
1811 if (priv->mii_bus) {
1812 mdiobus_unregister(priv->mii_bus);
1813 kfree(priv->mii_bus->irq);
1816 out_free_mdio:
1817 if (priv->mii_bus)
1818 mdiobus_free(priv->mii_bus);
1820 out_uninit_hw:
1821 /* turn off mdc clock */
1822 enet_writel(priv, 0, ENET_MIISC_REG);
1823 if (priv->phy_clk) {
1824 clk_disable(priv->phy_clk);
1825 clk_put(priv->phy_clk);
1828 out_put_clk_mac:
1829 clk_disable(priv->mac_clk);
1830 clk_put(priv->mac_clk);
1832 out_unmap:
1833 iounmap(priv->base);
1835 out_release_mem:
1836 release_mem_region(res_mem->start, iomem_size);
1837 out:
1838 free_netdev(dev);
1839 return ret;
1844 * exit func, stops hardware and unregisters netdevice
1846 static int __devexit bcm_enet_remove(struct platform_device *pdev)
1848 struct bcm_enet_priv *priv;
1849 struct net_device *dev;
1850 struct resource *res;
1852 /* stop netdevice */
1853 dev = platform_get_drvdata(pdev);
1854 priv = netdev_priv(dev);
1855 unregister_netdev(dev);
1857 /* turn off mdc clock */
1858 enet_writel(priv, 0, ENET_MIISC_REG);
1860 if (priv->has_phy) {
1861 mdiobus_unregister(priv->mii_bus);
1862 kfree(priv->mii_bus->irq);
1863 mdiobus_free(priv->mii_bus);
1864 } else {
1865 struct bcm63xx_enet_platform_data *pd;
1867 pd = pdev->dev.platform_data;
1868 if (pd && pd->mii_config)
1869 pd->mii_config(dev, 0, bcm_enet_mdio_read_mii,
1870 bcm_enet_mdio_write_mii);
1873 /* release device resources */
1874 iounmap(priv->base);
1875 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1876 release_mem_region(res->start, res->end - res->start + 1);
1878 /* disable hw block clocks */
1879 if (priv->phy_clk) {
1880 clk_disable(priv->phy_clk);
1881 clk_put(priv->phy_clk);
1883 clk_disable(priv->mac_clk);
1884 clk_put(priv->mac_clk);
1886 platform_set_drvdata(pdev, NULL);
1887 free_netdev(dev);
1888 return 0;
1891 struct platform_driver bcm63xx_enet_driver = {
1892 .probe = bcm_enet_probe,
1893 .remove = __devexit_p(bcm_enet_remove),
1894 .driver = {
1895 .name = "bcm63xx_enet",
1896 .owner = THIS_MODULE,
1901 * reserve & remap memory space shared between all macs
1903 static int __devinit bcm_enet_shared_probe(struct platform_device *pdev)
1905 struct resource *res;
1906 unsigned int iomem_size;
1908 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1909 if (!res)
1910 return -ENODEV;
1912 iomem_size = res->end - res->start + 1;
1913 if (!request_mem_region(res->start, iomem_size, "bcm63xx_enet_dma"))
1914 return -EBUSY;
1916 bcm_enet_shared_base = ioremap(res->start, iomem_size);
1917 if (!bcm_enet_shared_base) {
1918 release_mem_region(res->start, iomem_size);
1919 return -ENOMEM;
1921 return 0;
1924 static int __devexit bcm_enet_shared_remove(struct platform_device *pdev)
1926 struct resource *res;
1928 iounmap(bcm_enet_shared_base);
1929 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1930 release_mem_region(res->start, res->end - res->start + 1);
1931 return 0;
1935 * this "shared" driver is needed because both macs share a single
1936 * address space
1938 struct platform_driver bcm63xx_enet_shared_driver = {
1939 .probe = bcm_enet_shared_probe,
1940 .remove = __devexit_p(bcm_enet_shared_remove),
1941 .driver = {
1942 .name = "bcm63xx_enet_shared",
1943 .owner = THIS_MODULE,
1948 * entry point
1950 static int __init bcm_enet_init(void)
1952 int ret;
1954 ret = platform_driver_register(&bcm63xx_enet_shared_driver);
1955 if (ret)
1956 return ret;
1958 ret = platform_driver_register(&bcm63xx_enet_driver);
1959 if (ret)
1960 platform_driver_unregister(&bcm63xx_enet_shared_driver);
1962 return ret;
1965 static void __exit bcm_enet_exit(void)
1967 platform_driver_unregister(&bcm63xx_enet_driver);
1968 platform_driver_unregister(&bcm63xx_enet_shared_driver);
1972 module_init(bcm_enet_init);
1973 module_exit(bcm_enet_exit);
1975 MODULE_DESCRIPTION("BCM63xx internal ethernet mac driver");
1976 MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
1977 MODULE_LICENSE("GPL");