2 * Driver for (BCM4706)? GBit MAC core on BCMA bus.
4 * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
6 * Licensed under the GNU/GPL. See COPYING for details.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/bcma/bcma.h>
13 #include <linux/etherdevice.h>
14 #include <linux/interrupt.h>
15 #include <linux/bcm47xx_nvram.h>
16 #include <linux/phy.h>
17 #include <linux/phy_fixed.h>
21 static bool bgmac_wait_value(struct bgmac *bgmac, u16 reg, u32 mask,
22 u32 value, int timeout)
27 for (i = 0; i < timeout / 10; i++) {
28 val = bgmac_read(bgmac, reg);
29 if ((val & mask) == value)
33 dev_err(bgmac->dev, "Timeout waiting for reg 0x%X\n", reg);
37 /**************************************************
39 **************************************************/
41 static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
49 /* Suspend DMA TX ring first.
50 * bgmac_wait_value doesn't support waiting for any of few values, so
51 * implement whole loop here.
53 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
54 BGMAC_DMA_TX_SUSPEND);
55 for (i = 0; i < 10000 / 10; i++) {
56 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
57 val &= BGMAC_DMA_TX_STAT;
58 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
59 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
60 val == BGMAC_DMA_TX_STAT_STOPPED) {
67 dev_err(bgmac->dev, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
68 ring->mmio_base, val);
70 /* Remove SUSPEND bit */
71 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
72 if (!bgmac_wait_value(bgmac,
73 ring->mmio_base + BGMAC_DMA_TX_STATUS,
74 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
76 dev_warn(bgmac->dev, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
79 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
80 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
81 dev_err(bgmac->dev, "Reset of DMA TX ring 0x%X failed\n",
86 static void bgmac_dma_tx_enable(struct bgmac *bgmac,
87 struct bgmac_dma_ring *ring)
91 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
92 if (bgmac->feature_flags & BGMAC_FEAT_TX_MASK_SETUP) {
93 ctl &= ~BGMAC_DMA_TX_BL_MASK;
94 ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
96 ctl &= ~BGMAC_DMA_TX_MR_MASK;
97 ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
99 ctl &= ~BGMAC_DMA_TX_PC_MASK;
100 ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
102 ctl &= ~BGMAC_DMA_TX_PT_MASK;
103 ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
105 ctl |= BGMAC_DMA_TX_ENABLE;
106 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
107 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
111 bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
112 int i, int len, u32 ctl0)
114 struct bgmac_slot_info *slot;
115 struct bgmac_dma_desc *dma_desc;
118 if (i == BGMAC_TX_RING_SLOTS - 1)
119 ctl0 |= BGMAC_DESC_CTL0_EOT;
121 ctl1 = len & BGMAC_DESC_CTL1_LEN;
123 slot = &ring->slots[i];
124 dma_desc = &ring->cpu_base[i];
125 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
126 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
127 dma_desc->ctl0 = cpu_to_le32(ctl0);
128 dma_desc->ctl1 = cpu_to_le32(ctl1);
131 static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
132 struct bgmac_dma_ring *ring,
135 struct device *dma_dev = bgmac->dma_dev;
136 struct net_device *net_dev = bgmac->net_dev;
137 int index = ring->end % BGMAC_TX_RING_SLOTS;
138 struct bgmac_slot_info *slot = &ring->slots[index];
143 if (skb->len > BGMAC_DESC_CTL1_LEN) {
144 netdev_err(bgmac->net_dev, "Too long skb (%d)\n", skb->len);
148 if (skb->ip_summed == CHECKSUM_PARTIAL)
149 skb_checksum_help(skb);
151 nr_frags = skb_shinfo(skb)->nr_frags;
153 /* ring->end - ring->start will return the number of valid slots,
154 * even when ring->end overflows
156 if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
157 netdev_err(bgmac->net_dev, "TX ring is full, queue should be stopped!\n");
158 netif_stop_queue(net_dev);
159 return NETDEV_TX_BUSY;
162 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
164 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
167 flags = BGMAC_DESC_CTL0_SOF;
169 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
171 bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
174 for (i = 0; i < nr_frags; i++) {
175 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
176 int len = skb_frag_size(frag);
178 index = (index + 1) % BGMAC_TX_RING_SLOTS;
179 slot = &ring->slots[index];
180 slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
182 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
185 if (i == nr_frags - 1)
186 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
188 bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
192 netdev_sent_queue(net_dev, skb->len);
193 ring->end += nr_frags + 1;
197 /* Increase ring->end to point empty slot. We tell hardware the first
198 * slot it should *not* read.
200 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
202 (ring->end % BGMAC_TX_RING_SLOTS) *
203 sizeof(struct bgmac_dma_desc));
205 if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
206 netif_stop_queue(net_dev);
211 dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
215 int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
216 struct bgmac_slot_info *slot = &ring->slots[index];
217 u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
218 int len = ctl1 & BGMAC_DESC_CTL1_LEN;
220 dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
224 netdev_err(bgmac->net_dev, "Mapping error of skb on ring 0x%X\n",
229 net_dev->stats.tx_dropped++;
230 net_dev->stats.tx_errors++;
234 /* Free transmitted packets */
235 static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
237 struct device *dma_dev = bgmac->dma_dev;
239 unsigned bytes_compl = 0, pkts_compl = 0;
241 /* The last slot that hardware didn't consume yet */
242 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
243 empty_slot &= BGMAC_DMA_TX_STATDPTR;
244 empty_slot -= ring->index_base;
245 empty_slot &= BGMAC_DMA_TX_STATDPTR;
246 empty_slot /= sizeof(struct bgmac_dma_desc);
248 while (ring->start != ring->end) {
249 int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
250 struct bgmac_slot_info *slot = &ring->slots[slot_idx];
254 if (slot_idx == empty_slot)
257 ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0);
258 ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
259 len = ctl1 & BGMAC_DESC_CTL1_LEN;
260 if (ctl0 & BGMAC_DESC_CTL0_SOF)
261 /* Unmap no longer used buffer */
262 dma_unmap_single(dma_dev, slot->dma_addr, len,
265 dma_unmap_page(dma_dev, slot->dma_addr, len,
269 bgmac->net_dev->stats.tx_bytes += slot->skb->len;
270 bgmac->net_dev->stats.tx_packets++;
271 bytes_compl += slot->skb->len;
274 /* Free memory! :) */
275 dev_kfree_skb(slot->skb);
286 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
288 if (netif_queue_stopped(bgmac->net_dev))
289 netif_wake_queue(bgmac->net_dev);
292 static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
294 if (!ring->mmio_base)
297 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
298 if (!bgmac_wait_value(bgmac,
299 ring->mmio_base + BGMAC_DMA_RX_STATUS,
300 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
302 dev_err(bgmac->dev, "Reset of ring 0x%X RX failed\n",
306 static void bgmac_dma_rx_enable(struct bgmac *bgmac,
307 struct bgmac_dma_ring *ring)
311 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
313 /* preserve ONLY bits 16-17 from current hardware value */
314 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
316 if (bgmac->feature_flags & BGMAC_FEAT_RX_MASK_SETUP) {
317 ctl &= ~BGMAC_DMA_RX_BL_MASK;
318 ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
320 ctl &= ~BGMAC_DMA_RX_PC_MASK;
321 ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
323 ctl &= ~BGMAC_DMA_RX_PT_MASK;
324 ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
326 ctl |= BGMAC_DMA_RX_ENABLE;
327 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
328 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
329 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
330 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
333 static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
334 struct bgmac_slot_info *slot)
336 struct device *dma_dev = bgmac->dma_dev;
338 struct bgmac_rx_header *rx;
342 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
346 /* Poison - if everything goes fine, hardware will overwrite it */
347 rx = buf + BGMAC_RX_BUF_OFFSET;
348 rx->len = cpu_to_le16(0xdead);
349 rx->flags = cpu_to_le16(0xbeef);
351 /* Map skb for the DMA */
352 dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
353 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
354 if (dma_mapping_error(dma_dev, dma_addr)) {
355 netdev_err(bgmac->net_dev, "DMA mapping error\n");
356 put_page(virt_to_head_page(buf));
360 /* Update the slot */
362 slot->dma_addr = dma_addr;
367 static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
368 struct bgmac_dma_ring *ring)
372 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
374 ring->end * sizeof(struct bgmac_dma_desc));
377 static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
378 struct bgmac_dma_ring *ring, int desc_idx)
380 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
381 u32 ctl0 = 0, ctl1 = 0;
383 if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
384 ctl0 |= BGMAC_DESC_CTL0_EOT;
385 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
386 /* Is there any BGMAC device that requires extension? */
387 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
388 * B43_DMA64_DCTL1_ADDREXT_MASK;
391 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
392 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
393 dma_desc->ctl0 = cpu_to_le32(ctl0);
394 dma_desc->ctl1 = cpu_to_le32(ctl1);
396 ring->end = desc_idx;
399 static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
400 struct bgmac_slot_info *slot)
402 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
404 dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
406 rx->len = cpu_to_le16(0xdead);
407 rx->flags = cpu_to_le16(0xbeef);
408 dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
412 static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
418 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
419 end_slot &= BGMAC_DMA_RX_STATDPTR;
420 end_slot -= ring->index_base;
421 end_slot &= BGMAC_DMA_RX_STATDPTR;
422 end_slot /= sizeof(struct bgmac_dma_desc);
424 while (ring->start != end_slot) {
425 struct device *dma_dev = bgmac->dma_dev;
426 struct bgmac_slot_info *slot = &ring->slots[ring->start];
427 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
429 void *buf = slot->buf;
430 dma_addr_t dma_addr = slot->dma_addr;
434 /* Prepare new skb as replacement */
435 if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
436 bgmac_dma_rx_poison_buf(dma_dev, slot);
440 /* Unmap buffer to make it accessible to the CPU */
441 dma_unmap_single(dma_dev, dma_addr,
442 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
444 /* Get info from the header */
445 len = le16_to_cpu(rx->len);
446 flags = le16_to_cpu(rx->flags);
448 /* Check for poison and drop or pass the packet */
449 if (len == 0xdead && flags == 0xbeef) {
450 netdev_err(bgmac->net_dev, "Found poisoned packet at slot %d, DMA issue!\n",
452 put_page(virt_to_head_page(buf));
453 bgmac->net_dev->stats.rx_errors++;
457 if (len > BGMAC_RX_ALLOC_SIZE) {
458 netdev_err(bgmac->net_dev, "Found oversized packet at slot %d, DMA issue!\n",
460 put_page(virt_to_head_page(buf));
461 bgmac->net_dev->stats.rx_length_errors++;
462 bgmac->net_dev->stats.rx_errors++;
469 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
470 if (unlikely(!skb)) {
471 netdev_err(bgmac->net_dev, "build_skb failed\n");
472 put_page(virt_to_head_page(buf));
473 bgmac->net_dev->stats.rx_errors++;
476 skb_put(skb, BGMAC_RX_FRAME_OFFSET +
477 BGMAC_RX_BUF_OFFSET + len);
478 skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
479 BGMAC_RX_BUF_OFFSET);
481 skb_checksum_none_assert(skb);
482 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
483 bgmac->net_dev->stats.rx_bytes += len;
484 bgmac->net_dev->stats.rx_packets++;
485 napi_gro_receive(&bgmac->napi, skb);
489 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
491 if (++ring->start >= BGMAC_RX_RING_SLOTS)
494 if (handled >= weight) /* Should never be greater */
498 bgmac_dma_rx_update_index(bgmac, ring);
503 /* Does ring support unaligned addressing? */
504 static bool bgmac_dma_unaligned(struct bgmac *bgmac,
505 struct bgmac_dma_ring *ring,
506 enum bgmac_dma_ring_type ring_type)
509 case BGMAC_DMA_RING_TX:
510 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
512 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
515 case BGMAC_DMA_RING_RX:
516 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
518 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
525 static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
526 struct bgmac_dma_ring *ring)
528 struct device *dma_dev = bgmac->dma_dev;
529 struct bgmac_dma_desc *dma_desc = ring->cpu_base;
530 struct bgmac_slot_info *slot;
533 for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
534 u32 ctl1 = le32_to_cpu(dma_desc[i].ctl1);
535 unsigned int len = ctl1 & BGMAC_DESC_CTL1_LEN;
537 slot = &ring->slots[i];
538 dev_kfree_skb(slot->skb);
544 dma_unmap_single(dma_dev, slot->dma_addr,
547 dma_unmap_page(dma_dev, slot->dma_addr,
552 static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
553 struct bgmac_dma_ring *ring)
555 struct device *dma_dev = bgmac->dma_dev;
556 struct bgmac_slot_info *slot;
559 for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
560 slot = &ring->slots[i];
564 dma_unmap_single(dma_dev, slot->dma_addr,
567 put_page(virt_to_head_page(slot->buf));
572 static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
573 struct bgmac_dma_ring *ring,
576 struct device *dma_dev = bgmac->dma_dev;
582 /* Free ring of descriptors */
583 size = num_slots * sizeof(struct bgmac_dma_desc);
584 dma_free_coherent(dma_dev, size, ring->cpu_base,
588 static void bgmac_dma_cleanup(struct bgmac *bgmac)
592 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
593 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
595 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
596 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
599 static void bgmac_dma_free(struct bgmac *bgmac)
603 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
604 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
605 BGMAC_TX_RING_SLOTS);
607 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
608 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
609 BGMAC_RX_RING_SLOTS);
612 static int bgmac_dma_alloc(struct bgmac *bgmac)
614 struct device *dma_dev = bgmac->dma_dev;
615 struct bgmac_dma_ring *ring;
616 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
617 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
618 int size; /* ring size: different for Tx and Rx */
621 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
622 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
624 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
625 if (!(bgmac_idm_read(bgmac, BCMA_IOST) & BCMA_IOST_DMA64)) {
626 dev_err(bgmac->dev, "Core does not report 64-bit DMA\n");
631 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
632 ring = &bgmac->tx_ring[i];
633 ring->mmio_base = ring_base[i];
635 /* Alloc ring of descriptors */
636 size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
637 ring->cpu_base = dma_alloc_coherent(dma_dev, size,
640 if (!ring->cpu_base) {
641 dev_err(bgmac->dev, "Allocation of TX ring 0x%X failed\n",
646 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
649 ring->index_base = lower_32_bits(ring->dma_base);
651 ring->index_base = 0;
653 /* No need to alloc TX slots yet */
656 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
657 ring = &bgmac->rx_ring[i];
658 ring->mmio_base = ring_base[i];
660 /* Alloc ring of descriptors */
661 size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
662 ring->cpu_base = dma_alloc_coherent(dma_dev, size,
665 if (!ring->cpu_base) {
666 dev_err(bgmac->dev, "Allocation of RX ring 0x%X failed\n",
671 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
674 ring->index_base = lower_32_bits(ring->dma_base);
676 ring->index_base = 0;
682 bgmac_dma_free(bgmac);
686 static int bgmac_dma_init(struct bgmac *bgmac)
688 struct bgmac_dma_ring *ring;
691 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
692 ring = &bgmac->tx_ring[i];
694 if (!ring->unaligned)
695 bgmac_dma_tx_enable(bgmac, ring);
696 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
697 lower_32_bits(ring->dma_base));
698 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
699 upper_32_bits(ring->dma_base));
701 bgmac_dma_tx_enable(bgmac, ring);
704 ring->end = 0; /* Points the slot that should *not* be read */
707 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
710 ring = &bgmac->rx_ring[i];
712 if (!ring->unaligned)
713 bgmac_dma_rx_enable(bgmac, ring);
714 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
715 lower_32_bits(ring->dma_base));
716 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
717 upper_32_bits(ring->dma_base));
719 bgmac_dma_rx_enable(bgmac, ring);
723 for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
724 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
728 bgmac_dma_rx_setup_desc(bgmac, ring, j);
731 bgmac_dma_rx_update_index(bgmac, ring);
737 bgmac_dma_cleanup(bgmac);
742 /**************************************************
744 **************************************************/
746 /* TODO: can we just drop @force? Can we don't reset MAC at all if there is
747 * nothing to change? Try if after stabilizng driver.
749 static void bgmac_umac_cmd_maskset(struct bgmac *bgmac, u32 mask, u32 set,
752 u32 cmdcfg = bgmac_umac_read(bgmac, UMAC_CMD);
753 u32 new_val = (cmdcfg & mask) | set;
756 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
757 cmdcfg_sr = CMD_SW_RESET;
759 cmdcfg_sr = CMD_SW_RESET_OLD;
761 bgmac_umac_maskset(bgmac, UMAC_CMD, ~0, cmdcfg_sr);
764 if (new_val != cmdcfg || force)
765 bgmac_umac_write(bgmac, UMAC_CMD, new_val);
767 bgmac_umac_maskset(bgmac, UMAC_CMD, ~cmdcfg_sr, 0);
771 static void bgmac_write_mac_address(struct bgmac *bgmac, const u8 *addr)
775 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
776 bgmac_umac_write(bgmac, UMAC_MAC0, tmp);
777 tmp = (addr[4] << 8) | addr[5];
778 bgmac_umac_write(bgmac, UMAC_MAC1, tmp);
781 static void bgmac_set_rx_mode(struct net_device *net_dev)
783 struct bgmac *bgmac = netdev_priv(net_dev);
785 if (net_dev->flags & IFF_PROMISC)
786 bgmac_umac_cmd_maskset(bgmac, ~0, CMD_PROMISC, true);
788 bgmac_umac_cmd_maskset(bgmac, ~CMD_PROMISC, 0, true);
791 #if 0 /* We don't use that regs yet */
792 static void bgmac_chip_stats_update(struct bgmac *bgmac)
796 if (!(bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)) {
797 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
798 bgmac->mib_tx_regs[i] =
800 BGMAC_TX_GOOD_OCTETS + (i * 4));
801 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
802 bgmac->mib_rx_regs[i] =
804 BGMAC_RX_GOOD_OCTETS + (i * 4));
807 /* TODO: what else? how to handle BCM4706? Specs are needed */
811 static void bgmac_clear_mib(struct bgmac *bgmac)
815 if (bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)
818 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
819 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
820 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
821 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
822 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
825 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
826 static void bgmac_mac_speed(struct bgmac *bgmac)
828 u32 mask = ~(CMD_SPEED_MASK << CMD_SPEED_SHIFT | CMD_HD_EN);
831 switch (bgmac->mac_speed) {
833 set |= CMD_SPEED_10 << CMD_SPEED_SHIFT;
836 set |= CMD_SPEED_100 << CMD_SPEED_SHIFT;
839 set |= CMD_SPEED_1000 << CMD_SPEED_SHIFT;
842 set |= CMD_SPEED_2500 << CMD_SPEED_SHIFT;
845 dev_err(bgmac->dev, "Unsupported speed: %d\n",
849 if (bgmac->mac_duplex == DUPLEX_HALF)
852 bgmac_umac_cmd_maskset(bgmac, mask, set, true);
855 static void bgmac_miiconfig(struct bgmac *bgmac)
857 if (bgmac->feature_flags & BGMAC_FEAT_FORCE_SPEED_2500) {
858 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
859 bgmac_idm_write(bgmac, BCMA_IOCTL,
860 bgmac_idm_read(bgmac, BCMA_IOCTL) |
861 0x40 | BGMAC_BCMA_IOCTL_SW_CLKEN);
863 bgmac->mac_speed = SPEED_2500;
864 bgmac->mac_duplex = DUPLEX_FULL;
865 bgmac_mac_speed(bgmac);
869 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
870 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
871 if (imode == 0 || imode == 1) {
872 bgmac->mac_speed = SPEED_100;
873 bgmac->mac_duplex = DUPLEX_FULL;
874 bgmac_mac_speed(bgmac);
879 static void bgmac_chip_reset_idm_config(struct bgmac *bgmac)
883 iost = bgmac_idm_read(bgmac, BCMA_IOST);
884 if (bgmac->feature_flags & BGMAC_FEAT_IOST_ATTACHED)
885 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
887 /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
888 if (!(bgmac->feature_flags & BGMAC_FEAT_NO_RESET)) {
891 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
892 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
893 if (bgmac->in_init || !bgmac->has_robosw)
894 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
896 bgmac_clk_enable(bgmac, flags);
899 if (iost & BGMAC_BCMA_IOST_ATTACHED && (bgmac->in_init || !bgmac->has_robosw))
900 bgmac_idm_write(bgmac, BCMA_IOCTL,
901 bgmac_idm_read(bgmac, BCMA_IOCTL) &
902 ~BGMAC_BCMA_IOCTL_SW_RESET);
905 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
906 static void bgmac_chip_reset(struct bgmac *bgmac)
911 if (bgmac_clk_enabled(bgmac)) {
912 if (!bgmac->stats_grabbed) {
913 /* bgmac_chip_stats_update(bgmac); */
914 bgmac->stats_grabbed = true;
917 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
918 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
920 bgmac_umac_cmd_maskset(bgmac, ~0, CMD_LCL_LOOP_EN, false);
923 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
924 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
926 /* TODO: Clear software multicast filter list */
929 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK))
930 bgmac_chip_reset_idm_config(bgmac);
932 /* Request Misc PLL for corerev > 2 */
933 if (bgmac->feature_flags & BGMAC_FEAT_MISC_PLL_REQ) {
934 bgmac_set(bgmac, BCMA_CLKCTLST,
935 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
936 bgmac_wait_value(bgmac, BCMA_CLKCTLST,
937 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
938 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
942 if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_PHY) {
944 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
945 BGMAC_CHIPCTL_1_IF_TYPE_MII;
948 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
949 if (kstrtou8(buf, 0, &et_swtype))
950 dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
955 } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_EPHYRMII) {
956 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RMII |
957 BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
958 } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_RGMII) {
959 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
960 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
962 bgmac_cco_ctl_maskset(bgmac, 1, ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
963 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
965 } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE) {
966 u32 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_MII |
967 BGMAC_CHIPCTL_4_SW_TYPE_EPHY;
971 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
972 if (kstrtou8(buf, 0, &et_swtype))
973 dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
975 sw_type = (et_swtype & 0x0f) << 12;
976 } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII) {
977 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_RGMII |
978 BGMAC_CHIPCTL_4_SW_TYPE_RGMII;
980 bgmac_cco_ctl_maskset(bgmac, 4, ~(BGMAC_CHIPCTL_4_IF_TYPE_MASK |
981 BGMAC_CHIPCTL_4_SW_TYPE_MASK),
983 } else if (bgmac->feature_flags & BGMAC_FEAT_CC7_IF_TYPE_RGMII) {
984 bgmac_cco_ctl_maskset(bgmac, 7, ~BGMAC_CHIPCTL_7_IF_TYPE_MASK,
985 BGMAC_CHIPCTL_7_IF_TYPE_RGMII);
988 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
989 * Specs don't say about using UMAC_CMD_SR, but in this routine
990 * UMAC_CMD is read _after_ putting chip in a reset. So it has to
991 * be keps until taking MAC out of the reset.
993 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
994 cmdcfg_sr = CMD_SW_RESET;
996 cmdcfg_sr = CMD_SW_RESET_OLD;
998 bgmac_umac_cmd_maskset(bgmac,
1001 CMD_RX_PAUSE_IGNORE |
1009 CMD_TX_PAUSE_IGNORE |
1017 bgmac->mac_speed = SPEED_UNKNOWN;
1018 bgmac->mac_duplex = DUPLEX_UNKNOWN;
1020 bgmac_clear_mib(bgmac);
1021 if (bgmac->feature_flags & BGMAC_FEAT_CMN_PHY_CTL)
1022 bgmac_cmn_maskset32(bgmac, BCMA_GMAC_CMN_PHY_CTL, ~0,
1023 BCMA_GMAC_CMN_PC_MTE);
1025 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1026 bgmac_miiconfig(bgmac);
1028 bgmac->mii_bus->reset(bgmac->mii_bus);
1030 netdev_reset_queue(bgmac->net_dev);
1033 static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1035 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1038 static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1040 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
1041 bgmac_read(bgmac, BGMAC_INT_MASK);
1044 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1045 static void bgmac_enable(struct bgmac *bgmac)
1051 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
1052 cmdcfg_sr = CMD_SW_RESET;
1054 cmdcfg_sr = CMD_SW_RESET_OLD;
1056 cmdcfg = bgmac_umac_read(bgmac, UMAC_CMD);
1057 bgmac_umac_cmd_maskset(bgmac, ~(CMD_TX_EN | CMD_RX_EN),
1060 cmdcfg |= CMD_TX_EN | CMD_RX_EN;
1061 bgmac_umac_write(bgmac, UMAC_CMD, cmdcfg);
1063 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1065 if (bgmac->feature_flags & BGMAC_FEAT_CLKCTLST || mode != 0)
1066 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1067 if (!(bgmac->feature_flags & BGMAC_FEAT_CLKCTLST) && mode == 2)
1068 bgmac_cco_ctl_maskset(bgmac, 1, ~0,
1069 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1071 if (bgmac->feature_flags & (BGMAC_FEAT_FLW_CTRL1 |
1072 BGMAC_FEAT_FLW_CTRL2)) {
1075 if (bgmac->feature_flags & BGMAC_FEAT_FLW_CTRL1)
1078 fl_ctl = 0x03cb04cb;
1080 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1081 bgmac_umac_write(bgmac, UMAC_PAUSE_CTRL, 0x27fff);
1084 if (bgmac->feature_flags & BGMAC_FEAT_SET_RXQ_CLK) {
1089 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1090 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1091 bp_clk = bgmac_get_bus_clock(bgmac) / 1000000;
1092 mdp = (bp_clk * 128 / 1000) - 3;
1093 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1094 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1098 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
1099 static void bgmac_chip_init(struct bgmac *bgmac)
1101 /* Clear any erroneously pending interrupts */
1102 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
1104 /* 1 interrupt per received frame */
1105 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1107 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1108 bgmac_umac_cmd_maskset(bgmac, ~CMD_RX_PAUSE_IGNORE, 0, true);
1110 bgmac_set_rx_mode(bgmac->net_dev);
1112 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
1114 if (bgmac->loopback)
1115 bgmac_umac_cmd_maskset(bgmac, ~0, CMD_LCL_LOOP_EN, false);
1117 bgmac_umac_cmd_maskset(bgmac, ~CMD_LCL_LOOP_EN, 0, false);
1119 bgmac_umac_write(bgmac, UMAC_MAX_FRAME_LEN, 32 + ETHER_MAX_LEN);
1121 bgmac_chip_intrs_on(bgmac);
1123 bgmac_enable(bgmac);
1126 static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1128 struct bgmac *bgmac = netdev_priv(dev_id);
1130 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1131 int_status &= bgmac->int_mask;
1136 int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1138 dev_err(bgmac->dev, "Unknown IRQs: 0x%08X\n", int_status);
1140 /* Disable new interrupts until handling existing ones */
1141 bgmac_chip_intrs_off(bgmac);
1143 napi_schedule(&bgmac->napi);
1148 static int bgmac_poll(struct napi_struct *napi, int weight)
1150 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
1154 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
1156 bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1157 handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
1159 /* Poll again if more events arrived in the meantime */
1160 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
1163 if (handled < weight) {
1164 napi_complete_done(napi, handled);
1165 bgmac_chip_intrs_on(bgmac);
1171 /**************************************************
1173 **************************************************/
1175 static int bgmac_open(struct net_device *net_dev)
1177 struct bgmac *bgmac = netdev_priv(net_dev);
1180 bgmac_chip_reset(bgmac);
1182 err = bgmac_dma_init(bgmac);
1186 /* Specs say about reclaiming rings here, but we do that in DMA init */
1187 bgmac_chip_init(bgmac);
1189 err = request_irq(bgmac->irq, bgmac_interrupt, IRQF_SHARED,
1190 net_dev->name, net_dev);
1192 dev_err(bgmac->dev, "IRQ request error: %d!\n", err);
1193 bgmac_dma_cleanup(bgmac);
1196 napi_enable(&bgmac->napi);
1198 phy_start(net_dev->phydev);
1200 netif_start_queue(net_dev);
1205 static int bgmac_stop(struct net_device *net_dev)
1207 struct bgmac *bgmac = netdev_priv(net_dev);
1209 netif_carrier_off(net_dev);
1211 phy_stop(net_dev->phydev);
1213 napi_disable(&bgmac->napi);
1214 bgmac_chip_intrs_off(bgmac);
1215 free_irq(bgmac->irq, net_dev);
1217 bgmac_chip_reset(bgmac);
1218 bgmac_dma_cleanup(bgmac);
1223 static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1224 struct net_device *net_dev)
1226 struct bgmac *bgmac = netdev_priv(net_dev);
1227 struct bgmac_dma_ring *ring;
1229 /* No QOS support yet */
1230 ring = &bgmac->tx_ring[0];
1231 return bgmac_dma_tx_add(bgmac, ring, skb);
1234 static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1236 struct bgmac *bgmac = netdev_priv(net_dev);
1237 struct sockaddr *sa = addr;
1240 ret = eth_prepare_mac_addr_change(net_dev, addr);
1244 eth_hw_addr_set(net_dev, sa->sa_data);
1245 bgmac_write_mac_address(bgmac, net_dev->dev_addr);
1247 eth_commit_mac_addr_change(net_dev, addr);
1251 static int bgmac_change_mtu(struct net_device *net_dev, int mtu)
1253 struct bgmac *bgmac = netdev_priv(net_dev);
1255 bgmac_umac_write(bgmac, UMAC_MAX_FRAME_LEN, 32 + mtu);
1259 static const struct net_device_ops bgmac_netdev_ops = {
1260 .ndo_open = bgmac_open,
1261 .ndo_stop = bgmac_stop,
1262 .ndo_start_xmit = bgmac_start_xmit,
1263 .ndo_set_rx_mode = bgmac_set_rx_mode,
1264 .ndo_set_mac_address = bgmac_set_mac_address,
1265 .ndo_validate_addr = eth_validate_addr,
1266 .ndo_eth_ioctl = phy_do_ioctl_running,
1267 .ndo_change_mtu = bgmac_change_mtu,
1270 /**************************************************
1272 **************************************************/
1280 static struct bgmac_stat bgmac_get_strings_stats[] = {
1281 { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
1282 { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
1283 { 8, BGMAC_TX_OCTETS, "tx_octets" },
1284 { 4, BGMAC_TX_PKTS, "tx_pkts" },
1285 { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
1286 { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
1287 { 4, BGMAC_TX_LEN_64, "tx_64" },
1288 { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
1289 { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
1290 { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
1291 { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
1292 { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
1293 { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
1294 { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
1295 { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
1296 { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
1297 { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
1298 { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
1299 { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
1300 { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
1301 { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
1302 { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
1303 { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
1304 { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
1305 { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
1306 { 4, BGMAC_TX_DEFERED, "tx_defered" },
1307 { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
1308 { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
1309 { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
1310 { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
1311 { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
1312 { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
1313 { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
1314 { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
1315 { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
1316 { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
1317 { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
1318 { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
1319 { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
1320 { 8, BGMAC_RX_OCTETS, "rx_octets" },
1321 { 4, BGMAC_RX_PKTS, "rx_pkts" },
1322 { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
1323 { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
1324 { 4, BGMAC_RX_LEN_64, "rx_64" },
1325 { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
1326 { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
1327 { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
1328 { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
1329 { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
1330 { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
1331 { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
1332 { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
1333 { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
1334 { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
1335 { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
1336 { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
1337 { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
1338 { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
1339 { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
1340 { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
1341 { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
1342 { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
1343 { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
1344 { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
1345 { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
1346 { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
1349 #define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
1351 static int bgmac_get_sset_count(struct net_device *dev, int string_set)
1353 switch (string_set) {
1355 return BGMAC_STATS_LEN;
1361 static void bgmac_get_strings(struct net_device *dev, u32 stringset,
1366 if (stringset != ETH_SS_STATS)
1369 for (i = 0; i < BGMAC_STATS_LEN; i++)
1370 strscpy(data + i * ETH_GSTRING_LEN,
1371 bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
1374 static void bgmac_get_ethtool_stats(struct net_device *dev,
1375 struct ethtool_stats *ss, uint64_t *data)
1377 struct bgmac *bgmac = netdev_priv(dev);
1378 const struct bgmac_stat *s;
1382 if (!netif_running(dev))
1385 for (i = 0; i < BGMAC_STATS_LEN; i++) {
1386 s = &bgmac_get_strings_stats[i];
1389 val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
1390 val |= bgmac_read(bgmac, s->offset);
1395 static void bgmac_get_drvinfo(struct net_device *net_dev,
1396 struct ethtool_drvinfo *info)
1398 strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1399 strscpy(info->bus_info, "AXI", sizeof(info->bus_info));
1402 static const struct ethtool_ops bgmac_ethtool_ops = {
1403 .get_strings = bgmac_get_strings,
1404 .get_sset_count = bgmac_get_sset_count,
1405 .get_ethtool_stats = bgmac_get_ethtool_stats,
1406 .get_drvinfo = bgmac_get_drvinfo,
1407 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1408 .set_link_ksettings = phy_ethtool_set_link_ksettings,
1411 /**************************************************
1413 **************************************************/
1415 void bgmac_adjust_link(struct net_device *net_dev)
1417 struct bgmac *bgmac = netdev_priv(net_dev);
1418 struct phy_device *phy_dev = net_dev->phydev;
1419 bool update = false;
1421 if (phy_dev->link) {
1422 if (phy_dev->speed != bgmac->mac_speed) {
1423 bgmac->mac_speed = phy_dev->speed;
1427 if (phy_dev->duplex != bgmac->mac_duplex) {
1428 bgmac->mac_duplex = phy_dev->duplex;
1434 bgmac_mac_speed(bgmac);
1435 phy_print_status(phy_dev);
1438 EXPORT_SYMBOL_GPL(bgmac_adjust_link);
1440 int bgmac_phy_connect_direct(struct bgmac *bgmac)
1442 struct fixed_phy_status fphy_status = {
1444 .speed = SPEED_1000,
1445 .duplex = DUPLEX_FULL,
1447 struct phy_device *phy_dev;
1450 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
1451 if (!phy_dev || IS_ERR(phy_dev)) {
1452 dev_err(bgmac->dev, "Failed to register fixed PHY device\n");
1456 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1457 PHY_INTERFACE_MODE_MII);
1459 dev_err(bgmac->dev, "Connecting PHY failed\n");
1465 EXPORT_SYMBOL_GPL(bgmac_phy_connect_direct);
1467 struct bgmac *bgmac_alloc(struct device *dev)
1469 struct net_device *net_dev;
1470 struct bgmac *bgmac;
1472 /* Allocation and references */
1473 net_dev = devm_alloc_etherdev(dev, sizeof(*bgmac));
1477 net_dev->netdev_ops = &bgmac_netdev_ops;
1478 net_dev->ethtool_ops = &bgmac_ethtool_ops;
1480 bgmac = netdev_priv(net_dev);
1482 bgmac->net_dev = net_dev;
1486 EXPORT_SYMBOL_GPL(bgmac_alloc);
1488 int bgmac_enet_probe(struct bgmac *bgmac)
1490 struct net_device *net_dev = bgmac->net_dev;
1493 bgmac->in_init = true;
1495 bgmac_chip_intrs_off(bgmac);
1497 net_dev->irq = bgmac->irq;
1498 SET_NETDEV_DEV(net_dev, bgmac->dev);
1499 dev_set_drvdata(bgmac->dev, bgmac);
1501 if (!is_valid_ether_addr(net_dev->dev_addr)) {
1502 dev_err(bgmac->dev, "Invalid MAC addr: %pM\n",
1504 eth_hw_addr_random(net_dev);
1505 dev_warn(bgmac->dev, "Using random MAC: %pM\n",
1509 /* This (reset &) enable is not preset in specs or reference driver but
1510 * Broadcom does it in arch PCI code when enabling fake PCI device.
1512 bgmac_clk_enable(bgmac, 0);
1514 /* This seems to be fixing IRQ by assigning OOB #6 to the core */
1515 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
1516 if (bgmac->feature_flags & BGMAC_FEAT_IRQ_ID_OOB_6)
1517 bgmac_idm_write(bgmac, BCMA_OOB_SEL_OUT_A30, 0x86);
1520 bgmac_chip_reset(bgmac);
1522 err = bgmac_dma_alloc(bgmac);
1524 dev_err(bgmac->dev, "Unable to alloc memory for DMA\n");
1528 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
1529 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
1530 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1532 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll);
1534 err = bgmac_phy_connect(bgmac);
1536 dev_err(bgmac->dev, "Cannot connect to phy\n");
1540 net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1541 net_dev->hw_features = net_dev->features;
1542 net_dev->vlan_features = net_dev->features;
1544 /* Omit FCS from max MTU size */
1545 net_dev->max_mtu = BGMAC_RX_MAX_FRAME_SIZE - ETH_FCS_LEN;
1547 bgmac->in_init = false;
1549 err = register_netdev(bgmac->net_dev);
1551 dev_err(bgmac->dev, "Cannot register net device\n");
1552 goto err_phy_disconnect;
1555 netif_carrier_off(net_dev);
1560 phy_disconnect(net_dev->phydev);
1562 bgmac_dma_free(bgmac);
1567 EXPORT_SYMBOL_GPL(bgmac_enet_probe);
1569 void bgmac_enet_remove(struct bgmac *bgmac)
1571 unregister_netdev(bgmac->net_dev);
1572 phy_disconnect(bgmac->net_dev->phydev);
1573 netif_napi_del(&bgmac->napi);
1574 bgmac_dma_free(bgmac);
1576 EXPORT_SYMBOL_GPL(bgmac_enet_remove);
1578 int bgmac_enet_suspend(struct bgmac *bgmac)
1580 if (!netif_running(bgmac->net_dev))
1583 phy_stop(bgmac->net_dev->phydev);
1585 netif_stop_queue(bgmac->net_dev);
1587 napi_disable(&bgmac->napi);
1589 netif_tx_lock(bgmac->net_dev);
1590 netif_device_detach(bgmac->net_dev);
1591 netif_tx_unlock(bgmac->net_dev);
1593 bgmac_chip_intrs_off(bgmac);
1594 bgmac_chip_reset(bgmac);
1595 bgmac_dma_cleanup(bgmac);
1599 EXPORT_SYMBOL_GPL(bgmac_enet_suspend);
1601 int bgmac_enet_resume(struct bgmac *bgmac)
1605 if (!netif_running(bgmac->net_dev))
1608 rc = bgmac_dma_init(bgmac);
1612 bgmac_chip_init(bgmac);
1614 napi_enable(&bgmac->napi);
1616 netif_tx_lock(bgmac->net_dev);
1617 netif_device_attach(bgmac->net_dev);
1618 netif_tx_unlock(bgmac->net_dev);
1620 netif_start_queue(bgmac->net_dev);
1622 phy_start(bgmac->net_dev->phydev);
1626 EXPORT_SYMBOL_GPL(bgmac_enet_resume);
1628 MODULE_AUTHOR("Rafał Miłecki");
1629 MODULE_LICENSE("GPL");