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.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/etherdevice.h>
15 #include <linux/mii.h>
16 #include <linux/interrupt.h>
17 #include <linux/dma-mapping.h>
18 #include <bcm47xx_nvram.h>
20 static const struct bcma_device_id bgmac_bcma_tbl[] = {
21 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_4706_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
22 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
25 MODULE_DEVICE_TABLE(bcma, bgmac_bcma_tbl);
27 static bool bgmac_wait_value(struct bcma_device *core, u16 reg, u32 mask,
28 u32 value, int timeout)
33 for (i = 0; i < timeout / 10; i++) {
34 val = bcma_read32(core, reg);
35 if ((val & mask) == value)
39 pr_err("Timeout waiting for reg 0x%X\n", reg);
43 /**************************************************
45 **************************************************/
47 static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
55 /* Suspend DMA TX ring first.
56 * bgmac_wait_value doesn't support waiting for any of few values, so
57 * implement whole loop here.
59 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
60 BGMAC_DMA_TX_SUSPEND);
61 for (i = 0; i < 10000 / 10; i++) {
62 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
63 val &= BGMAC_DMA_TX_STAT;
64 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
65 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
66 val == BGMAC_DMA_TX_STAT_STOPPED) {
73 bgmac_err(bgmac, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
74 ring->mmio_base, val);
76 /* Remove SUSPEND bit */
77 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
78 if (!bgmac_wait_value(bgmac->core,
79 ring->mmio_base + BGMAC_DMA_TX_STATUS,
80 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
82 bgmac_warn(bgmac, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
85 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
86 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
87 bgmac_err(bgmac, "Reset of DMA TX ring 0x%X failed\n",
92 static void bgmac_dma_tx_enable(struct bgmac *bgmac,
93 struct bgmac_dma_ring *ring)
97 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
98 ctl |= BGMAC_DMA_TX_ENABLE;
99 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
100 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
103 static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
104 struct bgmac_dma_ring *ring,
107 struct device *dma_dev = bgmac->core->dma_dev;
108 struct net_device *net_dev = bgmac->net_dev;
109 struct bgmac_dma_desc *dma_desc;
110 struct bgmac_slot_info *slot;
114 if (skb->len > BGMAC_DESC_CTL1_LEN) {
115 bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
119 if (ring->start <= ring->end)
120 free_slots = ring->start - ring->end + BGMAC_TX_RING_SLOTS;
122 free_slots = ring->start - ring->end;
123 if (free_slots == 1) {
124 bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
125 netif_stop_queue(net_dev);
126 return NETDEV_TX_BUSY;
129 slot = &ring->slots[ring->end];
131 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb->len,
133 if (dma_mapping_error(dma_dev, slot->dma_addr)) {
134 bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n",
139 ctl0 = BGMAC_DESC_CTL0_IOC | BGMAC_DESC_CTL0_SOF | BGMAC_DESC_CTL0_EOF;
140 if (ring->end == ring->num_slots - 1)
141 ctl0 |= BGMAC_DESC_CTL0_EOT;
142 ctl1 = skb->len & BGMAC_DESC_CTL1_LEN;
144 dma_desc = ring->cpu_base;
145 dma_desc += ring->end;
146 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
147 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
148 dma_desc->ctl0 = cpu_to_le32(ctl0);
149 dma_desc->ctl1 = cpu_to_le32(ctl1);
153 /* Increase ring->end to point empty slot. We tell hardware the first
154 * slot it should *not* read.
156 if (++ring->end >= BGMAC_TX_RING_SLOTS)
158 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
159 ring->end * sizeof(struct bgmac_dma_desc));
161 /* Always keep one slot free to allow detecting bugged calls. */
162 if (--free_slots == 1)
163 netif_stop_queue(net_dev);
168 netif_stop_queue(net_dev);
173 /* Free transmitted packets */
174 static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
176 struct device *dma_dev = bgmac->core->dma_dev;
180 /* The last slot that hardware didn't consume yet */
181 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
182 empty_slot &= BGMAC_DMA_TX_STATDPTR;
183 empty_slot /= sizeof(struct bgmac_dma_desc);
185 while (ring->start != empty_slot) {
186 struct bgmac_slot_info *slot = &ring->slots[ring->start];
189 /* Unmap no longer used buffer */
190 dma_unmap_single(dma_dev, slot->dma_addr,
191 slot->skb->len, DMA_TO_DEVICE);
194 /* Free memory! :) */
195 dev_kfree_skb(slot->skb);
198 bgmac_err(bgmac, "Hardware reported transmission for empty TX ring slot %d! End of ring: %d\n",
199 ring->start, ring->end);
202 if (++ring->start >= BGMAC_TX_RING_SLOTS)
207 if (freed && netif_queue_stopped(bgmac->net_dev))
208 netif_wake_queue(bgmac->net_dev);
211 static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
213 if (!ring->mmio_base)
216 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
217 if (!bgmac_wait_value(bgmac->core,
218 ring->mmio_base + BGMAC_DMA_RX_STATUS,
219 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
221 bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
225 static void bgmac_dma_rx_enable(struct bgmac *bgmac,
226 struct bgmac_dma_ring *ring)
230 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
231 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
232 ctl |= BGMAC_DMA_RX_ENABLE;
233 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
234 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
235 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
236 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
239 static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
240 struct bgmac_slot_info *slot)
242 struct device *dma_dev = bgmac->core->dma_dev;
243 struct bgmac_rx_header *rx;
246 slot->skb = netdev_alloc_skb(bgmac->net_dev, BGMAC_RX_BUF_SIZE);
248 bgmac_err(bgmac, "Allocation of skb failed!\n");
252 /* Poison - if everything goes fine, hardware will overwrite it */
253 rx = (struct bgmac_rx_header *)slot->skb->data;
254 rx->len = cpu_to_le16(0xdead);
255 rx->flags = cpu_to_le16(0xbeef);
257 /* Map skb for the DMA */
258 slot->dma_addr = dma_map_single(dma_dev, slot->skb->data,
259 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
260 if (dma_mapping_error(dma_dev, slot->dma_addr)) {
261 bgmac_err(bgmac, "DMA mapping error\n");
264 if (slot->dma_addr & 0xC0000000)
265 bgmac_warn(bgmac, "DMA address using 0xC0000000 bit(s), it may need translation trick\n");
270 static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
276 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
277 end_slot &= BGMAC_DMA_RX_STATDPTR;
278 end_slot /= sizeof(struct bgmac_dma_desc);
280 ring->end = end_slot;
282 while (ring->start != ring->end) {
283 struct device *dma_dev = bgmac->core->dma_dev;
284 struct bgmac_slot_info *slot = &ring->slots[ring->start];
285 struct sk_buff *skb = slot->skb;
286 struct sk_buff *new_skb;
287 struct bgmac_rx_header *rx;
290 /* Unmap buffer to make it accessible to the CPU */
291 dma_sync_single_for_cpu(dma_dev, slot->dma_addr,
292 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
294 /* Get info from the header */
295 rx = (struct bgmac_rx_header *)skb->data;
296 len = le16_to_cpu(rx->len);
297 flags = le16_to_cpu(rx->flags);
299 /* Check for poison and drop or pass the packet */
300 if (len == 0xdead && flags == 0xbeef) {
301 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
304 new_skb = netdev_alloc_skb_ip_align(bgmac->net_dev, len);
306 skb_put(new_skb, len);
307 skb_copy_from_linear_data_offset(skb, BGMAC_RX_FRAME_OFFSET,
311 eth_type_trans(new_skb, bgmac->net_dev);
312 netif_receive_skb(new_skb);
315 bgmac->net_dev->stats.rx_dropped++;
316 bgmac_err(bgmac, "Allocation of skb for copying packet failed!\n");
319 /* Poison the old skb */
320 rx->len = cpu_to_le16(0xdead);
321 rx->flags = cpu_to_le16(0xbeef);
324 /* Make it back accessible to the hardware */
325 dma_sync_single_for_device(dma_dev, slot->dma_addr,
326 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
328 if (++ring->start >= BGMAC_RX_RING_SLOTS)
331 if (handled >= weight) /* Should never be greater */
338 /* Does ring support unaligned addressing? */
339 static bool bgmac_dma_unaligned(struct bgmac *bgmac,
340 struct bgmac_dma_ring *ring,
341 enum bgmac_dma_ring_type ring_type)
344 case BGMAC_DMA_RING_TX:
345 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
347 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
350 case BGMAC_DMA_RING_RX:
351 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
353 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
360 static void bgmac_dma_ring_free(struct bgmac *bgmac,
361 struct bgmac_dma_ring *ring)
363 struct device *dma_dev = bgmac->core->dma_dev;
364 struct bgmac_slot_info *slot;
368 for (i = 0; i < ring->num_slots; i++) {
369 slot = &ring->slots[i];
372 dma_unmap_single(dma_dev, slot->dma_addr,
373 slot->skb->len, DMA_TO_DEVICE);
374 dev_kfree_skb(slot->skb);
378 if (ring->cpu_base) {
379 /* Free ring of descriptors */
380 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
381 dma_free_coherent(dma_dev, size, ring->cpu_base,
386 static void bgmac_dma_free(struct bgmac *bgmac)
390 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
391 bgmac_dma_ring_free(bgmac, &bgmac->tx_ring[i]);
392 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
393 bgmac_dma_ring_free(bgmac, &bgmac->rx_ring[i]);
396 static int bgmac_dma_alloc(struct bgmac *bgmac)
398 struct device *dma_dev = bgmac->core->dma_dev;
399 struct bgmac_dma_ring *ring;
400 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
401 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
402 int size; /* ring size: different for Tx and Rx */
406 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
407 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
409 if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
410 bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
414 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
415 ring = &bgmac->tx_ring[i];
416 ring->num_slots = BGMAC_TX_RING_SLOTS;
417 ring->mmio_base = ring_base[i];
418 if (bgmac_dma_unaligned(bgmac, ring, BGMAC_DMA_RING_TX))
419 bgmac_warn(bgmac, "TX on ring 0x%X supports unaligned addressing but this feature is not implemented\n",
422 /* Alloc ring of descriptors */
423 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
424 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
427 if (!ring->cpu_base) {
428 bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
432 if (ring->dma_base & 0xC0000000)
433 bgmac_warn(bgmac, "DMA address using 0xC0000000 bit(s), it may need translation trick\n");
435 /* No need to alloc TX slots yet */
438 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
441 ring = &bgmac->rx_ring[i];
442 ring->num_slots = BGMAC_RX_RING_SLOTS;
443 ring->mmio_base = ring_base[i];
444 if (bgmac_dma_unaligned(bgmac, ring, BGMAC_DMA_RING_RX))
445 bgmac_warn(bgmac, "RX on ring 0x%X supports unaligned addressing but this feature is not implemented\n",
448 /* Alloc ring of descriptors */
449 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
450 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
453 if (!ring->cpu_base) {
454 bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
459 if (ring->dma_base & 0xC0000000)
460 bgmac_warn(bgmac, "DMA address using 0xC0000000 bit(s), it may need translation trick\n");
463 for (j = 0; j < ring->num_slots; j++) {
464 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
466 bgmac_err(bgmac, "Can't allocate skb for slot in RX ring\n");
475 bgmac_dma_free(bgmac);
479 static void bgmac_dma_init(struct bgmac *bgmac)
481 struct bgmac_dma_ring *ring;
482 struct bgmac_dma_desc *dma_desc;
486 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
487 ring = &bgmac->tx_ring[i];
489 /* We don't implement unaligned addressing, so enable first */
490 bgmac_dma_tx_enable(bgmac, ring);
491 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
492 lower_32_bits(ring->dma_base));
493 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
494 upper_32_bits(ring->dma_base));
497 ring->end = 0; /* Points the slot that should *not* be read */
500 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
503 ring = &bgmac->rx_ring[i];
505 /* We don't implement unaligned addressing, so enable first */
506 bgmac_dma_rx_enable(bgmac, ring);
507 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
508 lower_32_bits(ring->dma_base));
509 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
510 upper_32_bits(ring->dma_base));
512 for (j = 0, dma_desc = ring->cpu_base; j < ring->num_slots;
516 if (j == ring->num_slots - 1)
517 ctl0 |= BGMAC_DESC_CTL0_EOT;
518 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
519 /* Is there any BGMAC device that requires extension? */
520 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
521 * B43_DMA64_DCTL1_ADDREXT_MASK;
524 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[j].dma_addr));
525 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[j].dma_addr));
526 dma_desc->ctl0 = cpu_to_le32(ctl0);
527 dma_desc->ctl1 = cpu_to_le32(ctl1);
530 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
531 ring->num_slots * sizeof(struct bgmac_dma_desc));
538 /**************************************************
540 **************************************************/
542 static u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
544 struct bcma_device *core;
549 BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
550 BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
551 BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
552 BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
553 BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
554 BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
555 BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
556 BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
557 BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
558 BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
559 BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
561 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
562 core = bgmac->core->bus->drv_gmac_cmn.core;
563 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
564 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
567 phy_access_addr = BGMAC_PHY_ACCESS;
568 phy_ctl_addr = BGMAC_PHY_CNTL;
571 tmp = bcma_read32(core, phy_ctl_addr);
572 tmp &= ~BGMAC_PC_EPA_MASK;
574 bcma_write32(core, phy_ctl_addr, tmp);
576 tmp = BGMAC_PA_START;
577 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
578 tmp |= reg << BGMAC_PA_REG_SHIFT;
579 bcma_write32(core, phy_access_addr, tmp);
581 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
582 bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
587 return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
590 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
591 static int bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
593 struct bcma_device *core;
598 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
599 core = bgmac->core->bus->drv_gmac_cmn.core;
600 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
601 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
604 phy_access_addr = BGMAC_PHY_ACCESS;
605 phy_ctl_addr = BGMAC_PHY_CNTL;
608 tmp = bcma_read32(core, phy_ctl_addr);
609 tmp &= ~BGMAC_PC_EPA_MASK;
611 bcma_write32(core, phy_ctl_addr, tmp);
613 bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
614 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
615 bgmac_warn(bgmac, "Error setting MDIO int\n");
617 tmp = BGMAC_PA_START;
618 tmp |= BGMAC_PA_WRITE;
619 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
620 tmp |= reg << BGMAC_PA_REG_SHIFT;
622 bcma_write32(core, phy_access_addr, tmp);
624 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
625 bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
633 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyforce */
634 static void bgmac_phy_force(struct bgmac *bgmac)
637 u16 mask = ~(BGMAC_PHY_CTL_SPEED | BGMAC_PHY_CTL_SPEED_MSB |
638 BGMAC_PHY_CTL_ANENAB | BGMAC_PHY_CTL_DUPLEX);
640 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
646 ctl = bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL);
648 if (bgmac->full_duplex)
649 ctl |= BGMAC_PHY_CTL_DUPLEX;
650 if (bgmac->speed == BGMAC_SPEED_100)
651 ctl |= BGMAC_PHY_CTL_SPEED_100;
652 else if (bgmac->speed == BGMAC_SPEED_1000)
653 ctl |= BGMAC_PHY_CTL_SPEED_1000;
654 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL, ctl);
657 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyadvertise */
658 static void bgmac_phy_advertise(struct bgmac *bgmac)
662 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
668 /* Adv selected 10/100 speeds */
669 adv = bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV);
670 adv &= ~(BGMAC_PHY_ADV_10HALF | BGMAC_PHY_ADV_10FULL |
671 BGMAC_PHY_ADV_100HALF | BGMAC_PHY_ADV_100FULL);
672 if (!bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_10)
673 adv |= BGMAC_PHY_ADV_10HALF;
674 if (!bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_100)
675 adv |= BGMAC_PHY_ADV_100HALF;
676 if (bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_10)
677 adv |= BGMAC_PHY_ADV_10FULL;
678 if (bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_100)
679 adv |= BGMAC_PHY_ADV_100FULL;
680 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV, adv);
682 /* Adv selected 1000 speeds */
683 adv = bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV2);
684 adv &= ~(BGMAC_PHY_ADV2_1000HALF | BGMAC_PHY_ADV2_1000FULL);
685 if (!bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_1000)
686 adv |= BGMAC_PHY_ADV2_1000HALF;
687 if (bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_1000)
688 adv |= BGMAC_PHY_ADV2_1000FULL;
689 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV2, adv);
692 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL,
693 bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL) |
694 BGMAC_PHY_CTL_RESTART);
697 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
698 static void bgmac_phy_init(struct bgmac *bgmac)
700 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
701 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
704 if (ci->id == BCMA_CHIP_ID_BCM5356) {
705 for (i = 0; i < 5; i++) {
706 bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
707 bgmac_phy_write(bgmac, i, 0x15, 0x0100);
708 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
709 bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
710 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
713 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
714 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
715 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
716 bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
717 bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
718 for (i = 0; i < 5; i++) {
719 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
720 bgmac_phy_write(bgmac, i, 0x16, 0x5284);
721 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
722 bgmac_phy_write(bgmac, i, 0x17, 0x0010);
723 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
724 bgmac_phy_write(bgmac, i, 0x16, 0x5296);
725 bgmac_phy_write(bgmac, i, 0x17, 0x1073);
726 bgmac_phy_write(bgmac, i, 0x17, 0x9073);
727 bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
728 bgmac_phy_write(bgmac, i, 0x17, 0x9273);
729 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
734 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
735 static void bgmac_phy_reset(struct bgmac *bgmac)
737 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
740 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL,
741 BGMAC_PHY_CTL_RESET);
743 if (bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL) &
745 bgmac_err(bgmac, "PHY reset failed\n");
746 bgmac_phy_init(bgmac);
749 /**************************************************
751 **************************************************/
753 /* TODO: can we just drop @force? Can we don't reset MAC at all if there is
754 * nothing to change? Try if after stabilizng driver.
756 static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
759 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
760 u32 new_val = (cmdcfg & mask) | set;
762 bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR);
765 if (new_val != cmdcfg || force)
766 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
768 bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR);
772 static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
776 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
777 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
778 tmp = (addr[4] << 8) | addr[5];
779 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
782 static void bgmac_set_rx_mode(struct net_device *net_dev)
784 struct bgmac *bgmac = netdev_priv(net_dev);
786 if (net_dev->flags & IFF_PROMISC)
787 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
789 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
792 #if 0 /* We don't use that regs yet */
793 static void bgmac_chip_stats_update(struct bgmac *bgmac)
797 if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
798 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
799 bgmac->mib_tx_regs[i] =
801 BGMAC_TX_GOOD_OCTETS + (i * 4));
802 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
803 bgmac->mib_rx_regs[i] =
805 BGMAC_RX_GOOD_OCTETS + (i * 4));
808 /* TODO: what else? how to handle BCM4706? Specs are needed */
812 static void bgmac_clear_mib(struct bgmac *bgmac)
816 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
819 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
820 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
821 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
822 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
823 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
826 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
827 static void bgmac_speed(struct bgmac *bgmac, int speed)
829 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
832 if (speed & BGMAC_SPEED_10)
833 set |= BGMAC_CMDCFG_ES_10;
834 if (speed & BGMAC_SPEED_100)
835 set |= BGMAC_CMDCFG_ES_100;
836 if (speed & BGMAC_SPEED_1000)
837 set |= BGMAC_CMDCFG_ES_1000;
838 if (!bgmac->full_duplex)
839 set |= BGMAC_CMDCFG_HD;
840 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
843 static void bgmac_miiconfig(struct bgmac *bgmac)
845 u8 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
847 if (imode == 0 || imode == 1) {
849 bgmac_speed(bgmac, BGMAC_SPEED_100);
851 bgmac_speed(bgmac, bgmac->speed);
855 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
856 static void bgmac_chip_reset(struct bgmac *bgmac)
858 struct bcma_device *core = bgmac->core;
859 struct bcma_bus *bus = core->bus;
860 struct bcma_chipinfo *ci = &bus->chipinfo;
865 if (bcma_core_is_enabled(core)) {
866 if (!bgmac->stats_grabbed) {
867 /* bgmac_chip_stats_update(bgmac); */
868 bgmac->stats_grabbed = true;
871 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
872 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
874 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
877 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
878 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
880 /* TODO: Clear software multicast filter list */
883 iost = bcma_aread32(core, BCMA_IOST);
884 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == 10) ||
885 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
886 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == 9))
887 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
889 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
890 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
891 if (!bgmac->has_robosw)
892 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
895 bcma_core_enable(core, flags);
897 if (core->id.rev > 2) {
898 bgmac_set(bgmac, BCMA_CLKCTLST, 1 << 8);
899 bgmac_wait_value(bgmac->core, BCMA_CLKCTLST, 1 << 24, 1 << 24,
903 if (ci->id == BCMA_CHIP_ID_BCM5357 || ci->id == BCMA_CHIP_ID_BCM4749 ||
904 ci->id == BCMA_CHIP_ID_BCM53572) {
905 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
907 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
908 BGMAC_CHIPCTL_1_IF_TYPE_RMII;
911 if (bcm47xx_nvram_getenv("et_swtype", buf, 1) > 0) {
912 if (kstrtou8(buf, 0, &et_swtype))
913 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
918 } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == 9) {
919 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
920 } else if ((ci->id != BCMA_CHIP_ID_BCM53572 && ci->pkg == 10) ||
921 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == 9)) {
922 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
923 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
925 bcma_chipco_chipctl_maskset(cc, 1,
926 ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
927 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
931 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
932 bcma_awrite32(core, BCMA_IOCTL,
933 bcma_aread32(core, BCMA_IOCTL) &
934 ~BGMAC_BCMA_IOCTL_SW_RESET);
936 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
937 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
938 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
939 * be keps until taking MAC out of the reset.
941 bgmac_cmdcfg_maskset(bgmac,
953 BGMAC_CMDCFG_PAD_EN |
961 bgmac_clear_mib(bgmac);
962 if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
963 bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
964 BCMA_GMAC_CMN_PC_MTE);
966 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
967 bgmac_miiconfig(bgmac);
968 bgmac_phy_init(bgmac);
970 bgmac->int_status = 0;
973 static void bgmac_chip_intrs_on(struct bgmac *bgmac)
975 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
978 static void bgmac_chip_intrs_off(struct bgmac *bgmac)
980 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
981 bgmac_read(bgmac, BGMAC_INT_MASK);
984 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
985 static void bgmac_enable(struct bgmac *bgmac)
987 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
995 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
996 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
997 BGMAC_CMDCFG_SR, true);
999 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1000 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1002 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1004 if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
1005 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1006 if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
1007 bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
1008 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1011 case BCMA_CHIP_ID_BCM5357:
1012 case BCMA_CHIP_ID_BCM4749:
1013 case BCMA_CHIP_ID_BCM53572:
1014 case BCMA_CHIP_ID_BCM4716:
1015 case BCMA_CHIP_ID_BCM47162:
1016 fl_ctl = 0x03cb04cb;
1017 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1018 ci->id == BCMA_CHIP_ID_BCM4749 ||
1019 ci->id == BCMA_CHIP_ID_BCM53572)
1021 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1022 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
1026 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1027 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1028 bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) / 1000000;
1029 mdp = (bp_clk * 128 / 1000) - 3;
1030 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1031 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1034 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
1035 static void bgmac_chip_init(struct bgmac *bgmac, bool full_init)
1037 struct bgmac_dma_ring *ring;
1040 /* 1 interrupt per received frame */
1041 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1043 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1044 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1046 bgmac_set_rx_mode(bgmac->net_dev);
1048 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
1050 if (bgmac->loopback)
1051 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
1053 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
1055 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1057 if (!bgmac->autoneg) {
1058 bgmac_speed(bgmac, bgmac->speed);
1059 bgmac_phy_force(bgmac);
1060 } else if (bgmac->speed) { /* if there is anything to adv */
1061 bgmac_phy_advertise(bgmac);
1065 bgmac_dma_init(bgmac);
1066 if (1) /* FIXME: is there any case we don't want IRQs? */
1067 bgmac_chip_intrs_on(bgmac);
1069 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
1070 ring = &bgmac->rx_ring[i];
1071 bgmac_dma_rx_enable(bgmac, ring);
1075 bgmac_enable(bgmac);
1078 static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1080 struct bgmac *bgmac = netdev_priv(dev_id);
1082 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1083 int_status &= bgmac->int_mask;
1089 bgmac_write(bgmac, BGMAC_INT_STATUS, int_status);
1091 /* Disable new interrupts until handling existing ones */
1092 bgmac_chip_intrs_off(bgmac);
1094 bgmac->int_status = int_status;
1096 napi_schedule(&bgmac->napi);
1101 static int bgmac_poll(struct napi_struct *napi, int weight)
1103 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
1104 struct bgmac_dma_ring *ring;
1107 if (bgmac->int_status & BGMAC_IS_TX0) {
1108 ring = &bgmac->tx_ring[0];
1109 bgmac_dma_tx_free(bgmac, ring);
1110 bgmac->int_status &= ~BGMAC_IS_TX0;
1113 if (bgmac->int_status & BGMAC_IS_RX) {
1114 ring = &bgmac->rx_ring[0];
1115 handled += bgmac_dma_rx_read(bgmac, ring, weight);
1116 bgmac->int_status &= ~BGMAC_IS_RX;
1119 if (bgmac->int_status) {
1120 bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", bgmac->int_status);
1121 bgmac->int_status = 0;
1124 if (handled < weight)
1125 napi_complete(napi);
1127 bgmac_chip_intrs_on(bgmac);
1132 /**************************************************
1134 **************************************************/
1136 static int bgmac_open(struct net_device *net_dev)
1138 struct bgmac *bgmac = netdev_priv(net_dev);
1141 bgmac_chip_reset(bgmac);
1142 /* Specs say about reclaiming rings here, but we do that in DMA init */
1143 bgmac_chip_init(bgmac, true);
1145 err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
1146 KBUILD_MODNAME, net_dev);
1148 bgmac_err(bgmac, "IRQ request error: %d!\n", err);
1151 napi_enable(&bgmac->napi);
1153 netif_carrier_on(net_dev);
1159 static int bgmac_stop(struct net_device *net_dev)
1161 struct bgmac *bgmac = netdev_priv(net_dev);
1163 netif_carrier_off(net_dev);
1165 napi_disable(&bgmac->napi);
1166 bgmac_chip_intrs_off(bgmac);
1167 free_irq(bgmac->core->irq, net_dev);
1169 bgmac_chip_reset(bgmac);
1174 static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1175 struct net_device *net_dev)
1177 struct bgmac *bgmac = netdev_priv(net_dev);
1178 struct bgmac_dma_ring *ring;
1180 /* No QOS support yet */
1181 ring = &bgmac->tx_ring[0];
1182 return bgmac_dma_tx_add(bgmac, ring, skb);
1185 static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1187 struct bgmac *bgmac = netdev_priv(net_dev);
1190 ret = eth_prepare_mac_addr_change(net_dev, addr);
1193 bgmac_write_mac_address(bgmac, (u8 *)addr);
1194 eth_commit_mac_addr_change(net_dev, addr);
1198 static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1200 struct bgmac *bgmac = netdev_priv(net_dev);
1201 struct mii_ioctl_data *data = if_mii(ifr);
1205 data->phy_id = bgmac->phyaddr;
1208 if (!netif_running(net_dev))
1210 data->val_out = bgmac_phy_read(bgmac, data->phy_id,
1211 data->reg_num & 0x1f);
1214 if (!netif_running(net_dev))
1216 bgmac_phy_write(bgmac, data->phy_id, data->reg_num & 0x1f,
1224 static const struct net_device_ops bgmac_netdev_ops = {
1225 .ndo_open = bgmac_open,
1226 .ndo_stop = bgmac_stop,
1227 .ndo_start_xmit = bgmac_start_xmit,
1228 .ndo_set_rx_mode = bgmac_set_rx_mode,
1229 .ndo_set_mac_address = bgmac_set_mac_address,
1230 .ndo_validate_addr = eth_validate_addr,
1231 .ndo_do_ioctl = bgmac_ioctl,
1234 /**************************************************
1236 **************************************************/
1238 static int bgmac_get_settings(struct net_device *net_dev,
1239 struct ethtool_cmd *cmd)
1241 struct bgmac *bgmac = netdev_priv(net_dev);
1243 cmd->supported = SUPPORTED_10baseT_Half |
1244 SUPPORTED_10baseT_Full |
1245 SUPPORTED_100baseT_Half |
1246 SUPPORTED_100baseT_Full |
1247 SUPPORTED_1000baseT_Half |
1248 SUPPORTED_1000baseT_Full |
1251 if (bgmac->autoneg) {
1252 WARN_ON(cmd->advertising);
1253 if (bgmac->full_duplex) {
1254 if (bgmac->speed & BGMAC_SPEED_10)
1255 cmd->advertising |= ADVERTISED_10baseT_Full;
1256 if (bgmac->speed & BGMAC_SPEED_100)
1257 cmd->advertising |= ADVERTISED_100baseT_Full;
1258 if (bgmac->speed & BGMAC_SPEED_1000)
1259 cmd->advertising |= ADVERTISED_1000baseT_Full;
1261 if (bgmac->speed & BGMAC_SPEED_10)
1262 cmd->advertising |= ADVERTISED_10baseT_Half;
1263 if (bgmac->speed & BGMAC_SPEED_100)
1264 cmd->advertising |= ADVERTISED_100baseT_Half;
1265 if (bgmac->speed & BGMAC_SPEED_1000)
1266 cmd->advertising |= ADVERTISED_1000baseT_Half;
1269 switch (bgmac->speed) {
1270 case BGMAC_SPEED_10:
1271 ethtool_cmd_speed_set(cmd, SPEED_10);
1273 case BGMAC_SPEED_100:
1274 ethtool_cmd_speed_set(cmd, SPEED_100);
1276 case BGMAC_SPEED_1000:
1277 ethtool_cmd_speed_set(cmd, SPEED_1000);
1282 cmd->duplex = bgmac->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
1284 cmd->autoneg = bgmac->autoneg;
1290 static int bgmac_set_settings(struct net_device *net_dev,
1291 struct ethtool_cmd *cmd)
1293 struct bgmac *bgmac = netdev_priv(net_dev);
1299 static void bgmac_get_drvinfo(struct net_device *net_dev,
1300 struct ethtool_drvinfo *info)
1302 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1303 strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
1306 static const struct ethtool_ops bgmac_ethtool_ops = {
1307 .get_settings = bgmac_get_settings,
1308 .get_drvinfo = bgmac_get_drvinfo,
1311 /**************************************************
1313 **************************************************/
1315 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
1316 static int bgmac_probe(struct bcma_device *core)
1318 struct net_device *net_dev;
1319 struct bgmac *bgmac;
1320 struct ssb_sprom *sprom = &core->bus->sprom;
1321 u8 *mac = core->core_unit ? sprom->et1mac : sprom->et0mac;
1324 /* We don't support 2nd, 3rd, ... units, SPROM has to be adjusted */
1325 if (core->core_unit > 1) {
1326 pr_err("Unsupported core_unit %d\n", core->core_unit);
1330 if (!is_valid_ether_addr(mac)) {
1331 dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
1332 eth_random_addr(mac);
1333 dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
1336 /* Allocation and references */
1337 net_dev = alloc_etherdev(sizeof(*bgmac));
1340 net_dev->netdev_ops = &bgmac_netdev_ops;
1341 net_dev->irq = core->irq;
1342 SET_ETHTOOL_OPS(net_dev, &bgmac_ethtool_ops);
1343 bgmac = netdev_priv(net_dev);
1344 bgmac->net_dev = net_dev;
1346 bcma_set_drvdata(core, bgmac);
1349 bgmac->autoneg = true;
1350 bgmac->full_duplex = true;
1351 bgmac->speed = BGMAC_SPEED_10 | BGMAC_SPEED_100 | BGMAC_SPEED_1000;
1352 memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
1354 /* On BCM4706 we need common core to access PHY */
1355 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
1356 !core->bus->drv_gmac_cmn.core) {
1357 bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
1359 goto err_netdev_free;
1361 bgmac->cmn = core->bus->drv_gmac_cmn.core;
1363 bgmac->phyaddr = core->core_unit ? sprom->et1phyaddr :
1365 bgmac->phyaddr &= BGMAC_PHY_MASK;
1366 if (bgmac->phyaddr == BGMAC_PHY_MASK) {
1367 bgmac_err(bgmac, "No PHY found\n");
1369 goto err_netdev_free;
1371 bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
1372 bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
1374 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
1375 bgmac_err(bgmac, "PCI setup not implemented\n");
1377 goto err_netdev_free;
1380 bgmac_chip_reset(bgmac);
1382 err = bgmac_dma_alloc(bgmac);
1384 bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
1385 goto err_netdev_free;
1388 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
1389 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
1390 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1392 /* TODO: reset the external phy. Specs are needed */
1393 bgmac_phy_reset(bgmac);
1395 bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
1396 BGMAC_BFL_ENETROBO);
1397 if (bgmac->has_robosw)
1398 bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
1400 if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
1401 bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
1403 err = register_netdev(bgmac->net_dev);
1405 bgmac_err(bgmac, "Cannot register net device\n");
1410 netif_carrier_off(net_dev);
1412 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1417 bgmac_dma_free(bgmac);
1420 bcma_set_drvdata(core, NULL);
1421 free_netdev(net_dev);
1426 static void bgmac_remove(struct bcma_device *core)
1428 struct bgmac *bgmac = bcma_get_drvdata(core);
1430 netif_napi_del(&bgmac->napi);
1431 unregister_netdev(bgmac->net_dev);
1432 bgmac_dma_free(bgmac);
1433 bcma_set_drvdata(core, NULL);
1434 free_netdev(bgmac->net_dev);
1437 static struct bcma_driver bgmac_bcma_driver = {
1438 .name = KBUILD_MODNAME,
1439 .id_table = bgmac_bcma_tbl,
1440 .probe = bgmac_probe,
1441 .remove = bgmac_remove,
1444 static int __init bgmac_init(void)
1448 err = bcma_driver_register(&bgmac_bcma_driver);
1451 pr_info("Broadcom 47xx GBit MAC driver loaded\n");
1456 static void __exit bgmac_exit(void)
1458 bcma_driver_unregister(&bgmac_bcma_driver);
1461 module_init(bgmac_init)
1462 module_exit(bgmac_exit)
1464 MODULE_AUTHOR("Rafał Miłecki");
1465 MODULE_LICENSE("GPL");