1dd4544f0SRafał Miłecki /*
2dd4544f0SRafał Miłecki * Driver for (BCM4706)? GBit MAC core on BCMA bus.
3dd4544f0SRafał Miłecki *
4dd4544f0SRafał Miłecki * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
5dd4544f0SRafał Miłecki *
6dd4544f0SRafał Miłecki * Licensed under the GNU/GPL. See COPYING for details.
7dd4544f0SRafał Miłecki */
8dd4544f0SRafał Miłecki
9f6a95a24SJon Mason
10f6a95a24SJon Mason #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11f6a95a24SJon Mason
12f6a95a24SJon Mason #include <linux/bcma/bcma.h>
13f6a95a24SJon Mason #include <linux/etherdevice.h>
14282ccf6eSFlorian Westphal #include <linux/interrupt.h>
15f6a95a24SJon Mason #include <linux/bcm47xx_nvram.h>
1613bf7760SRussell King #include <linux/phy.h>
1713bf7760SRussell King #include <linux/phy_fixed.h>
184d215ae7SFlorian Fainelli #include <net/dsa.h>
19dd4544f0SRafał Miłecki #include "bgmac.h"
20dd4544f0SRafał Miłecki
bgmac_wait_value(struct bgmac * bgmac,u16 reg,u32 mask,u32 value,int timeout)21f6a95a24SJon Mason static bool bgmac_wait_value(struct bgmac *bgmac, u16 reg, u32 mask,
22dd4544f0SRafał Miłecki u32 value, int timeout)
23dd4544f0SRafał Miłecki {
24dd4544f0SRafał Miłecki u32 val;
25dd4544f0SRafał Miłecki int i;
26dd4544f0SRafał Miłecki
27dd4544f0SRafał Miłecki for (i = 0; i < timeout / 10; i++) {
28f6a95a24SJon Mason val = bgmac_read(bgmac, reg);
29dd4544f0SRafał Miłecki if ((val & mask) == value)
30dd4544f0SRafał Miłecki return true;
31dd4544f0SRafał Miłecki udelay(10);
32dd4544f0SRafał Miłecki }
33f6a95a24SJon Mason dev_err(bgmac->dev, "Timeout waiting for reg 0x%X\n", reg);
34dd4544f0SRafał Miłecki return false;
35dd4544f0SRafał Miłecki }
36dd4544f0SRafał Miłecki
37dd4544f0SRafał Miłecki /**************************************************
38dd4544f0SRafał Miłecki * DMA
39dd4544f0SRafał Miłecki **************************************************/
40dd4544f0SRafał Miłecki
bgmac_dma_tx_reset(struct bgmac * bgmac,struct bgmac_dma_ring * ring)41dd4544f0SRafał Miłecki static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
42dd4544f0SRafał Miłecki {
43dd4544f0SRafał Miłecki u32 val;
44dd4544f0SRafał Miłecki int i;
45dd4544f0SRafał Miłecki
46dd4544f0SRafał Miłecki if (!ring->mmio_base)
47dd4544f0SRafał Miłecki return;
48dd4544f0SRafał Miłecki
49dd4544f0SRafał Miłecki /* Suspend DMA TX ring first.
50dd4544f0SRafał Miłecki * bgmac_wait_value doesn't support waiting for any of few values, so
51dd4544f0SRafał Miłecki * implement whole loop here.
52dd4544f0SRafał Miłecki */
53dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
54dd4544f0SRafał Miłecki BGMAC_DMA_TX_SUSPEND);
55dd4544f0SRafał Miłecki for (i = 0; i < 10000 / 10; i++) {
56dd4544f0SRafał Miłecki val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
57dd4544f0SRafał Miłecki val &= BGMAC_DMA_TX_STAT;
58dd4544f0SRafał Miłecki if (val == BGMAC_DMA_TX_STAT_DISABLED ||
59dd4544f0SRafał Miłecki val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
60dd4544f0SRafał Miłecki val == BGMAC_DMA_TX_STAT_STOPPED) {
61dd4544f0SRafał Miłecki i = 0;
62dd4544f0SRafał Miłecki break;
63dd4544f0SRafał Miłecki }
64dd4544f0SRafał Miłecki udelay(10);
65dd4544f0SRafał Miłecki }
66dd4544f0SRafał Miłecki if (i)
67d00a8281SJon Mason dev_err(bgmac->dev, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
68dd4544f0SRafał Miłecki ring->mmio_base, val);
69dd4544f0SRafał Miłecki
70dd4544f0SRafał Miłecki /* Remove SUSPEND bit */
71dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
72f6a95a24SJon Mason if (!bgmac_wait_value(bgmac,
73dd4544f0SRafał Miłecki ring->mmio_base + BGMAC_DMA_TX_STATUS,
74dd4544f0SRafał Miłecki BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
75dd4544f0SRafał Miłecki 10000)) {
76d00a8281SJon Mason dev_warn(bgmac->dev, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
77dd4544f0SRafał Miłecki ring->mmio_base);
78dd4544f0SRafał Miłecki udelay(300);
79dd4544f0SRafał Miłecki val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
80dd4544f0SRafał Miłecki if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
81d00a8281SJon Mason dev_err(bgmac->dev, "Reset of DMA TX ring 0x%X failed\n",
82dd4544f0SRafał Miłecki ring->mmio_base);
83dd4544f0SRafał Miłecki }
84dd4544f0SRafał Miłecki }
85dd4544f0SRafał Miłecki
bgmac_dma_tx_enable(struct bgmac * bgmac,struct bgmac_dma_ring * ring)86dd4544f0SRafał Miłecki static void bgmac_dma_tx_enable(struct bgmac *bgmac,
87dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring)
88dd4544f0SRafał Miłecki {
89dd4544f0SRafał Miłecki u32 ctl;
90dd4544f0SRafał Miłecki
91dd4544f0SRafał Miłecki ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
92db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_TX_MASK_SETUP) {
9356ceecdeSHauke Mehrtens ctl &= ~BGMAC_DMA_TX_BL_MASK;
9456ceecdeSHauke Mehrtens ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
9556ceecdeSHauke Mehrtens
9656ceecdeSHauke Mehrtens ctl &= ~BGMAC_DMA_TX_MR_MASK;
9756ceecdeSHauke Mehrtens ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
9856ceecdeSHauke Mehrtens
9956ceecdeSHauke Mehrtens ctl &= ~BGMAC_DMA_TX_PC_MASK;
10056ceecdeSHauke Mehrtens ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
10156ceecdeSHauke Mehrtens
10256ceecdeSHauke Mehrtens ctl &= ~BGMAC_DMA_TX_PT_MASK;
10356ceecdeSHauke Mehrtens ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
10456ceecdeSHauke Mehrtens }
105dd4544f0SRafał Miłecki ctl |= BGMAC_DMA_TX_ENABLE;
106dd4544f0SRafał Miłecki ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
107dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
108dd4544f0SRafał Miłecki }
109dd4544f0SRafał Miłecki
1109cde9450SFelix Fietkau static void
bgmac_dma_tx_add_buf(struct bgmac * bgmac,struct bgmac_dma_ring * ring,int i,int len,u32 ctl0)1119cde9450SFelix Fietkau bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
1129cde9450SFelix Fietkau int i, int len, u32 ctl0)
1139cde9450SFelix Fietkau {
1149cde9450SFelix Fietkau struct bgmac_slot_info *slot;
1159cde9450SFelix Fietkau struct bgmac_dma_desc *dma_desc;
1169cde9450SFelix Fietkau u32 ctl1;
1179cde9450SFelix Fietkau
11829ba877eSFelix Fietkau if (i == BGMAC_TX_RING_SLOTS - 1)
1199cde9450SFelix Fietkau ctl0 |= BGMAC_DESC_CTL0_EOT;
1209cde9450SFelix Fietkau
1219cde9450SFelix Fietkau ctl1 = len & BGMAC_DESC_CTL1_LEN;
1229cde9450SFelix Fietkau
1239cde9450SFelix Fietkau slot = &ring->slots[i];
1249cde9450SFelix Fietkau dma_desc = &ring->cpu_base[i];
1259cde9450SFelix Fietkau dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
1269cde9450SFelix Fietkau dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
1279cde9450SFelix Fietkau dma_desc->ctl0 = cpu_to_le32(ctl0);
1289cde9450SFelix Fietkau dma_desc->ctl1 = cpu_to_le32(ctl1);
1299cde9450SFelix Fietkau }
1309cde9450SFelix Fietkau
bgmac_dma_tx_add(struct bgmac * bgmac,struct bgmac_dma_ring * ring,struct sk_buff * skb)131dd4544f0SRafał Miłecki static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
132dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring,
133dd4544f0SRafał Miłecki struct sk_buff *skb)
134dd4544f0SRafał Miłecki {
135a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
136dd4544f0SRafał Miłecki struct net_device *net_dev = bgmac->net_dev;
137b38c83ddSFelix Fietkau int index = ring->end % BGMAC_TX_RING_SLOTS;
138b38c83ddSFelix Fietkau struct bgmac_slot_info *slot = &ring->slots[index];
1399cde9450SFelix Fietkau int nr_frags;
1409cde9450SFelix Fietkau u32 flags;
1419cde9450SFelix Fietkau int i;
142dd4544f0SRafał Miłecki
143dd4544f0SRafał Miłecki if (skb->len > BGMAC_DESC_CTL1_LEN) {
144d00a8281SJon Mason netdev_err(bgmac->net_dev, "Too long skb (%d)\n", skb->len);
1459cde9450SFelix Fietkau goto err_drop;
146dd4544f0SRafał Miłecki }
147dd4544f0SRafał Miłecki
1489cde9450SFelix Fietkau if (skb->ip_summed == CHECKSUM_PARTIAL)
1499cde9450SFelix Fietkau skb_checksum_help(skb);
1509cde9450SFelix Fietkau
1519cde9450SFelix Fietkau nr_frags = skb_shinfo(skb)->nr_frags;
1529cde9450SFelix Fietkau
153b38c83ddSFelix Fietkau /* ring->end - ring->start will return the number of valid slots,
154b38c83ddSFelix Fietkau * even when ring->end overflows
155b38c83ddSFelix Fietkau */
156b38c83ddSFelix Fietkau if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
157d00a8281SJon Mason netdev_err(bgmac->net_dev, "TX ring is full, queue should be stopped!\n");
158dd4544f0SRafał Miłecki netif_stop_queue(net_dev);
159dd4544f0SRafał Miłecki return NETDEV_TX_BUSY;
160dd4544f0SRafał Miłecki }
161dd4544f0SRafał Miłecki
1629cde9450SFelix Fietkau slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
163dd4544f0SRafał Miłecki DMA_TO_DEVICE);
1649cde9450SFelix Fietkau if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
1659cde9450SFelix Fietkau goto err_dma_head;
1669cde9450SFelix Fietkau
1679cde9450SFelix Fietkau flags = BGMAC_DESC_CTL0_SOF;
1689cde9450SFelix Fietkau if (!nr_frags)
1699cde9450SFelix Fietkau flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
1709cde9450SFelix Fietkau
1719cde9450SFelix Fietkau bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
1729cde9450SFelix Fietkau flags = 0;
1739cde9450SFelix Fietkau
1749cde9450SFelix Fietkau for (i = 0; i < nr_frags; i++) {
175d7840976SMatthew Wilcox (Oracle) skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1769cde9450SFelix Fietkau int len = skb_frag_size(frag);
1779cde9450SFelix Fietkau
1789cde9450SFelix Fietkau index = (index + 1) % BGMAC_TX_RING_SLOTS;
1799cde9450SFelix Fietkau slot = &ring->slots[index];
1809cde9450SFelix Fietkau slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
1819cde9450SFelix Fietkau len, DMA_TO_DEVICE);
1829cde9450SFelix Fietkau if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
1839cde9450SFelix Fietkau goto err_dma;
1849cde9450SFelix Fietkau
1859cde9450SFelix Fietkau if (i == nr_frags - 1)
1869cde9450SFelix Fietkau flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
1879cde9450SFelix Fietkau
1889cde9450SFelix Fietkau bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
189dd4544f0SRafał Miłecki }
190dd4544f0SRafał Miłecki
1919cde9450SFelix Fietkau slot->skb = skb;
19249a467b4SHauke Mehrtens netdev_sent_queue(net_dev, skb->len);
1931b7680c6SSandor Bodo-Merle ring->end += nr_frags + 1;
19449a467b4SHauke Mehrtens
195dd4544f0SRafał Miłecki wmb();
196dd4544f0SRafał Miłecki
197dd4544f0SRafał Miłecki /* Increase ring->end to point empty slot. We tell hardware the first
198dd4544f0SRafał Miłecki * slot it should *not* read.
199dd4544f0SRafał Miłecki */
200dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
2019900303eSRafał Miłecki ring->index_base +
202b38c83ddSFelix Fietkau (ring->end % BGMAC_TX_RING_SLOTS) *
203b38c83ddSFelix Fietkau sizeof(struct bgmac_dma_desc));
204dd4544f0SRafał Miłecki
205b38c83ddSFelix Fietkau if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
206dd4544f0SRafał Miłecki netif_stop_queue(net_dev);
207dd4544f0SRafał Miłecki
208dd4544f0SRafał Miłecki return NETDEV_TX_OK;
209dd4544f0SRafał Miłecki
2109cde9450SFelix Fietkau err_dma:
2119cde9450SFelix Fietkau dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
2129cde9450SFelix Fietkau DMA_TO_DEVICE);
2139cde9450SFelix Fietkau
214e86663c4SFlorian Fainelli while (i-- > 0) {
2159cde9450SFelix Fietkau int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
2169cde9450SFelix Fietkau struct bgmac_slot_info *slot = &ring->slots[index];
2179cde9450SFelix Fietkau u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
2189cde9450SFelix Fietkau int len = ctl1 & BGMAC_DESC_CTL1_LEN;
2199cde9450SFelix Fietkau
2209cde9450SFelix Fietkau dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
2219cde9450SFelix Fietkau }
2229cde9450SFelix Fietkau
2239cde9450SFelix Fietkau err_dma_head:
224d00a8281SJon Mason netdev_err(bgmac->net_dev, "Mapping error of skb on ring 0x%X\n",
2259cde9450SFelix Fietkau ring->mmio_base);
2269cde9450SFelix Fietkau
2279cde9450SFelix Fietkau err_drop:
228dd4544f0SRafał Miłecki dev_kfree_skb(skb);
2296d490f62SFlorian Fainelli net_dev->stats.tx_dropped++;
2306d490f62SFlorian Fainelli net_dev->stats.tx_errors++;
231dd4544f0SRafał Miłecki return NETDEV_TX_OK;
232dd4544f0SRafał Miłecki }
233dd4544f0SRafał Miłecki
234dd4544f0SRafał Miłecki /* Free transmitted packets */
bgmac_dma_tx_free(struct bgmac * bgmac,struct bgmac_dma_ring * ring)235dd4544f0SRafał Miłecki static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
236dd4544f0SRafał Miłecki {
237a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
238dd4544f0SRafał Miłecki int empty_slot;
23949a467b4SHauke Mehrtens unsigned bytes_compl = 0, pkts_compl = 0;
240dd4544f0SRafał Miłecki
241dd4544f0SRafał Miłecki /* The last slot that hardware didn't consume yet */
242dd4544f0SRafał Miłecki empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
243dd4544f0SRafał Miłecki empty_slot &= BGMAC_DMA_TX_STATDPTR;
2449900303eSRafał Miłecki empty_slot -= ring->index_base;
2459900303eSRafał Miłecki empty_slot &= BGMAC_DMA_TX_STATDPTR;
246dd4544f0SRafał Miłecki empty_slot /= sizeof(struct bgmac_dma_desc);
247dd4544f0SRafał Miłecki
248b38c83ddSFelix Fietkau while (ring->start != ring->end) {
249b38c83ddSFelix Fietkau int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
250b38c83ddSFelix Fietkau struct bgmac_slot_info *slot = &ring->slots[slot_idx];
251d2b13233SFlorian Fainelli u32 ctl0, ctl1;
252b38c83ddSFelix Fietkau int len;
2539cde9450SFelix Fietkau
254b38c83ddSFelix Fietkau if (slot_idx == empty_slot)
255b38c83ddSFelix Fietkau break;
2569cde9450SFelix Fietkau
257d2b13233SFlorian Fainelli ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0);
258b38c83ddSFelix Fietkau ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
259b38c83ddSFelix Fietkau len = ctl1 & BGMAC_DESC_CTL1_LEN;
260d2b13233SFlorian Fainelli if (ctl0 & BGMAC_DESC_CTL0_SOF)
2619cde9450SFelix Fietkau /* Unmap no longer used buffer */
2629cde9450SFelix Fietkau dma_unmap_single(dma_dev, slot->dma_addr, len,
2639cde9450SFelix Fietkau DMA_TO_DEVICE);
2649cde9450SFelix Fietkau else
2659cde9450SFelix Fietkau dma_unmap_page(dma_dev, slot->dma_addr, len,
2669cde9450SFelix Fietkau DMA_TO_DEVICE);
267dd4544f0SRafał Miłecki
268dd4544f0SRafał Miłecki if (slot->skb) {
2696d490f62SFlorian Fainelli bgmac->net_dev->stats.tx_bytes += slot->skb->len;
2706d490f62SFlorian Fainelli bgmac->net_dev->stats.tx_packets++;
27149a467b4SHauke Mehrtens bytes_compl += slot->skb->len;
27249a467b4SHauke Mehrtens pkts_compl++;
27349a467b4SHauke Mehrtens
274dd4544f0SRafał Miłecki /* Free memory! :) */
275dd4544f0SRafał Miłecki dev_kfree_skb(slot->skb);
276dd4544f0SRafał Miłecki slot->skb = NULL;
277dd4544f0SRafał Miłecki }
278dd4544f0SRafał Miłecki
2799cde9450SFelix Fietkau slot->dma_addr = 0;
280b38c83ddSFelix Fietkau ring->start++;
281dd4544f0SRafał Miłecki }
282dd4544f0SRafał Miłecki
2839cde9450SFelix Fietkau if (!pkts_compl)
2849cde9450SFelix Fietkau return;
2859cde9450SFelix Fietkau
28649a467b4SHauke Mehrtens netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
28749a467b4SHauke Mehrtens
2889cde9450SFelix Fietkau if (netif_queue_stopped(bgmac->net_dev))
289dd4544f0SRafał Miłecki netif_wake_queue(bgmac->net_dev);
290dd4544f0SRafał Miłecki }
291dd4544f0SRafał Miłecki
bgmac_dma_rx_reset(struct bgmac * bgmac,struct bgmac_dma_ring * ring)292dd4544f0SRafał Miłecki static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
293dd4544f0SRafał Miłecki {
294dd4544f0SRafał Miłecki if (!ring->mmio_base)
295dd4544f0SRafał Miłecki return;
296dd4544f0SRafał Miłecki
297dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
298f6a95a24SJon Mason if (!bgmac_wait_value(bgmac,
299dd4544f0SRafał Miłecki ring->mmio_base + BGMAC_DMA_RX_STATUS,
300dd4544f0SRafał Miłecki BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
301dd4544f0SRafał Miłecki 10000))
302d00a8281SJon Mason dev_err(bgmac->dev, "Reset of ring 0x%X RX failed\n",
303dd4544f0SRafał Miłecki ring->mmio_base);
304dd4544f0SRafał Miłecki }
305dd4544f0SRafał Miłecki
bgmac_dma_rx_enable(struct bgmac * bgmac,struct bgmac_dma_ring * ring)306dd4544f0SRafał Miłecki static void bgmac_dma_rx_enable(struct bgmac *bgmac,
307dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring)
308dd4544f0SRafał Miłecki {
309dd4544f0SRafał Miłecki u32 ctl;
310dd4544f0SRafał Miłecki
311dd4544f0SRafał Miłecki ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
312fcdefccaSAndy Gospodarek
313fcdefccaSAndy Gospodarek /* preserve ONLY bits 16-17 from current hardware value */
314fcdefccaSAndy Gospodarek ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
315fcdefccaSAndy Gospodarek
316db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_RX_MASK_SETUP) {
31756ceecdeSHauke Mehrtens ctl &= ~BGMAC_DMA_RX_BL_MASK;
31856ceecdeSHauke Mehrtens ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
31956ceecdeSHauke Mehrtens
32056ceecdeSHauke Mehrtens ctl &= ~BGMAC_DMA_RX_PC_MASK;
32156ceecdeSHauke Mehrtens ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
32256ceecdeSHauke Mehrtens
32356ceecdeSHauke Mehrtens ctl &= ~BGMAC_DMA_RX_PT_MASK;
32456ceecdeSHauke Mehrtens ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
32556ceecdeSHauke Mehrtens }
326dd4544f0SRafał Miłecki ctl |= BGMAC_DMA_RX_ENABLE;
327dd4544f0SRafał Miłecki ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
328dd4544f0SRafał Miłecki ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
329dd4544f0SRafał Miłecki ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
330dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
331dd4544f0SRafał Miłecki }
332dd4544f0SRafał Miłecki
bgmac_dma_rx_skb_for_slot(struct bgmac * bgmac,struct bgmac_slot_info * slot)333dd4544f0SRafał Miłecki static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
334dd4544f0SRafał Miłecki struct bgmac_slot_info *slot)
335dd4544f0SRafał Miłecki {
336a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
337b757a62eSNathan Hintz dma_addr_t dma_addr;
338dd4544f0SRafał Miłecki struct bgmac_rx_header *rx;
33945c9b3c0SFelix Fietkau void *buf;
340dd4544f0SRafał Miłecki
341dd4544f0SRafał Miłecki /* Alloc skb */
34245c9b3c0SFelix Fietkau buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
34345c9b3c0SFelix Fietkau if (!buf)
344dd4544f0SRafał Miłecki return -ENOMEM;
345dd4544f0SRafał Miłecki
346dd4544f0SRafał Miłecki /* Poison - if everything goes fine, hardware will overwrite it */
3474b62dce4SFelix Fietkau rx = buf + BGMAC_RX_BUF_OFFSET;
348dd4544f0SRafał Miłecki rx->len = cpu_to_le16(0xdead);
349dd4544f0SRafał Miłecki rx->flags = cpu_to_le16(0xbeef);
350dd4544f0SRafał Miłecki
351dd4544f0SRafał Miłecki /* Map skb for the DMA */
3524b62dce4SFelix Fietkau dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
3534b62dce4SFelix Fietkau BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
354b757a62eSNathan Hintz if (dma_mapping_error(dma_dev, dma_addr)) {
355d00a8281SJon Mason netdev_err(bgmac->net_dev, "DMA mapping error\n");
35645c9b3c0SFelix Fietkau put_page(virt_to_head_page(buf));
357dd4544f0SRafał Miłecki return -ENOMEM;
358dd4544f0SRafał Miłecki }
359b757a62eSNathan Hintz
360b757a62eSNathan Hintz /* Update the slot */
36145c9b3c0SFelix Fietkau slot->buf = buf;
362b757a62eSNathan Hintz slot->dma_addr = dma_addr;
363b757a62eSNathan Hintz
364dd4544f0SRafał Miłecki return 0;
365dd4544f0SRafał Miłecki }
366dd4544f0SRafał Miłecki
bgmac_dma_rx_update_index(struct bgmac * bgmac,struct bgmac_dma_ring * ring)3674668ae1fSFelix Fietkau static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
3684668ae1fSFelix Fietkau struct bgmac_dma_ring *ring)
3694668ae1fSFelix Fietkau {
3704668ae1fSFelix Fietkau dma_wmb();
3714668ae1fSFelix Fietkau
3724668ae1fSFelix Fietkau bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
3734668ae1fSFelix Fietkau ring->index_base +
3744668ae1fSFelix Fietkau ring->end * sizeof(struct bgmac_dma_desc));
3754668ae1fSFelix Fietkau }
3764668ae1fSFelix Fietkau
bgmac_dma_rx_setup_desc(struct bgmac * bgmac,struct bgmac_dma_ring * ring,int desc_idx)377d549c76bSRafał Miłecki static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
378d549c76bSRafał Miłecki struct bgmac_dma_ring *ring, int desc_idx)
379d549c76bSRafał Miłecki {
380d549c76bSRafał Miłecki struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
381d549c76bSRafał Miłecki u32 ctl0 = 0, ctl1 = 0;
382d549c76bSRafał Miłecki
38329ba877eSFelix Fietkau if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
384d549c76bSRafał Miłecki ctl0 |= BGMAC_DESC_CTL0_EOT;
385d549c76bSRafał Miłecki ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
386d549c76bSRafał Miłecki /* Is there any BGMAC device that requires extension? */
387d549c76bSRafał Miłecki /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
388d549c76bSRafał Miłecki * B43_DMA64_DCTL1_ADDREXT_MASK;
389d549c76bSRafał Miłecki */
390d549c76bSRafał Miłecki
391d549c76bSRafał Miłecki dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
392d549c76bSRafał Miłecki dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
393d549c76bSRafał Miłecki dma_desc->ctl0 = cpu_to_le32(ctl0);
394d549c76bSRafał Miłecki dma_desc->ctl1 = cpu_to_le32(ctl1);
3954668ae1fSFelix Fietkau
3964668ae1fSFelix Fietkau ring->end = desc_idx;
397d549c76bSRafał Miłecki }
398d549c76bSRafał Miłecki
bgmac_dma_rx_poison_buf(struct device * dma_dev,struct bgmac_slot_info * slot)39956faacd0SFelix Fietkau static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
40056faacd0SFelix Fietkau struct bgmac_slot_info *slot)
40156faacd0SFelix Fietkau {
40256faacd0SFelix Fietkau struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
40356faacd0SFelix Fietkau
40456faacd0SFelix Fietkau dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
40556faacd0SFelix Fietkau DMA_FROM_DEVICE);
40656faacd0SFelix Fietkau rx->len = cpu_to_le16(0xdead);
40756faacd0SFelix Fietkau rx->flags = cpu_to_le16(0xbeef);
40856faacd0SFelix Fietkau dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
40956faacd0SFelix Fietkau DMA_FROM_DEVICE);
41056faacd0SFelix Fietkau }
41156faacd0SFelix Fietkau
bgmac_dma_rx_read(struct bgmac * bgmac,struct bgmac_dma_ring * ring,int weight)412dd4544f0SRafał Miłecki static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
413dd4544f0SRafał Miłecki int weight)
414dd4544f0SRafał Miłecki {
415dd4544f0SRafał Miłecki u32 end_slot;
416dd4544f0SRafał Miłecki int handled = 0;
417dd4544f0SRafał Miłecki
418dd4544f0SRafał Miłecki end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
419dd4544f0SRafał Miłecki end_slot &= BGMAC_DMA_RX_STATDPTR;
4209900303eSRafał Miłecki end_slot -= ring->index_base;
4219900303eSRafał Miłecki end_slot &= BGMAC_DMA_RX_STATDPTR;
422dd4544f0SRafał Miłecki end_slot /= sizeof(struct bgmac_dma_desc);
423dd4544f0SRafał Miłecki
4244668ae1fSFelix Fietkau while (ring->start != end_slot) {
425a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
426dd4544f0SRafał Miłecki struct bgmac_slot_info *slot = &ring->slots[ring->start];
4274b62dce4SFelix Fietkau struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
42845c9b3c0SFelix Fietkau struct sk_buff *skb;
42945c9b3c0SFelix Fietkau void *buf = slot->buf;
43056faacd0SFelix Fietkau dma_addr_t dma_addr = slot->dma_addr;
431dd4544f0SRafał Miłecki u16 len, flags;
432dd4544f0SRafał Miłecki
43356faacd0SFelix Fietkau do {
43456faacd0SFelix Fietkau /* Prepare new skb as replacement */
43556faacd0SFelix Fietkau if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
43656faacd0SFelix Fietkau bgmac_dma_rx_poison_buf(dma_dev, slot);
43756faacd0SFelix Fietkau break;
43856faacd0SFelix Fietkau }
43956faacd0SFelix Fietkau
440dd4544f0SRafał Miłecki /* Unmap buffer to make it accessible to the CPU */
44156faacd0SFelix Fietkau dma_unmap_single(dma_dev, dma_addr,
442dd4544f0SRafał Miłecki BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
443dd4544f0SRafał Miłecki
444dd4544f0SRafał Miłecki /* Get info from the header */
445dd4544f0SRafał Miłecki len = le16_to_cpu(rx->len);
446dd4544f0SRafał Miłecki flags = le16_to_cpu(rx->flags);
447dd4544f0SRafał Miłecki
448dd4544f0SRafał Miłecki /* Check for poison and drop or pass the packet */
449dd4544f0SRafał Miłecki if (len == 0xdead && flags == 0xbeef) {
450d00a8281SJon Mason netdev_err(bgmac->net_dev, "Found poisoned packet at slot %d, DMA issue!\n",
451dd4544f0SRafał Miłecki ring->start);
45256faacd0SFelix Fietkau put_page(virt_to_head_page(buf));
4536d490f62SFlorian Fainelli bgmac->net_dev->stats.rx_errors++;
45492b9ccd3SRafał Miłecki break;
45592b9ccd3SRafał Miłecki }
45692b9ccd3SRafał Miłecki
4576a6c7084SFelix Fietkau if (len > BGMAC_RX_ALLOC_SIZE) {
458d00a8281SJon Mason netdev_err(bgmac->net_dev, "Found oversized packet at slot %d, DMA issue!\n",
4596a6c7084SFelix Fietkau ring->start);
4606a6c7084SFelix Fietkau put_page(virt_to_head_page(buf));
4616d490f62SFlorian Fainelli bgmac->net_dev->stats.rx_length_errors++;
4626d490f62SFlorian Fainelli bgmac->net_dev->stats.rx_errors++;
4636a6c7084SFelix Fietkau break;
4646a6c7084SFelix Fietkau }
4656a6c7084SFelix Fietkau
46602e71127SHauke Mehrtens /* Omit CRC. */
46702e71127SHauke Mehrtens len -= ETH_FCS_LEN;
46802e71127SHauke Mehrtens
46945c9b3c0SFelix Fietkau skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
470750afbf8SDavid S. Miller if (unlikely(!skb)) {
471d00a8281SJon Mason netdev_err(bgmac->net_dev, "build_skb failed\n");
472f1640c3dSwangweidong put_page(virt_to_head_page(buf));
4736d490f62SFlorian Fainelli bgmac->net_dev->stats.rx_errors++;
474f1640c3dSwangweidong break;
475f1640c3dSwangweidong }
4764b62dce4SFelix Fietkau skb_put(skb, BGMAC_RX_FRAME_OFFSET +
4774b62dce4SFelix Fietkau BGMAC_RX_BUF_OFFSET + len);
4784b62dce4SFelix Fietkau skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
4794b62dce4SFelix Fietkau BGMAC_RX_BUF_OFFSET);
48092b9ccd3SRafał Miłecki
48192b9ccd3SRafał Miłecki skb_checksum_none_assert(skb);
48292b9ccd3SRafał Miłecki skb->protocol = eth_type_trans(skb, bgmac->net_dev);
4836d490f62SFlorian Fainelli bgmac->net_dev->stats.rx_bytes += len;
4846d490f62SFlorian Fainelli bgmac->net_dev->stats.rx_packets++;
48545c9b3c0SFelix Fietkau napi_gro_receive(&bgmac->napi, skb);
48692b9ccd3SRafał Miłecki handled++;
48792b9ccd3SRafał Miłecki } while (0);
48892b9ccd3SRafał Miłecki
48956faacd0SFelix Fietkau bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
49056faacd0SFelix Fietkau
491dd4544f0SRafał Miłecki if (++ring->start >= BGMAC_RX_RING_SLOTS)
492dd4544f0SRafał Miłecki ring->start = 0;
493dd4544f0SRafał Miłecki
494dd4544f0SRafał Miłecki if (handled >= weight) /* Should never be greater */
495dd4544f0SRafał Miłecki break;
496dd4544f0SRafał Miłecki }
497dd4544f0SRafał Miłecki
4984668ae1fSFelix Fietkau bgmac_dma_rx_update_index(bgmac, ring);
4994668ae1fSFelix Fietkau
500dd4544f0SRafał Miłecki return handled;
501dd4544f0SRafał Miłecki }
502dd4544f0SRafał Miłecki
503dd4544f0SRafał Miłecki /* Does ring support unaligned addressing? */
bgmac_dma_unaligned(struct bgmac * bgmac,struct bgmac_dma_ring * ring,enum bgmac_dma_ring_type ring_type)504dd4544f0SRafał Miłecki static bool bgmac_dma_unaligned(struct bgmac *bgmac,
505dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring,
506dd4544f0SRafał Miłecki enum bgmac_dma_ring_type ring_type)
507dd4544f0SRafał Miłecki {
508dd4544f0SRafał Miłecki switch (ring_type) {
509dd4544f0SRafał Miłecki case BGMAC_DMA_RING_TX:
510dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
511dd4544f0SRafał Miłecki 0xff0);
512dd4544f0SRafał Miłecki if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
513dd4544f0SRafał Miłecki return true;
514dd4544f0SRafał Miłecki break;
515dd4544f0SRafał Miłecki case BGMAC_DMA_RING_RX:
516dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
517dd4544f0SRafał Miłecki 0xff0);
518dd4544f0SRafał Miłecki if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
519dd4544f0SRafał Miłecki return true;
520dd4544f0SRafał Miłecki break;
521dd4544f0SRafał Miłecki }
522dd4544f0SRafał Miłecki return false;
523dd4544f0SRafał Miłecki }
524dd4544f0SRafał Miłecki
bgmac_dma_tx_ring_free(struct bgmac * bgmac,struct bgmac_dma_ring * ring)52545c9b3c0SFelix Fietkau static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
526dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring)
527dd4544f0SRafał Miłecki {
528a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
5299cde9450SFelix Fietkau struct bgmac_dma_desc *dma_desc = ring->cpu_base;
530dd4544f0SRafał Miłecki struct bgmac_slot_info *slot;
531dd4544f0SRafał Miłecki int i;
532dd4544f0SRafał Miłecki
53329ba877eSFelix Fietkau for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
53460d6e6f0SFlorian Fainelli u32 ctl1 = le32_to_cpu(dma_desc[i].ctl1);
53560d6e6f0SFlorian Fainelli unsigned int len = ctl1 & BGMAC_DESC_CTL1_LEN;
5369cde9450SFelix Fietkau
537dd4544f0SRafał Miłecki slot = &ring->slots[i];
538dd4544f0SRafał Miłecki dev_kfree_skb(slot->skb);
5399cde9450SFelix Fietkau
5409cde9450SFelix Fietkau if (!slot->dma_addr)
5419cde9450SFelix Fietkau continue;
5429cde9450SFelix Fietkau
5439cde9450SFelix Fietkau if (slot->skb)
5449cde9450SFelix Fietkau dma_unmap_single(dma_dev, slot->dma_addr,
5459cde9450SFelix Fietkau len, DMA_TO_DEVICE);
5469cde9450SFelix Fietkau else
5479cde9450SFelix Fietkau dma_unmap_page(dma_dev, slot->dma_addr,
5489cde9450SFelix Fietkau len, DMA_TO_DEVICE);
549dd4544f0SRafał Miłecki }
55045c9b3c0SFelix Fietkau }
551dd4544f0SRafał Miłecki
bgmac_dma_rx_ring_free(struct bgmac * bgmac,struct bgmac_dma_ring * ring)55245c9b3c0SFelix Fietkau static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
55345c9b3c0SFelix Fietkau struct bgmac_dma_ring *ring)
55445c9b3c0SFelix Fietkau {
555a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
55645c9b3c0SFelix Fietkau struct bgmac_slot_info *slot;
55745c9b3c0SFelix Fietkau int i;
55845c9b3c0SFelix Fietkau
55929ba877eSFelix Fietkau for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
56045c9b3c0SFelix Fietkau slot = &ring->slots[i];
56156faacd0SFelix Fietkau if (!slot->dma_addr)
56245c9b3c0SFelix Fietkau continue;
56345c9b3c0SFelix Fietkau
56445c9b3c0SFelix Fietkau dma_unmap_single(dma_dev, slot->dma_addr,
56545c9b3c0SFelix Fietkau BGMAC_RX_BUF_SIZE,
56645c9b3c0SFelix Fietkau DMA_FROM_DEVICE);
56745c9b3c0SFelix Fietkau put_page(virt_to_head_page(slot->buf));
56856faacd0SFelix Fietkau slot->dma_addr = 0;
56945c9b3c0SFelix Fietkau }
57045c9b3c0SFelix Fietkau }
57145c9b3c0SFelix Fietkau
bgmac_dma_ring_desc_free(struct bgmac * bgmac,struct bgmac_dma_ring * ring,int num_slots)57245c9b3c0SFelix Fietkau static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
57329ba877eSFelix Fietkau struct bgmac_dma_ring *ring,
57429ba877eSFelix Fietkau int num_slots)
57545c9b3c0SFelix Fietkau {
576a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
57745c9b3c0SFelix Fietkau int size;
57845c9b3c0SFelix Fietkau
57945c9b3c0SFelix Fietkau if (!ring->cpu_base)
58045c9b3c0SFelix Fietkau return;
58145c9b3c0SFelix Fietkau
582dd4544f0SRafał Miłecki /* Free ring of descriptors */
58329ba877eSFelix Fietkau size = num_slots * sizeof(struct bgmac_dma_desc);
584dd4544f0SRafał Miłecki dma_free_coherent(dma_dev, size, ring->cpu_base,
585dd4544f0SRafał Miłecki ring->dma_base);
586dd4544f0SRafał Miłecki }
587dd4544f0SRafał Miłecki
bgmac_dma_cleanup(struct bgmac * bgmac)58874b6f291SFelix Fietkau static void bgmac_dma_cleanup(struct bgmac *bgmac)
58974b6f291SFelix Fietkau {
59074b6f291SFelix Fietkau int i;
59174b6f291SFelix Fietkau
59274b6f291SFelix Fietkau for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
59374b6f291SFelix Fietkau bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
59474b6f291SFelix Fietkau
59574b6f291SFelix Fietkau for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
59674b6f291SFelix Fietkau bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
59774b6f291SFelix Fietkau }
59874b6f291SFelix Fietkau
bgmac_dma_free(struct bgmac * bgmac)599dd4544f0SRafał Miłecki static void bgmac_dma_free(struct bgmac *bgmac)
600dd4544f0SRafał Miłecki {
601dd4544f0SRafał Miłecki int i;
602dd4544f0SRafał Miłecki
60374b6f291SFelix Fietkau for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
60429ba877eSFelix Fietkau bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
60529ba877eSFelix Fietkau BGMAC_TX_RING_SLOTS);
60674b6f291SFelix Fietkau
60774b6f291SFelix Fietkau for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
60829ba877eSFelix Fietkau bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
60929ba877eSFelix Fietkau BGMAC_RX_RING_SLOTS);
61045c9b3c0SFelix Fietkau }
611dd4544f0SRafał Miłecki
bgmac_dma_alloc(struct bgmac * bgmac)612dd4544f0SRafał Miłecki static int bgmac_dma_alloc(struct bgmac *bgmac)
613dd4544f0SRafał Miłecki {
614a0b68486SJon Mason struct device *dma_dev = bgmac->dma_dev;
615dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring;
616dd4544f0SRafał Miłecki static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
617dd4544f0SRafał Miłecki BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
618dd4544f0SRafał Miłecki int size; /* ring size: different for Tx and Rx */
619dd4544f0SRafał Miłecki int i;
620dd4544f0SRafał Miłecki
621dd4544f0SRafał Miłecki BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
622dd4544f0SRafał Miłecki BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
623dd4544f0SRafał Miłecki
624a163bdb0SAbhishek Shah if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
625f6a95a24SJon Mason if (!(bgmac_idm_read(bgmac, BCMA_IOST) & BCMA_IOST_DMA64)) {
626d00a8281SJon Mason dev_err(bgmac->dev, "Core does not report 64-bit DMA\n");
627dd4544f0SRafał Miłecki return -ENOTSUPP;
628dd4544f0SRafał Miłecki }
629a163bdb0SAbhishek Shah }
630dd4544f0SRafał Miłecki
631dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
632dd4544f0SRafał Miłecki ring = &bgmac->tx_ring[i];
633dd4544f0SRafał Miłecki ring->mmio_base = ring_base[i];
634dd4544f0SRafał Miłecki
635dd4544f0SRafał Miłecki /* Alloc ring of descriptors */
63629ba877eSFelix Fietkau size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
637750afb08SLuis Chamberlain ring->cpu_base = dma_alloc_coherent(dma_dev, size,
638dd4544f0SRafał Miłecki &ring->dma_base,
639dd4544f0SRafał Miłecki GFP_KERNEL);
640dd4544f0SRafał Miłecki if (!ring->cpu_base) {
641d00a8281SJon Mason dev_err(bgmac->dev, "Allocation of TX ring 0x%X failed\n",
642dd4544f0SRafał Miłecki ring->mmio_base);
643dd4544f0SRafał Miłecki goto err_dma_free;
644dd4544f0SRafał Miłecki }
645dd4544f0SRafał Miłecki
6469900303eSRafał Miłecki ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
6479900303eSRafał Miłecki BGMAC_DMA_RING_TX);
6489900303eSRafał Miłecki if (ring->unaligned)
6499900303eSRafał Miłecki ring->index_base = lower_32_bits(ring->dma_base);
6509900303eSRafał Miłecki else
6519900303eSRafał Miłecki ring->index_base = 0;
6529900303eSRafał Miłecki
653dd4544f0SRafał Miłecki /* No need to alloc TX slots yet */
654dd4544f0SRafał Miłecki }
655dd4544f0SRafał Miłecki
656dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
657dd4544f0SRafał Miłecki ring = &bgmac->rx_ring[i];
658dd4544f0SRafał Miłecki ring->mmio_base = ring_base[i];
659dd4544f0SRafał Miłecki
660dd4544f0SRafał Miłecki /* Alloc ring of descriptors */
66129ba877eSFelix Fietkau size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
662750afb08SLuis Chamberlain ring->cpu_base = dma_alloc_coherent(dma_dev, size,
663dd4544f0SRafał Miłecki &ring->dma_base,
664dd4544f0SRafał Miłecki GFP_KERNEL);
665dd4544f0SRafał Miłecki if (!ring->cpu_base) {
666d00a8281SJon Mason dev_err(bgmac->dev, "Allocation of RX ring 0x%X failed\n",
667dd4544f0SRafał Miłecki ring->mmio_base);
668dd4544f0SRafał Miłecki goto err_dma_free;
669dd4544f0SRafał Miłecki }
670dd4544f0SRafał Miłecki
6719900303eSRafał Miłecki ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
6729900303eSRafał Miłecki BGMAC_DMA_RING_RX);
6739900303eSRafał Miłecki if (ring->unaligned)
6749900303eSRafał Miłecki ring->index_base = lower_32_bits(ring->dma_base);
6759900303eSRafał Miłecki else
6769900303eSRafał Miłecki ring->index_base = 0;
677dd4544f0SRafał Miłecki }
678dd4544f0SRafał Miłecki
679dd4544f0SRafał Miłecki return 0;
680dd4544f0SRafał Miłecki
681dd4544f0SRafał Miłecki err_dma_free:
682dd4544f0SRafał Miłecki bgmac_dma_free(bgmac);
683dd4544f0SRafał Miłecki return -ENOMEM;
684dd4544f0SRafał Miłecki }
685dd4544f0SRafał Miłecki
bgmac_dma_init(struct bgmac * bgmac)68674b6f291SFelix Fietkau static int bgmac_dma_init(struct bgmac *bgmac)
687dd4544f0SRafał Miłecki {
688dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring;
68974b6f291SFelix Fietkau int i, err;
690dd4544f0SRafał Miłecki
691dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
692dd4544f0SRafał Miłecki ring = &bgmac->tx_ring[i];
693dd4544f0SRafał Miłecki
6949900303eSRafał Miłecki if (!ring->unaligned)
695dd4544f0SRafał Miłecki bgmac_dma_tx_enable(bgmac, ring);
696dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
697dd4544f0SRafał Miłecki lower_32_bits(ring->dma_base));
698dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
699dd4544f0SRafał Miłecki upper_32_bits(ring->dma_base));
7009900303eSRafał Miłecki if (ring->unaligned)
7019900303eSRafał Miłecki bgmac_dma_tx_enable(bgmac, ring);
702dd4544f0SRafał Miłecki
703dd4544f0SRafał Miłecki ring->start = 0;
704dd4544f0SRafał Miłecki ring->end = 0; /* Points the slot that should *not* be read */
705dd4544f0SRafał Miłecki }
706dd4544f0SRafał Miłecki
707dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
70870a737b7SRafał Miłecki int j;
70970a737b7SRafał Miłecki
710dd4544f0SRafał Miłecki ring = &bgmac->rx_ring[i];
711dd4544f0SRafał Miłecki
7129900303eSRafał Miłecki if (!ring->unaligned)
713dd4544f0SRafał Miłecki bgmac_dma_rx_enable(bgmac, ring);
714dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
715dd4544f0SRafał Miłecki lower_32_bits(ring->dma_base));
716dd4544f0SRafał Miłecki bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
717dd4544f0SRafał Miłecki upper_32_bits(ring->dma_base));
7189900303eSRafał Miłecki if (ring->unaligned)
7199900303eSRafał Miłecki bgmac_dma_rx_enable(bgmac, ring);
720dd4544f0SRafał Miłecki
7214668ae1fSFelix Fietkau ring->start = 0;
7224668ae1fSFelix Fietkau ring->end = 0;
72329ba877eSFelix Fietkau for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
72474b6f291SFelix Fietkau err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
72574b6f291SFelix Fietkau if (err)
72674b6f291SFelix Fietkau goto error;
72774b6f291SFelix Fietkau
728d549c76bSRafał Miłecki bgmac_dma_rx_setup_desc(bgmac, ring, j);
72974b6f291SFelix Fietkau }
730dd4544f0SRafał Miłecki
7314668ae1fSFelix Fietkau bgmac_dma_rx_update_index(bgmac, ring);
732dd4544f0SRafał Miłecki }
73374b6f291SFelix Fietkau
73474b6f291SFelix Fietkau return 0;
73574b6f291SFelix Fietkau
73674b6f291SFelix Fietkau error:
73774b6f291SFelix Fietkau bgmac_dma_cleanup(bgmac);
73874b6f291SFelix Fietkau return err;
739dd4544f0SRafał Miłecki }
740dd4544f0SRafał Miłecki
741dd4544f0SRafał Miłecki
742dd4544f0SRafał Miłecki /**************************************************
743dd4544f0SRafał Miłecki * Chip ops
744dd4544f0SRafał Miłecki **************************************************/
745dd4544f0SRafał Miłecki
746dd4544f0SRafał Miłecki /* TODO: can we just drop @force? Can we don't reset MAC at all if there is
747dd4544f0SRafał Miłecki * nothing to change? Try if after stabilizng driver.
748dd4544f0SRafał Miłecki */
bgmac_umac_cmd_maskset(struct bgmac * bgmac,u32 mask,u32 set,bool force)74912cf8e75SRafał Miłecki static void bgmac_umac_cmd_maskset(struct bgmac *bgmac, u32 mask, u32 set,
750dd4544f0SRafał Miłecki bool force)
751dd4544f0SRafał Miłecki {
75228e303daSRafał Miłecki u32 cmdcfg = bgmac_umac_read(bgmac, UMAC_CMD);
753dd4544f0SRafał Miłecki u32 new_val = (cmdcfg & mask) | set;
754db791eb2SJon Mason u32 cmdcfg_sr;
755dd4544f0SRafał Miłecki
756db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
75728e303daSRafał Miłecki cmdcfg_sr = CMD_SW_RESET;
758db791eb2SJon Mason else
75928e303daSRafał Miłecki cmdcfg_sr = CMD_SW_RESET_OLD;
760db791eb2SJon Mason
76128e303daSRafał Miłecki bgmac_umac_maskset(bgmac, UMAC_CMD, ~0, cmdcfg_sr);
762dd4544f0SRafał Miłecki udelay(2);
763dd4544f0SRafał Miłecki
764dd4544f0SRafał Miłecki if (new_val != cmdcfg || force)
76528e303daSRafał Miłecki bgmac_umac_write(bgmac, UMAC_CMD, new_val);
766dd4544f0SRafał Miłecki
76728e303daSRafał Miłecki bgmac_umac_maskset(bgmac, UMAC_CMD, ~cmdcfg_sr, 0);
768dd4544f0SRafał Miłecki udelay(2);
769dd4544f0SRafał Miłecki }
770dd4544f0SRafał Miłecki
bgmac_write_mac_address(struct bgmac * bgmac,const u8 * addr)77176660757SJakub Kicinski static void bgmac_write_mac_address(struct bgmac *bgmac, const u8 *addr)
7724e209001SHauke Mehrtens {
7734e209001SHauke Mehrtens u32 tmp;
7744e209001SHauke Mehrtens
7754e209001SHauke Mehrtens tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
77628e303daSRafał Miłecki bgmac_umac_write(bgmac, UMAC_MAC0, tmp);
7774e209001SHauke Mehrtens tmp = (addr[4] << 8) | addr[5];
77828e303daSRafał Miłecki bgmac_umac_write(bgmac, UMAC_MAC1, tmp);
7794e209001SHauke Mehrtens }
7804e209001SHauke Mehrtens
bgmac_set_rx_mode(struct net_device * net_dev)781c6edfe10SHauke Mehrtens static void bgmac_set_rx_mode(struct net_device *net_dev)
782c6edfe10SHauke Mehrtens {
783c6edfe10SHauke Mehrtens struct bgmac *bgmac = netdev_priv(net_dev);
784c6edfe10SHauke Mehrtens
785c6edfe10SHauke Mehrtens if (net_dev->flags & IFF_PROMISC)
78628e303daSRafał Miłecki bgmac_umac_cmd_maskset(bgmac, ~0, CMD_PROMISC, true);
787c6edfe10SHauke Mehrtens else
78828e303daSRafał Miłecki bgmac_umac_cmd_maskset(bgmac, ~CMD_PROMISC, 0, true);
789c6edfe10SHauke Mehrtens }
790c6edfe10SHauke Mehrtens
791dd4544f0SRafał Miłecki #if 0 /* We don't use that regs yet */
792dd4544f0SRafał Miłecki static void bgmac_chip_stats_update(struct bgmac *bgmac)
793dd4544f0SRafał Miłecki {
794dd4544f0SRafał Miłecki int i;
795dd4544f0SRafał Miłecki
796db791eb2SJon Mason if (!(bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)) {
797dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
798dd4544f0SRafał Miłecki bgmac->mib_tx_regs[i] =
799dd4544f0SRafał Miłecki bgmac_read(bgmac,
800dd4544f0SRafał Miłecki BGMAC_TX_GOOD_OCTETS + (i * 4));
801dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
802dd4544f0SRafał Miłecki bgmac->mib_rx_regs[i] =
803dd4544f0SRafał Miłecki bgmac_read(bgmac,
804dd4544f0SRafał Miłecki BGMAC_RX_GOOD_OCTETS + (i * 4));
805dd4544f0SRafał Miłecki }
806dd4544f0SRafał Miłecki
807dd4544f0SRafał Miłecki /* TODO: what else? how to handle BCM4706? Specs are needed */
808dd4544f0SRafał Miłecki }
809dd4544f0SRafał Miłecki #endif
810dd4544f0SRafał Miłecki
bgmac_clear_mib(struct bgmac * bgmac)811dd4544f0SRafał Miłecki static void bgmac_clear_mib(struct bgmac *bgmac)
812dd4544f0SRafał Miłecki {
813dd4544f0SRafał Miłecki int i;
814dd4544f0SRafał Miłecki
815db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)
816dd4544f0SRafał Miłecki return;
817dd4544f0SRafał Miłecki
818dd4544f0SRafał Miłecki bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
819dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
820dd4544f0SRafał Miłecki bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
821dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
822dd4544f0SRafał Miłecki bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
823dd4544f0SRafał Miłecki }
824dd4544f0SRafał Miłecki
825dd4544f0SRafał Miłecki /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
bgmac_mac_speed(struct bgmac * bgmac)8265824d2d1SRafał Miłecki static void bgmac_mac_speed(struct bgmac *bgmac)
827dd4544f0SRafał Miłecki {
82828e303daSRafał Miłecki u32 mask = ~(CMD_SPEED_MASK << CMD_SPEED_SHIFT | CMD_HD_EN);
829dd4544f0SRafał Miłecki u32 set = 0;
830dd4544f0SRafał Miłecki
8315824d2d1SRafał Miłecki switch (bgmac->mac_speed) {
8325824d2d1SRafał Miłecki case SPEED_10:
83328e303daSRafał Miłecki set |= CMD_SPEED_10 << CMD_SPEED_SHIFT;
8345824d2d1SRafał Miłecki break;
8355824d2d1SRafał Miłecki case SPEED_100:
83628e303daSRafał Miłecki set |= CMD_SPEED_100 << CMD_SPEED_SHIFT;
8375824d2d1SRafał Miłecki break;
8385824d2d1SRafał Miłecki case SPEED_1000:
83928e303daSRafał Miłecki set |= CMD_SPEED_1000 << CMD_SPEED_SHIFT;
8405824d2d1SRafał Miłecki break;
8416df4aff9SHauke Mehrtens case SPEED_2500:
84228e303daSRafał Miłecki set |= CMD_SPEED_2500 << CMD_SPEED_SHIFT;
8436df4aff9SHauke Mehrtens break;
8445824d2d1SRafał Miłecki default:
845d00a8281SJon Mason dev_err(bgmac->dev, "Unsupported speed: %d\n",
846d00a8281SJon Mason bgmac->mac_speed);
8475824d2d1SRafał Miłecki }
8485824d2d1SRafał Miłecki
8495824d2d1SRafał Miłecki if (bgmac->mac_duplex == DUPLEX_HALF)
85028e303daSRafał Miłecki set |= CMD_HD_EN;
8515824d2d1SRafał Miłecki
85212cf8e75SRafał Miłecki bgmac_umac_cmd_maskset(bgmac, mask, set, true);
853dd4544f0SRafał Miłecki }
854dd4544f0SRafał Miłecki
bgmac_miiconfig(struct bgmac * bgmac)855dd4544f0SRafał Miłecki static void bgmac_miiconfig(struct bgmac *bgmac)
856dd4544f0SRafał Miłecki {
857db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_FORCE_SPEED_2500) {
858a163bdb0SAbhishek Shah if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
859f6a95a24SJon Mason bgmac_idm_write(bgmac, BCMA_IOCTL,
860a163bdb0SAbhishek Shah bgmac_idm_read(bgmac, BCMA_IOCTL) |
861a163bdb0SAbhishek Shah 0x40 | BGMAC_BCMA_IOCTL_SW_CLKEN);
862a163bdb0SAbhishek Shah }
8636df4aff9SHauke Mehrtens bgmac->mac_speed = SPEED_2500;
8646df4aff9SHauke Mehrtens bgmac->mac_duplex = DUPLEX_FULL;
8656df4aff9SHauke Mehrtens bgmac_mac_speed(bgmac);
8666df4aff9SHauke Mehrtens } else {
867db791eb2SJon Mason u8 imode;
868db791eb2SJon Mason
8696df4aff9SHauke Mehrtens imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
8706df4aff9SHauke Mehrtens BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
871dd4544f0SRafał Miłecki if (imode == 0 || imode == 1) {
8725824d2d1SRafał Miłecki bgmac->mac_speed = SPEED_100;
8735824d2d1SRafał Miłecki bgmac->mac_duplex = DUPLEX_FULL;
8745824d2d1SRafał Miłecki bgmac_mac_speed(bgmac);
875dd4544f0SRafał Miłecki }
876dd4544f0SRafał Miłecki }
8776df4aff9SHauke Mehrtens }
878dd4544f0SRafał Miłecki
bgmac_chip_reset_idm_config(struct bgmac * bgmac)879a163bdb0SAbhishek Shah static void bgmac_chip_reset_idm_config(struct bgmac *bgmac)
880a163bdb0SAbhishek Shah {
881a163bdb0SAbhishek Shah u32 iost;
882a163bdb0SAbhishek Shah
883a163bdb0SAbhishek Shah iost = bgmac_idm_read(bgmac, BCMA_IOST);
884a163bdb0SAbhishek Shah if (bgmac->feature_flags & BGMAC_FEAT_IOST_ATTACHED)
885a163bdb0SAbhishek Shah iost &= ~BGMAC_BCMA_IOST_ATTACHED;
886a163bdb0SAbhishek Shah
887a163bdb0SAbhishek Shah /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
888a163bdb0SAbhishek Shah if (!(bgmac->feature_flags & BGMAC_FEAT_NO_RESET)) {
889a163bdb0SAbhishek Shah u32 flags = 0;
890a163bdb0SAbhishek Shah
891a163bdb0SAbhishek Shah if (iost & BGMAC_BCMA_IOST_ATTACHED) {
892a163bdb0SAbhishek Shah flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
893f99e6d7cSRafał Miłecki if (bgmac->in_init || !bgmac->has_robosw)
894a163bdb0SAbhishek Shah flags |= BGMAC_BCMA_IOCTL_SW_RESET;
895a163bdb0SAbhishek Shah }
896a163bdb0SAbhishek Shah bgmac_clk_enable(bgmac, flags);
897a163bdb0SAbhishek Shah }
898a163bdb0SAbhishek Shah
899f99e6d7cSRafał Miłecki if (iost & BGMAC_BCMA_IOST_ATTACHED && (bgmac->in_init || !bgmac->has_robosw))
900a163bdb0SAbhishek Shah bgmac_idm_write(bgmac, BCMA_IOCTL,
901a163bdb0SAbhishek Shah bgmac_idm_read(bgmac, BCMA_IOCTL) &
902a163bdb0SAbhishek Shah ~BGMAC_BCMA_IOCTL_SW_RESET);
903a163bdb0SAbhishek Shah }
904a163bdb0SAbhishek Shah
905dd4544f0SRafał Miłecki /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
bgmac_chip_reset(struct bgmac * bgmac)906dd4544f0SRafał Miłecki static void bgmac_chip_reset(struct bgmac *bgmac)
907dd4544f0SRafał Miłecki {
908db791eb2SJon Mason u32 cmdcfg_sr;
909dd4544f0SRafał Miłecki int i;
910dd4544f0SRafał Miłecki
911f6a95a24SJon Mason if (bgmac_clk_enabled(bgmac)) {
912dd4544f0SRafał Miłecki if (!bgmac->stats_grabbed) {
913dd4544f0SRafał Miłecki /* bgmac_chip_stats_update(bgmac); */
914dd4544f0SRafał Miłecki bgmac->stats_grabbed = true;
915dd4544f0SRafał Miłecki }
916dd4544f0SRafał Miłecki
917dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
918dd4544f0SRafał Miłecki bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
919dd4544f0SRafał Miłecki
92028e303daSRafał Miłecki bgmac_umac_cmd_maskset(bgmac, ~0, CMD_LCL_LOOP_EN, false);
921dd4544f0SRafał Miłecki udelay(1);
922dd4544f0SRafał Miłecki
923dd4544f0SRafał Miłecki for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
924dd4544f0SRafał Miłecki bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
925dd4544f0SRafał Miłecki
926dd4544f0SRafał Miłecki /* TODO: Clear software multicast filter list */
927dd4544f0SRafał Miłecki }
928dd4544f0SRafał Miłecki
929a163bdb0SAbhishek Shah if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK))
930a163bdb0SAbhishek Shah bgmac_chip_reset_idm_config(bgmac);
931dd4544f0SRafał Miłecki
9326df4aff9SHauke Mehrtens /* Request Misc PLL for corerev > 2 */
933db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_MISC_PLL_REQ) {
9341a0ab767SRafał Miłecki bgmac_set(bgmac, BCMA_CLKCTLST,
9351a0ab767SRafał Miłecki BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
936f6a95a24SJon Mason bgmac_wait_value(bgmac, BCMA_CLKCTLST,
9371a0ab767SRafał Miłecki BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
9381a0ab767SRafał Miłecki BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
939dd4544f0SRafał Miłecki 1000);
940dd4544f0SRafał Miłecki }
941dd4544f0SRafał Miłecki
942db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_PHY) {
943dd4544f0SRafał Miłecki u8 et_swtype = 0;
944dd4544f0SRafał Miłecki u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
9456a391e7bSRafał Miłecki BGMAC_CHIPCTL_1_IF_TYPE_MII;
9463647268dSHauke Mehrtens char buf[4];
947dd4544f0SRafał Miłecki
9483647268dSHauke Mehrtens if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
949dd4544f0SRafał Miłecki if (kstrtou8(buf, 0, &et_swtype))
950d00a8281SJon Mason dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
951dd4544f0SRafał Miłecki buf);
952dd4544f0SRafał Miłecki et_swtype &= 0x0f;
953dd4544f0SRafał Miłecki et_swtype <<= 4;
954dd4544f0SRafał Miłecki sw_type = et_swtype;
955db791eb2SJon Mason } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_EPHYRMII) {
956e2d8f646SRafał Miłecki sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RMII |
957e2d8f646SRafał Miłecki BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
958db791eb2SJon Mason } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_RGMII) {
959b5a4c2f3SHauke Mehrtens sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
960b5a4c2f3SHauke Mehrtens BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
961dd4544f0SRafał Miłecki }
962f6a95a24SJon Mason bgmac_cco_ctl_maskset(bgmac, 1, ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
963dd4544f0SRafał Miłecki BGMAC_CHIPCTL_1_SW_TYPE_MASK),
964dd4544f0SRafał Miłecki sw_type);
9651cb94db3SRafał Miłecki } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE) {
9661cb94db3SRafał Miłecki u32 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_MII |
9671cb94db3SRafał Miłecki BGMAC_CHIPCTL_4_SW_TYPE_EPHY;
9681cb94db3SRafał Miłecki u8 et_swtype = 0;
9691cb94db3SRafał Miłecki char buf[4];
9701cb94db3SRafał Miłecki
9711cb94db3SRafał Miłecki if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
9721cb94db3SRafał Miłecki if (kstrtou8(buf, 0, &et_swtype))
9731cb94db3SRafał Miłecki dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
9741cb94db3SRafał Miłecki buf);
9751cb94db3SRafał Miłecki sw_type = (et_swtype & 0x0f) << 12;
9761cb94db3SRafał Miłecki } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII) {
9771cb94db3SRafał Miłecki sw_type = BGMAC_CHIPCTL_4_IF_TYPE_RGMII |
9781cb94db3SRafał Miłecki BGMAC_CHIPCTL_4_SW_TYPE_RGMII;
9791cb94db3SRafał Miłecki }
9801cb94db3SRafał Miłecki bgmac_cco_ctl_maskset(bgmac, 4, ~(BGMAC_CHIPCTL_4_IF_TYPE_MASK |
9811cb94db3SRafał Miłecki BGMAC_CHIPCTL_4_SW_TYPE_MASK),
9821cb94db3SRafał Miłecki sw_type);
9831cb94db3SRafał Miłecki } else if (bgmac->feature_flags & BGMAC_FEAT_CC7_IF_TYPE_RGMII) {
9841cb94db3SRafał Miłecki bgmac_cco_ctl_maskset(bgmac, 7, ~BGMAC_CHIPCTL_7_IF_TYPE_MASK,
9851cb94db3SRafał Miłecki BGMAC_CHIPCTL_7_IF_TYPE_RGMII);
986dd4544f0SRafał Miłecki }
987dd4544f0SRafał Miłecki
988dd4544f0SRafał Miłecki /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
98928e303daSRafał Miłecki * Specs don't say about using UMAC_CMD_SR, but in this routine
99028e303daSRafał Miłecki * UMAC_CMD is read _after_ putting chip in a reset. So it has to
991dd4544f0SRafał Miłecki * be keps until taking MAC out of the reset.
992dd4544f0SRafał Miłecki */
993db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
99428e303daSRafał Miłecki cmdcfg_sr = CMD_SW_RESET;
995db791eb2SJon Mason else
99628e303daSRafał Miłecki cmdcfg_sr = CMD_SW_RESET_OLD;
997db791eb2SJon Mason
99812cf8e75SRafał Miłecki bgmac_umac_cmd_maskset(bgmac,
99928e303daSRafał Miłecki ~(CMD_TX_EN |
100028e303daSRafał Miłecki CMD_RX_EN |
100128e303daSRafał Miłecki CMD_RX_PAUSE_IGNORE |
100228e303daSRafał Miłecki CMD_TX_ADDR_INS |
100328e303daSRafał Miłecki CMD_HD_EN |
100428e303daSRafał Miłecki CMD_LCL_LOOP_EN |
100528e303daSRafał Miłecki CMD_CNTL_FRM_EN |
100628e303daSRafał Miłecki CMD_RMT_LOOP_EN |
100728e303daSRafał Miłecki CMD_RX_ERR_DISC |
100828e303daSRafał Miłecki CMD_PRBL_EN |
100928e303daSRafał Miłecki CMD_TX_PAUSE_IGNORE |
101028e303daSRafał Miłecki CMD_PAD_EN |
101128e303daSRafał Miłecki CMD_PAUSE_FWD),
101228e303daSRafał Miłecki CMD_PROMISC |
101328e303daSRafał Miłecki CMD_NO_LEN_CHK |
101428e303daSRafał Miłecki CMD_CNTL_FRM_EN |
1015db791eb2SJon Mason cmdcfg_sr,
1016dd4544f0SRafał Miłecki false);
1017d469962fSRafał Miłecki bgmac->mac_speed = SPEED_UNKNOWN;
1018d469962fSRafał Miłecki bgmac->mac_duplex = DUPLEX_UNKNOWN;
1019dd4544f0SRafał Miłecki
1020dd4544f0SRafał Miłecki bgmac_clear_mib(bgmac);
1021db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_CMN_PHY_CTL)
1022f6a95a24SJon Mason bgmac_cmn_maskset32(bgmac, BCMA_GMAC_CMN_PHY_CTL, ~0,
1023dd4544f0SRafał Miłecki BCMA_GMAC_CMN_PC_MTE);
1024dd4544f0SRafał Miłecki else
1025dd4544f0SRafał Miłecki bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1026dd4544f0SRafał Miłecki bgmac_miiconfig(bgmac);
102755954f3bSJon Mason if (bgmac->mii_bus)
102855954f3bSJon Mason bgmac->mii_bus->reset(bgmac->mii_bus);
1029dd4544f0SRafał Miłecki
103049a467b4SHauke Mehrtens netdev_reset_queue(bgmac->net_dev);
1031dd4544f0SRafał Miłecki }
1032dd4544f0SRafał Miłecki
bgmac_chip_intrs_on(struct bgmac * bgmac)1033dd4544f0SRafał Miłecki static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1034dd4544f0SRafał Miłecki {
1035dd4544f0SRafał Miłecki bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1036dd4544f0SRafał Miłecki }
1037dd4544f0SRafał Miłecki
bgmac_chip_intrs_off(struct bgmac * bgmac)1038dd4544f0SRafał Miłecki static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1039dd4544f0SRafał Miłecki {
1040dd4544f0SRafał Miłecki bgmac_write(bgmac, BGMAC_INT_MASK, 0);
10414160815fSNathan Hintz bgmac_read(bgmac, BGMAC_INT_MASK);
1042dd4544f0SRafał Miłecki }
1043dd4544f0SRafał Miłecki
1044dd4544f0SRafał Miłecki /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
bgmac_enable(struct bgmac * bgmac)1045dd4544f0SRafał Miłecki static void bgmac_enable(struct bgmac *bgmac)
1046dd4544f0SRafał Miłecki {
1047db791eb2SJon Mason u32 cmdcfg_sr;
1048dd4544f0SRafał Miłecki u32 cmdcfg;
1049dd4544f0SRafał Miłecki u32 mode;
1050db791eb2SJon Mason
1051db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
105228e303daSRafał Miłecki cmdcfg_sr = CMD_SW_RESET;
1053db791eb2SJon Mason else
105428e303daSRafał Miłecki cmdcfg_sr = CMD_SW_RESET_OLD;
1055dd4544f0SRafał Miłecki
105628e303daSRafał Miłecki cmdcfg = bgmac_umac_read(bgmac, UMAC_CMD);
105728e303daSRafał Miłecki bgmac_umac_cmd_maskset(bgmac, ~(CMD_TX_EN | CMD_RX_EN),
1058db791eb2SJon Mason cmdcfg_sr, true);
1059dd4544f0SRafał Miłecki udelay(2);
106028e303daSRafał Miłecki cmdcfg |= CMD_TX_EN | CMD_RX_EN;
106128e303daSRafał Miłecki bgmac_umac_write(bgmac, UMAC_CMD, cmdcfg);
1062dd4544f0SRafał Miłecki
1063dd4544f0SRafał Miłecki mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1064dd4544f0SRafał Miłecki BGMAC_DS_MM_SHIFT;
1065cdb26d33SRafał Miłecki if (bgmac->feature_flags & BGMAC_FEAT_CLKCTLST || mode != 0)
1066dd4544f0SRafał Miłecki bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1067cdb26d33SRafał Miłecki if (!(bgmac->feature_flags & BGMAC_FEAT_CLKCTLST) && mode == 2)
1068f6a95a24SJon Mason bgmac_cco_ctl_maskset(bgmac, 1, ~0,
1069dd4544f0SRafał Miłecki BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1070dd4544f0SRafał Miłecki
1071db791eb2SJon Mason if (bgmac->feature_flags & (BGMAC_FEAT_FLW_CTRL1 |
1072db791eb2SJon Mason BGMAC_FEAT_FLW_CTRL2)) {
1073db791eb2SJon Mason u32 fl_ctl;
1074db791eb2SJon Mason
1075db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_FLW_CTRL1)
1076dd4544f0SRafał Miłecki fl_ctl = 0x2300e1;
1077db791eb2SJon Mason else
1078db791eb2SJon Mason fl_ctl = 0x03cb04cb;
1079db791eb2SJon Mason
1080dd4544f0SRafał Miłecki bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
108128e303daSRafał Miłecki bgmac_umac_write(bgmac, UMAC_PAUSE_CTRL, 0x27fff);
1082dd4544f0SRafał Miłecki }
1083dd4544f0SRafał Miłecki
1084db791eb2SJon Mason if (bgmac->feature_flags & BGMAC_FEAT_SET_RXQ_CLK) {
1085db791eb2SJon Mason u32 rxq_ctl;
1086db791eb2SJon Mason u16 bp_clk;
1087db791eb2SJon Mason u8 mdp;
1088db791eb2SJon Mason
1089dd4544f0SRafał Miłecki rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1090dd4544f0SRafał Miłecki rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1091f6a95a24SJon Mason bp_clk = bgmac_get_bus_clock(bgmac) / 1000000;
1092dd4544f0SRafał Miłecki mdp = (bp_clk * 128 / 1000) - 3;
1093dd4544f0SRafał Miłecki rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1094dd4544f0SRafał Miłecki bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1095dd4544f0SRafał Miłecki }
10966df4aff9SHauke Mehrtens }
1097dd4544f0SRafał Miłecki
1098dd4544f0SRafał Miłecki /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
bgmac_chip_init(struct bgmac * bgmac)109974b6f291SFelix Fietkau static void bgmac_chip_init(struct bgmac *bgmac)
1100dd4544f0SRafał Miłecki {
1101dd5c5d03SJon Mason /* Clear any erroneously pending interrupts */
1102dd5c5d03SJon Mason bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
1103dd5c5d03SJon Mason
1104dd4544f0SRafał Miłecki /* 1 interrupt per received frame */
1105dd4544f0SRafał Miłecki bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1106dd4544f0SRafał Miłecki
1107dd4544f0SRafał Miłecki /* Enable 802.3x tx flow control (honor received PAUSE frames) */
110828e303daSRafał Miłecki bgmac_umac_cmd_maskset(bgmac, ~CMD_RX_PAUSE_IGNORE, 0, true);
1109dd4544f0SRafał Miłecki
1110c6edfe10SHauke Mehrtens bgmac_set_rx_mode(bgmac->net_dev);
1111dd4544f0SRafał Miłecki
11124e209001SHauke Mehrtens bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
1113dd4544f0SRafał Miłecki
1114dd4544f0SRafał Miłecki if (bgmac->loopback)
111528e303daSRafał Miłecki bgmac_umac_cmd_maskset(bgmac, ~0, CMD_LCL_LOOP_EN, false);
1116dd4544f0SRafał Miłecki else
111728e303daSRafał Miłecki bgmac_umac_cmd_maskset(bgmac, ~CMD_LCL_LOOP_EN, 0, false);
1118dd4544f0SRafał Miłecki
111928e303daSRafał Miłecki bgmac_umac_write(bgmac, UMAC_MAX_FRAME_LEN, 32 + ETHER_MAX_LEN);
1120dd4544f0SRafał Miłecki
1121dd4544f0SRafał Miłecki bgmac_chip_intrs_on(bgmac);
1122dd4544f0SRafał Miłecki
1123dd4544f0SRafał Miłecki bgmac_enable(bgmac);
1124dd4544f0SRafał Miłecki }
1125dd4544f0SRafał Miłecki
bgmac_interrupt(int irq,void * dev_id)1126dd4544f0SRafał Miłecki static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1127dd4544f0SRafał Miłecki {
1128dd4544f0SRafał Miłecki struct bgmac *bgmac = netdev_priv(dev_id);
1129dd4544f0SRafał Miłecki
1130dd4544f0SRafał Miłecki u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1131dd4544f0SRafał Miłecki int_status &= bgmac->int_mask;
1132dd4544f0SRafał Miłecki
1133dd4544f0SRafał Miłecki if (!int_status)
1134dd4544f0SRafał Miłecki return IRQ_NONE;
1135dd4544f0SRafał Miłecki
1136eb64e292SFelix Fietkau int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1137eb64e292SFelix Fietkau if (int_status)
1138d00a8281SJon Mason dev_err(bgmac->dev, "Unknown IRQs: 0x%08X\n", int_status);
1139dd4544f0SRafał Miłecki
1140dd4544f0SRafał Miłecki /* Disable new interrupts until handling existing ones */
1141dd4544f0SRafał Miłecki bgmac_chip_intrs_off(bgmac);
1142dd4544f0SRafał Miłecki
1143dd4544f0SRafał Miłecki napi_schedule(&bgmac->napi);
1144dd4544f0SRafał Miłecki
1145dd4544f0SRafał Miłecki return IRQ_HANDLED;
1146dd4544f0SRafał Miłecki }
1147dd4544f0SRafał Miłecki
bgmac_poll(struct napi_struct * napi,int weight)1148dd4544f0SRafał Miłecki static int bgmac_poll(struct napi_struct *napi, int weight)
1149dd4544f0SRafał Miłecki {
1150dd4544f0SRafał Miłecki struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
1151dd4544f0SRafał Miłecki int handled = 0;
1152dd4544f0SRafał Miłecki
1153eb64e292SFelix Fietkau /* Ack */
1154eb64e292SFelix Fietkau bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
1155dd4544f0SRafał Miłecki
1156eb64e292SFelix Fietkau bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1157eb64e292SFelix Fietkau handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
1158dd4544f0SRafał Miłecki
1159eb64e292SFelix Fietkau /* Poll again if more events arrived in the meantime */
1160eb64e292SFelix Fietkau if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
1161e580267dSRafał Miłecki return weight;
1162dd4544f0SRafał Miłecki
116343f159c6SHauke Mehrtens if (handled < weight) {
11646ad20165SEric Dumazet napi_complete_done(napi, handled);
1165dd4544f0SRafał Miłecki bgmac_chip_intrs_on(bgmac);
116643f159c6SHauke Mehrtens }
1167dd4544f0SRafał Miłecki
1168dd4544f0SRafał Miłecki return handled;
1169dd4544f0SRafał Miłecki }
1170dd4544f0SRafał Miłecki
1171dd4544f0SRafał Miłecki /**************************************************
1172dd4544f0SRafał Miłecki * net_device_ops
1173dd4544f0SRafał Miłecki **************************************************/
1174dd4544f0SRafał Miłecki
bgmac_open(struct net_device * net_dev)1175dd4544f0SRafał Miłecki static int bgmac_open(struct net_device *net_dev)
1176dd4544f0SRafał Miłecki {
1177dd4544f0SRafał Miłecki struct bgmac *bgmac = netdev_priv(net_dev);
1178dd4544f0SRafał Miłecki int err = 0;
1179dd4544f0SRafał Miłecki
1180dd4544f0SRafał Miłecki bgmac_chip_reset(bgmac);
118174b6f291SFelix Fietkau
118274b6f291SFelix Fietkau err = bgmac_dma_init(bgmac);
118374b6f291SFelix Fietkau if (err)
118474b6f291SFelix Fietkau return err;
118574b6f291SFelix Fietkau
1186dd4544f0SRafał Miłecki /* Specs say about reclaiming rings here, but we do that in DMA init */
118774b6f291SFelix Fietkau bgmac_chip_init(bgmac);
1188dd4544f0SRafał Miłecki
1189f6a95a24SJon Mason err = request_irq(bgmac->irq, bgmac_interrupt, IRQF_SHARED,
1190d72e7c21SFlorian Fainelli net_dev->name, net_dev);
1191dd4544f0SRafał Miłecki if (err < 0) {
1192d00a8281SJon Mason dev_err(bgmac->dev, "IRQ request error: %d!\n", err);
119374b6f291SFelix Fietkau bgmac_dma_cleanup(bgmac);
119474b6f291SFelix Fietkau return err;
1195dd4544f0SRafał Miłecki }
1196dd4544f0SRafał Miłecki napi_enable(&bgmac->napi);
1197dd4544f0SRafał Miłecki
1198b21fcb25SPhilippe Reynes phy_start(net_dev->phydev);
11994e34da4dSRafał Miłecki
1200c3897f2aSFlorian Fainelli netif_start_queue(net_dev);
1201c3897f2aSFlorian Fainelli
120274b6f291SFelix Fietkau return 0;
1203dd4544f0SRafał Miłecki }
1204dd4544f0SRafał Miłecki
bgmac_stop(struct net_device * net_dev)1205dd4544f0SRafał Miłecki static int bgmac_stop(struct net_device *net_dev)
1206dd4544f0SRafał Miłecki {
1207dd4544f0SRafał Miłecki struct bgmac *bgmac = netdev_priv(net_dev);
1208dd4544f0SRafał Miłecki
1209dd4544f0SRafał Miłecki netif_carrier_off(net_dev);
1210dd4544f0SRafał Miłecki
1211b21fcb25SPhilippe Reynes phy_stop(net_dev->phydev);
12124e34da4dSRafał Miłecki
1213dd4544f0SRafał Miłecki napi_disable(&bgmac->napi);
1214dd4544f0SRafał Miłecki bgmac_chip_intrs_off(bgmac);
1215f6a95a24SJon Mason free_irq(bgmac->irq, net_dev);
1216dd4544f0SRafał Miłecki
1217dd4544f0SRafał Miłecki bgmac_chip_reset(bgmac);
121874b6f291SFelix Fietkau bgmac_dma_cleanup(bgmac);
1219dd4544f0SRafał Miłecki
1220dd4544f0SRafał Miłecki return 0;
1221dd4544f0SRafał Miłecki }
1222dd4544f0SRafał Miłecki
bgmac_start_xmit(struct sk_buff * skb,struct net_device * net_dev)1223dd4544f0SRafał Miłecki static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1224dd4544f0SRafał Miłecki struct net_device *net_dev)
1225dd4544f0SRafał Miłecki {
1226dd4544f0SRafał Miłecki struct bgmac *bgmac = netdev_priv(net_dev);
1227dd4544f0SRafał Miłecki struct bgmac_dma_ring *ring;
1228dd4544f0SRafał Miłecki
1229dd4544f0SRafał Miłecki /* No QOS support yet */
1230dd4544f0SRafał Miłecki ring = &bgmac->tx_ring[0];
1231dd4544f0SRafał Miłecki return bgmac_dma_tx_add(bgmac, ring, skb);
1232dd4544f0SRafał Miłecki }
1233dd4544f0SRafał Miłecki
bgmac_set_mac_address(struct net_device * net_dev,void * addr)12344e209001SHauke Mehrtens static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
12354e209001SHauke Mehrtens {
12364e209001SHauke Mehrtens struct bgmac *bgmac = netdev_priv(net_dev);
1237fa42245dSHari Vyas struct sockaddr *sa = addr;
12384e209001SHauke Mehrtens int ret;
12394e209001SHauke Mehrtens
12404e209001SHauke Mehrtens ret = eth_prepare_mac_addr_change(net_dev, addr);
12414e209001SHauke Mehrtens if (ret < 0)
12424e209001SHauke Mehrtens return ret;
1243fa42245dSHari Vyas
1244f3956ebbSJakub Kicinski eth_hw_addr_set(net_dev, sa->sa_data);
1245fa42245dSHari Vyas bgmac_write_mac_address(bgmac, net_dev->dev_addr);
1246fa42245dSHari Vyas
12474e209001SHauke Mehrtens eth_commit_mac_addr_change(net_dev, addr);
12484e209001SHauke Mehrtens return 0;
12494e209001SHauke Mehrtens }
12504e209001SHauke Mehrtens
bgmac_change_mtu(struct net_device * net_dev,int mtu)12518c7da639SMurali Krishna Policharla static int bgmac_change_mtu(struct net_device *net_dev, int mtu)
12528c7da639SMurali Krishna Policharla {
12538c7da639SMurali Krishna Policharla struct bgmac *bgmac = netdev_priv(net_dev);
12548c7da639SMurali Krishna Policharla
125528e303daSRafał Miłecki bgmac_umac_write(bgmac, UMAC_MAX_FRAME_LEN, 32 + mtu);
12568c7da639SMurali Krishna Policharla return 0;
12578c7da639SMurali Krishna Policharla }
12588c7da639SMurali Krishna Policharla
1259dd4544f0SRafał Miłecki static const struct net_device_ops bgmac_netdev_ops = {
1260dd4544f0SRafał Miłecki .ndo_open = bgmac_open,
1261dd4544f0SRafał Miłecki .ndo_stop = bgmac_stop,
1262dd4544f0SRafał Miłecki .ndo_start_xmit = bgmac_start_xmit,
1263c6edfe10SHauke Mehrtens .ndo_set_rx_mode = bgmac_set_rx_mode,
12644e209001SHauke Mehrtens .ndo_set_mac_address = bgmac_set_mac_address,
1265522c5907SHauke Mehrtens .ndo_validate_addr = eth_validate_addr,
1266a7605370SArnd Bergmann .ndo_eth_ioctl = phy_do_ioctl_running,
12678c7da639SMurali Krishna Policharla .ndo_change_mtu = bgmac_change_mtu,
1268dd4544f0SRafał Miłecki };
1269dd4544f0SRafał Miłecki
1270dd4544f0SRafał Miłecki /**************************************************
1271dd4544f0SRafał Miłecki * ethtool_ops
1272dd4544f0SRafał Miłecki **************************************************/
1273dd4544f0SRafał Miłecki
1274f6613d4fSFlorian Fainelli struct bgmac_stat {
1275f6613d4fSFlorian Fainelli u8 size;
1276f6613d4fSFlorian Fainelli u32 offset;
1277f6613d4fSFlorian Fainelli const char *name;
1278f6613d4fSFlorian Fainelli };
1279f6613d4fSFlorian Fainelli
1280f6613d4fSFlorian Fainelli static struct bgmac_stat bgmac_get_strings_stats[] = {
1281f6613d4fSFlorian Fainelli { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
1282f6613d4fSFlorian Fainelli { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
1283f6613d4fSFlorian Fainelli { 8, BGMAC_TX_OCTETS, "tx_octets" },
1284f6613d4fSFlorian Fainelli { 4, BGMAC_TX_PKTS, "tx_pkts" },
1285f6613d4fSFlorian Fainelli { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
1286f6613d4fSFlorian Fainelli { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
1287f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_64, "tx_64" },
1288f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
1289f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
1290f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
1291f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
1292f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
1293f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
1294f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
1295f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
1296f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
1297f6613d4fSFlorian Fainelli { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
1298f6613d4fSFlorian Fainelli { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
1299f6613d4fSFlorian Fainelli { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
1300f6613d4fSFlorian Fainelli { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
1301f6613d4fSFlorian Fainelli { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
1302f6613d4fSFlorian Fainelli { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
1303f6613d4fSFlorian Fainelli { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
1304f6613d4fSFlorian Fainelli { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
1305f6613d4fSFlorian Fainelli { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
1306f6613d4fSFlorian Fainelli { 4, BGMAC_TX_DEFERED, "tx_defered" },
1307f6613d4fSFlorian Fainelli { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
1308f6613d4fSFlorian Fainelli { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
1309f6613d4fSFlorian Fainelli { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
1310f6613d4fSFlorian Fainelli { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
1311f6613d4fSFlorian Fainelli { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
1312f6613d4fSFlorian Fainelli { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
1313f6613d4fSFlorian Fainelli { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
1314f6613d4fSFlorian Fainelli { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
1315f6613d4fSFlorian Fainelli { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
1316f6613d4fSFlorian Fainelli { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
1317f6613d4fSFlorian Fainelli { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
1318f6613d4fSFlorian Fainelli { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
1319f6613d4fSFlorian Fainelli { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
1320f6613d4fSFlorian Fainelli { 8, BGMAC_RX_OCTETS, "rx_octets" },
1321f6613d4fSFlorian Fainelli { 4, BGMAC_RX_PKTS, "rx_pkts" },
1322f6613d4fSFlorian Fainelli { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
1323f6613d4fSFlorian Fainelli { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
1324f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_64, "rx_64" },
1325f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
1326f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
1327f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
1328f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
1329f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
1330f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
1331f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
1332f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
1333f6613d4fSFlorian Fainelli { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
1334f6613d4fSFlorian Fainelli { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
1335f6613d4fSFlorian Fainelli { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
1336f6613d4fSFlorian Fainelli { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
1337f6613d4fSFlorian Fainelli { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
1338f6613d4fSFlorian Fainelli { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
1339f6613d4fSFlorian Fainelli { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
1340f6613d4fSFlorian Fainelli { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
1341f6613d4fSFlorian Fainelli { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
1342f6613d4fSFlorian Fainelli { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
1343f6613d4fSFlorian Fainelli { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
1344f6613d4fSFlorian Fainelli { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
1345f6613d4fSFlorian Fainelli { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
1346f6613d4fSFlorian Fainelli { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
1347f6613d4fSFlorian Fainelli };
1348f6613d4fSFlorian Fainelli
1349f6613d4fSFlorian Fainelli #define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
1350f6613d4fSFlorian Fainelli
bgmac_get_sset_count(struct net_device * dev,int string_set)1351f6613d4fSFlorian Fainelli static int bgmac_get_sset_count(struct net_device *dev, int string_set)
1352f6613d4fSFlorian Fainelli {
1353f6613d4fSFlorian Fainelli switch (string_set) {
1354f6613d4fSFlorian Fainelli case ETH_SS_STATS:
1355f6613d4fSFlorian Fainelli return BGMAC_STATS_LEN;
1356f6613d4fSFlorian Fainelli }
1357f6613d4fSFlorian Fainelli
1358f6613d4fSFlorian Fainelli return -EOPNOTSUPP;
1359f6613d4fSFlorian Fainelli }
1360f6613d4fSFlorian Fainelli
bgmac_get_strings(struct net_device * dev,u32 stringset,u8 * data)1361f6613d4fSFlorian Fainelli static void bgmac_get_strings(struct net_device *dev, u32 stringset,
1362f6613d4fSFlorian Fainelli u8 *data)
1363f6613d4fSFlorian Fainelli {
1364f6613d4fSFlorian Fainelli int i;
1365f6613d4fSFlorian Fainelli
1366f6613d4fSFlorian Fainelli if (stringset != ETH_SS_STATS)
1367f6613d4fSFlorian Fainelli return;
1368f6613d4fSFlorian Fainelli
1369f6613d4fSFlorian Fainelli for (i = 0; i < BGMAC_STATS_LEN; i++)
1370f029c781SWolfram Sang strscpy(data + i * ETH_GSTRING_LEN,
1371f6613d4fSFlorian Fainelli bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
1372f6613d4fSFlorian Fainelli }
1373f6613d4fSFlorian Fainelli
bgmac_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * ss,uint64_t * data)1374f6613d4fSFlorian Fainelli static void bgmac_get_ethtool_stats(struct net_device *dev,
1375f6613d4fSFlorian Fainelli struct ethtool_stats *ss, uint64_t *data)
1376f6613d4fSFlorian Fainelli {
1377f6613d4fSFlorian Fainelli struct bgmac *bgmac = netdev_priv(dev);
1378f6613d4fSFlorian Fainelli const struct bgmac_stat *s;
1379f6613d4fSFlorian Fainelli unsigned int i;
1380f6613d4fSFlorian Fainelli u64 val;
1381f6613d4fSFlorian Fainelli
1382f6613d4fSFlorian Fainelli if (!netif_running(dev))
1383f6613d4fSFlorian Fainelli return;
1384f6613d4fSFlorian Fainelli
1385f6613d4fSFlorian Fainelli for (i = 0; i < BGMAC_STATS_LEN; i++) {
1386f6613d4fSFlorian Fainelli s = &bgmac_get_strings_stats[i];
1387f6613d4fSFlorian Fainelli val = 0;
1388f6613d4fSFlorian Fainelli if (s->size == 8)
1389f6613d4fSFlorian Fainelli val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
1390f6613d4fSFlorian Fainelli val |= bgmac_read(bgmac, s->offset);
1391f6613d4fSFlorian Fainelli data[i] = val;
1392f6613d4fSFlorian Fainelli }
1393f6613d4fSFlorian Fainelli }
1394f6613d4fSFlorian Fainelli
bgmac_get_drvinfo(struct net_device * net_dev,struct ethtool_drvinfo * info)1395dd4544f0SRafał Miłecki static void bgmac_get_drvinfo(struct net_device *net_dev,
1396dd4544f0SRafał Miłecki struct ethtool_drvinfo *info)
1397dd4544f0SRafał Miłecki {
1398f029c781SWolfram Sang strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1399f029c781SWolfram Sang strscpy(info->bus_info, "AXI", sizeof(info->bus_info));
1400dd4544f0SRafał Miłecki }
1401dd4544f0SRafał Miłecki
1402dd4544f0SRafał Miłecki static const struct ethtool_ops bgmac_ethtool_ops = {
1403f6613d4fSFlorian Fainelli .get_strings = bgmac_get_strings,
1404f6613d4fSFlorian Fainelli .get_sset_count = bgmac_get_sset_count,
1405f6613d4fSFlorian Fainelli .get_ethtool_stats = bgmac_get_ethtool_stats,
1406dd4544f0SRafał Miłecki .get_drvinfo = bgmac_get_drvinfo,
1407904632a2SPhilippe Reynes .get_link_ksettings = phy_ethtool_get_link_ksettings,
1408904632a2SPhilippe Reynes .set_link_ksettings = phy_ethtool_set_link_ksettings,
1409dd4544f0SRafał Miłecki };
1410dd4544f0SRafał Miłecki
1411dd4544f0SRafał Miłecki /**************************************************
141211e5e76eSRafał Miłecki * MII
141311e5e76eSRafał Miłecki **************************************************/
141411e5e76eSRafał Miłecki
bgmac_adjust_link(struct net_device * net_dev)14151676aba5SJon Mason void bgmac_adjust_link(struct net_device *net_dev)
14165824d2d1SRafał Miłecki {
14175824d2d1SRafał Miłecki struct bgmac *bgmac = netdev_priv(net_dev);
1418b21fcb25SPhilippe Reynes struct phy_device *phy_dev = net_dev->phydev;
14195824d2d1SRafał Miłecki bool update = false;
14205824d2d1SRafał Miłecki
14215824d2d1SRafał Miłecki if (phy_dev->link) {
14225824d2d1SRafał Miłecki if (phy_dev->speed != bgmac->mac_speed) {
14235824d2d1SRafał Miłecki bgmac->mac_speed = phy_dev->speed;
14245824d2d1SRafał Miłecki update = true;
14255824d2d1SRafał Miłecki }
14265824d2d1SRafał Miłecki
14275824d2d1SRafał Miłecki if (phy_dev->duplex != bgmac->mac_duplex) {
14285824d2d1SRafał Miłecki bgmac->mac_duplex = phy_dev->duplex;
14295824d2d1SRafał Miłecki update = true;
14305824d2d1SRafał Miłecki }
14315824d2d1SRafał Miłecki }
14325824d2d1SRafał Miłecki
14335824d2d1SRafał Miłecki if (update) {
14345824d2d1SRafał Miłecki bgmac_mac_speed(bgmac);
14355824d2d1SRafał Miłecki phy_print_status(phy_dev);
14365824d2d1SRafał Miłecki }
14375824d2d1SRafał Miłecki }
14381676aba5SJon Mason EXPORT_SYMBOL_GPL(bgmac_adjust_link);
14395824d2d1SRafał Miłecki
bgmac_phy_connect_direct(struct bgmac * bgmac)14401676aba5SJon Mason int bgmac_phy_connect_direct(struct bgmac *bgmac)
1441c25b23b8SRafał Miłecki {
1442c25b23b8SRafał Miłecki struct fixed_phy_status fphy_status = {
1443c25b23b8SRafał Miłecki .link = 1,
1444c25b23b8SRafał Miłecki .speed = SPEED_1000,
1445c25b23b8SRafał Miłecki .duplex = DUPLEX_FULL,
1446c25b23b8SRafał Miłecki };
1447c25b23b8SRafał Miłecki struct phy_device *phy_dev;
1448c25b23b8SRafał Miłecki int err;
1449c25b23b8SRafał Miłecki
14505468e82fSLinus Walleij phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
145123a14488SRuan Jinjie if (IS_ERR(phy_dev)) {
1452d00a8281SJon Mason dev_err(bgmac->dev, "Failed to register fixed PHY device\n");
1453*d6499f0bSRuan Jinjie return PTR_ERR(phy_dev);
1454c25b23b8SRafał Miłecki }
1455c25b23b8SRafał Miłecki
1456c25b23b8SRafał Miłecki err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1457c25b23b8SRafał Miłecki PHY_INTERFACE_MODE_MII);
1458c25b23b8SRafał Miłecki if (err) {
1459d00a8281SJon Mason dev_err(bgmac->dev, "Connecting PHY failed\n");
1460c25b23b8SRafał Miłecki return err;
1461c25b23b8SRafał Miłecki }
1462c25b23b8SRafał Miłecki
1463c25b23b8SRafał Miłecki return err;
1464c25b23b8SRafał Miłecki }
14651676aba5SJon Mason EXPORT_SYMBOL_GPL(bgmac_phy_connect_direct);
146611e5e76eSRafał Miłecki
bgmac_alloc(struct device * dev)146734a5102cSRafał Miłecki struct bgmac *bgmac_alloc(struct device *dev)
1468dd4544f0SRafał Miłecki {
1469dd4544f0SRafał Miłecki struct net_device *net_dev;
1470dd4544f0SRafał Miłecki struct bgmac *bgmac;
1471dd4544f0SRafał Miłecki
1472dd4544f0SRafał Miłecki /* Allocation and references */
147334a5102cSRafał Miłecki net_dev = devm_alloc_etherdev(dev, sizeof(*bgmac));
1474dd4544f0SRafał Miłecki if (!net_dev)
147534a5102cSRafał Miłecki return NULL;
1476f6a95a24SJon Mason
1477dd4544f0SRafał Miłecki net_dev->netdev_ops = &bgmac_netdev_ops;
14787ad24ea4SWilfried Klaebe net_dev->ethtool_ops = &bgmac_ethtool_ops;
147934a5102cSRafał Miłecki
1480dd4544f0SRafał Miłecki bgmac = netdev_priv(net_dev);
148134a5102cSRafał Miłecki bgmac->dev = dev;
1482dd4544f0SRafał Miłecki bgmac->net_dev = net_dev;
148334a5102cSRafał Miłecki
148434a5102cSRafał Miłecki return bgmac;
148534a5102cSRafał Miłecki }
148634a5102cSRafał Miłecki EXPORT_SYMBOL_GPL(bgmac_alloc);
148734a5102cSRafał Miłecki
bgmac_enet_probe(struct bgmac * bgmac)148834a5102cSRafał Miłecki int bgmac_enet_probe(struct bgmac *bgmac)
148934a5102cSRafał Miłecki {
149034a5102cSRafał Miłecki struct net_device *net_dev = bgmac->net_dev;
149134a5102cSRafał Miłecki int err;
149234a5102cSRafał Miłecki
1493f99e6d7cSRafał Miłecki bgmac->in_init = true;
1494f99e6d7cSRafał Miłecki
1495f6a95a24SJon Mason net_dev->irq = bgmac->irq;
1496f6a95a24SJon Mason SET_NETDEV_DEV(net_dev, bgmac->dev);
1497f3537b34SJoey Zhong dev_set_drvdata(bgmac->dev, bgmac);
1498dd4544f0SRafał Miłecki
14996850f8b5STobias Klauser if (!is_valid_ether_addr(net_dev->dev_addr)) {
1500f6a95a24SJon Mason dev_err(bgmac->dev, "Invalid MAC addr: %pM\n",
15016850f8b5STobias Klauser net_dev->dev_addr);
15026850f8b5STobias Klauser eth_hw_addr_random(net_dev);
1503f6a95a24SJon Mason dev_warn(bgmac->dev, "Using random MAC: %pM\n",
15046850f8b5STobias Klauser net_dev->dev_addr);
1505f6a95a24SJon Mason }
1506dd4544f0SRafał Miłecki
1507f6a95a24SJon Mason /* This (reset &) enable is not preset in specs or reference driver but
1508f6a95a24SJon Mason * Broadcom does it in arch PCI code when enabling fake PCI device.
1509f6a95a24SJon Mason */
1510f6a95a24SJon Mason bgmac_clk_enable(bgmac, 0);
1511dd4544f0SRafał Miłecki
1512e7731194SRafał Miłecki bgmac_chip_intrs_off(bgmac);
1513e7731194SRafał Miłecki
15141cb94db3SRafał Miłecki /* This seems to be fixing IRQ by assigning OOB #6 to the core */
1515a163bdb0SAbhishek Shah if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
15161cb94db3SRafał Miłecki if (bgmac->feature_flags & BGMAC_FEAT_IRQ_ID_OOB_6)
15171cb94db3SRafał Miłecki bgmac_idm_write(bgmac, BCMA_OOB_SEL_OUT_A30, 0x86);
1518a163bdb0SAbhishek Shah }
15191cb94db3SRafał Miłecki
1520dd4544f0SRafał Miłecki bgmac_chip_reset(bgmac);
1521dd4544f0SRafał Miłecki
1522dd4544f0SRafał Miłecki err = bgmac_dma_alloc(bgmac);
1523dd4544f0SRafał Miłecki if (err) {
1524d00a8281SJon Mason dev_err(bgmac->dev, "Unable to alloc memory for DMA\n");
152534a5102cSRafał Miłecki goto err_out;
1526dd4544f0SRafał Miłecki }
1527dd4544f0SRafał Miłecki
1528dd4544f0SRafał Miłecki bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
1529edb15d83SRalf Baechle if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
1530dd4544f0SRafał Miłecki bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1531dd4544f0SRafał Miłecki
1532b48b89f9SJakub Kicinski netif_napi_add(net_dev, &bgmac->napi, bgmac_poll);
15336216642fSHauke Mehrtens
153455954f3bSJon Mason err = bgmac_phy_connect(bgmac);
153511e5e76eSRafał Miłecki if (err) {
1536d00a8281SJon Mason dev_err(bgmac->dev, "Cannot connect to phy\n");
1537f6a95a24SJon Mason goto err_dma_free;
153811e5e76eSRafał Miłecki }
153911e5e76eSRafał Miłecki
15409cde9450SFelix Fietkau net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
15419cde9450SFelix Fietkau net_dev->hw_features = net_dev->features;
15429cde9450SFelix Fietkau net_dev->vlan_features = net_dev->features;
15439cde9450SFelix Fietkau
15448c7da639SMurali Krishna Policharla /* Omit FCS from max MTU size */
15458c7da639SMurali Krishna Policharla net_dev->max_mtu = BGMAC_RX_MAX_FRAME_SIZE - ETH_FCS_LEN;
15468c7da639SMurali Krishna Policharla
1547f99e6d7cSRafał Miłecki bgmac->in_init = false;
1548f99e6d7cSRafał Miłecki
1549dd4544f0SRafał Miłecki err = register_netdev(bgmac->net_dev);
1550dd4544f0SRafał Miłecki if (err) {
1551d00a8281SJon Mason dev_err(bgmac->dev, "Cannot register net device\n");
155255954f3bSJon Mason goto err_phy_disconnect;
1553dd4544f0SRafał Miłecki }
1554dd4544f0SRafał Miłecki
1555dd4544f0SRafał Miłecki netif_carrier_off(net_dev);
1556dd4544f0SRafał Miłecki
1557dd4544f0SRafał Miłecki return 0;
1558dd4544f0SRafał Miłecki
155955954f3bSJon Mason err_phy_disconnect:
156055954f3bSJon Mason phy_disconnect(net_dev->phydev);
1561dd4544f0SRafał Miłecki err_dma_free:
1562dd4544f0SRafał Miłecki bgmac_dma_free(bgmac);
156334a5102cSRafał Miłecki err_out:
1564dd4544f0SRafał Miłecki
1565dd4544f0SRafał Miłecki return err;
1566dd4544f0SRafał Miłecki }
1567f6a95a24SJon Mason EXPORT_SYMBOL_GPL(bgmac_enet_probe);
1568dd4544f0SRafał Miłecki
bgmac_enet_remove(struct bgmac * bgmac)1569f6a95a24SJon Mason void bgmac_enet_remove(struct bgmac *bgmac)
1570dd4544f0SRafał Miłecki {
1571dd4544f0SRafał Miłecki unregister_netdev(bgmac->net_dev);
157255954f3bSJon Mason phy_disconnect(bgmac->net_dev->phydev);
15736216642fSHauke Mehrtens netif_napi_del(&bgmac->napi);
1574dd4544f0SRafał Miłecki bgmac_dma_free(bgmac);
1575dd4544f0SRafał Miłecki }
1576f6a95a24SJon Mason EXPORT_SYMBOL_GPL(bgmac_enet_remove);
1577dd4544f0SRafał Miłecki
bgmac_enet_suspend(struct bgmac * bgmac)1578f3537b34SJoey Zhong int bgmac_enet_suspend(struct bgmac *bgmac)
1579f3537b34SJoey Zhong {
1580f3537b34SJoey Zhong if (!netif_running(bgmac->net_dev))
1581f3537b34SJoey Zhong return 0;
1582f3537b34SJoey Zhong
1583f3537b34SJoey Zhong phy_stop(bgmac->net_dev->phydev);
1584f3537b34SJoey Zhong
1585f3537b34SJoey Zhong netif_stop_queue(bgmac->net_dev);
1586f3537b34SJoey Zhong
1587f3537b34SJoey Zhong napi_disable(&bgmac->napi);
1588f3537b34SJoey Zhong
1589f3537b34SJoey Zhong netif_tx_lock(bgmac->net_dev);
1590f3537b34SJoey Zhong netif_device_detach(bgmac->net_dev);
1591f3537b34SJoey Zhong netif_tx_unlock(bgmac->net_dev);
1592f3537b34SJoey Zhong
1593f3537b34SJoey Zhong bgmac_chip_intrs_off(bgmac);
1594f3537b34SJoey Zhong bgmac_chip_reset(bgmac);
1595f3537b34SJoey Zhong bgmac_dma_cleanup(bgmac);
1596f3537b34SJoey Zhong
1597f3537b34SJoey Zhong return 0;
1598f3537b34SJoey Zhong }
1599f3537b34SJoey Zhong EXPORT_SYMBOL_GPL(bgmac_enet_suspend);
1600f3537b34SJoey Zhong
bgmac_enet_resume(struct bgmac * bgmac)1601f3537b34SJoey Zhong int bgmac_enet_resume(struct bgmac *bgmac)
1602f3537b34SJoey Zhong {
1603f3537b34SJoey Zhong int rc;
1604f3537b34SJoey Zhong
1605f3537b34SJoey Zhong if (!netif_running(bgmac->net_dev))
1606f3537b34SJoey Zhong return 0;
1607f3537b34SJoey Zhong
1608f3537b34SJoey Zhong rc = bgmac_dma_init(bgmac);
1609f3537b34SJoey Zhong if (rc)
1610f3537b34SJoey Zhong return rc;
1611f3537b34SJoey Zhong
1612f3537b34SJoey Zhong bgmac_chip_init(bgmac);
1613f3537b34SJoey Zhong
1614f3537b34SJoey Zhong napi_enable(&bgmac->napi);
1615f3537b34SJoey Zhong
1616f3537b34SJoey Zhong netif_tx_lock(bgmac->net_dev);
1617f3537b34SJoey Zhong netif_device_attach(bgmac->net_dev);
1618f3537b34SJoey Zhong netif_tx_unlock(bgmac->net_dev);
1619f3537b34SJoey Zhong
1620f3537b34SJoey Zhong netif_start_queue(bgmac->net_dev);
1621f3537b34SJoey Zhong
1622f3537b34SJoey Zhong phy_start(bgmac->net_dev->phydev);
1623f3537b34SJoey Zhong
1624f3537b34SJoey Zhong return 0;
1625f3537b34SJoey Zhong }
1626f3537b34SJoey Zhong EXPORT_SYMBOL_GPL(bgmac_enet_resume);
1627f3537b34SJoey Zhong
1628dd4544f0SRafał Miłecki MODULE_AUTHOR("Rafał Miłecki");
1629dd4544f0SRafał Miłecki MODULE_LICENSE("GPL");
1630