blob: 48d672b204a4847bc113635ed9534d8a3fd53066 [file] [log] [blame]
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001/*
2 * Driver for (BCM4706)? GBit MAC core on BCMA bus.
3 *
4 * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
5 *
6 * Licensed under the GNU/GPL. See COPYING for details.
7 */
8
Jon Masonf6a95a22016-07-07 19:08:57 -04009
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/bcma/bcma.h>
13#include <linux/etherdevice.h>
Florian Westphal282ccf62017-03-29 17:17:31 +020014#include <linux/interrupt.h>
Jon Masonf6a95a22016-07-07 19:08:57 -040015#include <linux/bcm47xx_nvram.h>
Russell King13bf7762017-02-07 15:02:58 -080016#include <linux/phy.h>
17#include <linux/phy_fixed.h>
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000018#include "bgmac.h"
19
Jon Masonf6a95a22016-07-07 19:08:57 -040020static bool bgmac_wait_value(struct bgmac *bgmac, u16 reg, u32 mask,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000021 u32 value, int timeout)
22{
23 u32 val;
24 int i;
25
26 for (i = 0; i < timeout / 10; i++) {
Jon Masonf6a95a22016-07-07 19:08:57 -040027 val = bgmac_read(bgmac, reg);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000028 if ((val & mask) == value)
29 return true;
30 udelay(10);
31 }
Jon Masonf6a95a22016-07-07 19:08:57 -040032 dev_err(bgmac->dev, "Timeout waiting for reg 0x%X\n", reg);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000033 return false;
34}
35
36/**************************************************
37 * DMA
38 **************************************************/
39
40static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
41{
42 u32 val;
43 int i;
44
45 if (!ring->mmio_base)
46 return;
47
48 /* Suspend DMA TX ring first.
49 * bgmac_wait_value doesn't support waiting for any of few values, so
50 * implement whole loop here.
51 */
52 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
53 BGMAC_DMA_TX_SUSPEND);
54 for (i = 0; i < 10000 / 10; i++) {
55 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
56 val &= BGMAC_DMA_TX_STAT;
57 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
58 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
59 val == BGMAC_DMA_TX_STAT_STOPPED) {
60 i = 0;
61 break;
62 }
63 udelay(10);
64 }
65 if (i)
Jon Masond00a8282016-07-07 19:08:53 -040066 dev_err(bgmac->dev, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
67 ring->mmio_base, val);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000068
69 /* Remove SUSPEND bit */
70 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
Jon Masonf6a95a22016-07-07 19:08:57 -040071 if (!bgmac_wait_value(bgmac,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000072 ring->mmio_base + BGMAC_DMA_TX_STATUS,
73 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
74 10000)) {
Jon Masond00a8282016-07-07 19:08:53 -040075 dev_warn(bgmac->dev, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
76 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000077 udelay(300);
78 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
79 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
Jon Masond00a8282016-07-07 19:08:53 -040080 dev_err(bgmac->dev, "Reset of DMA TX ring 0x%X failed\n",
81 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000082 }
83}
84
85static void bgmac_dma_tx_enable(struct bgmac *bgmac,
86 struct bgmac_dma_ring *ring)
87{
88 u32 ctl;
89
90 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
Jon Masondb791eb2016-07-07 19:08:56 -040091 if (bgmac->feature_flags & BGMAC_FEAT_TX_MASK_SETUP) {
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +010092 ctl &= ~BGMAC_DMA_TX_BL_MASK;
93 ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
94
95 ctl &= ~BGMAC_DMA_TX_MR_MASK;
96 ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
97
98 ctl &= ~BGMAC_DMA_TX_PC_MASK;
99 ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
100
101 ctl &= ~BGMAC_DMA_TX_PT_MASK;
102 ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
103 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000104 ctl |= BGMAC_DMA_TX_ENABLE;
105 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
106 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
107}
108
Felix Fietkau9cde9452015-03-23 12:35:37 +0100109static void
110bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
111 int i, int len, u32 ctl0)
112{
113 struct bgmac_slot_info *slot;
114 struct bgmac_dma_desc *dma_desc;
115 u32 ctl1;
116
Felix Fietkau29ba8772015-04-14 12:08:02 +0200117 if (i == BGMAC_TX_RING_SLOTS - 1)
Felix Fietkau9cde9452015-03-23 12:35:37 +0100118 ctl0 |= BGMAC_DESC_CTL0_EOT;
119
120 ctl1 = len & BGMAC_DESC_CTL1_LEN;
121
122 slot = &ring->slots[i];
123 dma_desc = &ring->cpu_base[i];
124 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
125 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
126 dma_desc->ctl0 = cpu_to_le32(ctl0);
127 dma_desc->ctl1 = cpu_to_le32(ctl1);
128}
129
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000130static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
131 struct bgmac_dma_ring *ring,
132 struct sk_buff *skb)
133{
Jon Masona0b68482016-07-07 19:08:54 -0400134 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000135 struct net_device *net_dev = bgmac->net_dev;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200136 int index = ring->end % BGMAC_TX_RING_SLOTS;
137 struct bgmac_slot_info *slot = &ring->slots[index];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100138 int nr_frags;
139 u32 flags;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100140 int i;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000141
142 if (skb->len > BGMAC_DESC_CTL1_LEN) {
Jon Masond00a8282016-07-07 19:08:53 -0400143 netdev_err(bgmac->net_dev, "Too long skb (%d)\n", skb->len);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100144 goto err_drop;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000145 }
146
Felix Fietkau9cde9452015-03-23 12:35:37 +0100147 if (skb->ip_summed == CHECKSUM_PARTIAL)
148 skb_checksum_help(skb);
149
150 nr_frags = skb_shinfo(skb)->nr_frags;
151
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200152 /* ring->end - ring->start will return the number of valid slots,
153 * even when ring->end overflows
154 */
155 if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
Jon Masond00a8282016-07-07 19:08:53 -0400156 netdev_err(bgmac->net_dev, "TX ring is full, queue should be stopped!\n");
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000157 netif_stop_queue(net_dev);
158 return NETDEV_TX_BUSY;
159 }
160
Felix Fietkau9cde9452015-03-23 12:35:37 +0100161 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000162 DMA_TO_DEVICE);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100163 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
164 goto err_dma_head;
165
166 flags = BGMAC_DESC_CTL0_SOF;
167 if (!nr_frags)
168 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
169
170 bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
171 flags = 0;
172
173 for (i = 0; i < nr_frags; i++) {
174 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
175 int len = skb_frag_size(frag);
176
177 index = (index + 1) % BGMAC_TX_RING_SLOTS;
178 slot = &ring->slots[index];
179 slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
180 len, DMA_TO_DEVICE);
181 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
182 goto err_dma;
183
184 if (i == nr_frags - 1)
185 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
186
187 bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000188 }
189
Felix Fietkau9cde9452015-03-23 12:35:37 +0100190 slot->skb = skb;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200191 ring->end += nr_frags + 1;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200192 netdev_sent_queue(net_dev, skb->len);
193
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000194 wmb();
195
196 /* Increase ring->end to point empty slot. We tell hardware the first
197 * slot it should *not* read.
198 */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000199 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
Rafał Miłecki99003032013-09-15 23:13:18 +0200200 ring->index_base +
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200201 (ring->end % BGMAC_TX_RING_SLOTS) *
202 sizeof(struct bgmac_dma_desc));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000203
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200204 if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000205 netif_stop_queue(net_dev);
206
207 return NETDEV_TX_OK;
208
Felix Fietkau9cde9452015-03-23 12:35:37 +0100209err_dma:
210 dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
211 DMA_TO_DEVICE);
212
Florian Fainellie86663c2016-07-15 15:42:52 -0700213 while (i-- > 0) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100214 int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
215 struct bgmac_slot_info *slot = &ring->slots[index];
216 u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
217 int len = ctl1 & BGMAC_DESC_CTL1_LEN;
218
219 dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
220 }
221
222err_dma_head:
Jon Masond00a8282016-07-07 19:08:53 -0400223 netdev_err(bgmac->net_dev, "Mapping error of skb on ring 0x%X\n",
224 ring->mmio_base);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100225
226err_drop:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000227 dev_kfree_skb(skb);
Florian Fainelli6d490f622016-06-07 15:06:15 -0700228 net_dev->stats.tx_dropped++;
229 net_dev->stats.tx_errors++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000230 return NETDEV_TX_OK;
231}
232
233/* Free transmitted packets */
234static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
235{
Jon Masona0b68482016-07-07 19:08:54 -0400236 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000237 int empty_slot;
238 bool freed = false;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200239 unsigned bytes_compl = 0, pkts_compl = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000240
241 /* The last slot that hardware didn't consume yet */
242 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
243 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200244 empty_slot -= ring->index_base;
245 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000246 empty_slot /= sizeof(struct bgmac_dma_desc);
247
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200248 while (ring->start != ring->end) {
249 int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
250 struct bgmac_slot_info *slot = &ring->slots[slot_idx];
Florian Fainellid2b13232016-06-23 14:23:12 -0700251 u32 ctl0, ctl1;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200252 int len;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100253
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200254 if (slot_idx == empty_slot)
255 break;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100256
Florian Fainellid2b13232016-06-23 14:23:12 -0700257 ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0);
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200258 ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
259 len = ctl1 & BGMAC_DESC_CTL1_LEN;
Florian Fainellid2b13232016-06-23 14:23:12 -0700260 if (ctl0 & BGMAC_DESC_CTL0_SOF)
Felix Fietkau9cde9452015-03-23 12:35:37 +0100261 /* Unmap no longer used buffer */
262 dma_unmap_single(dma_dev, slot->dma_addr, len,
263 DMA_TO_DEVICE);
264 else
265 dma_unmap_page(dma_dev, slot->dma_addr, len,
266 DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000267
268 if (slot->skb) {
Florian Fainelli6d490f622016-06-07 15:06:15 -0700269 bgmac->net_dev->stats.tx_bytes += slot->skb->len;
270 bgmac->net_dev->stats.tx_packets++;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200271 bytes_compl += slot->skb->len;
272 pkts_compl++;
273
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000274 /* Free memory! :) */
275 dev_kfree_skb(slot->skb);
276 slot->skb = NULL;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000277 }
278
Felix Fietkau9cde9452015-03-23 12:35:37 +0100279 slot->dma_addr = 0;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200280 ring->start++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000281 freed = true;
282 }
283
Felix Fietkau9cde9452015-03-23 12:35:37 +0100284 if (!pkts_compl)
285 return;
286
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200287 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
288
Felix Fietkau9cde9452015-03-23 12:35:37 +0100289 if (netif_queue_stopped(bgmac->net_dev))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000290 netif_wake_queue(bgmac->net_dev);
291}
292
293static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
294{
295 if (!ring->mmio_base)
296 return;
297
298 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
Jon Masonf6a95a22016-07-07 19:08:57 -0400299 if (!bgmac_wait_value(bgmac,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000300 ring->mmio_base + BGMAC_DMA_RX_STATUS,
301 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
302 10000))
Jon Masond00a8282016-07-07 19:08:53 -0400303 dev_err(bgmac->dev, "Reset of ring 0x%X RX failed\n",
304 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000305}
306
307static void bgmac_dma_rx_enable(struct bgmac *bgmac,
308 struct bgmac_dma_ring *ring)
309{
310 u32 ctl;
311
312 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
Andy Gospodarekfcdefcc2016-10-31 13:32:03 -0400313
314 /* preserve ONLY bits 16-17 from current hardware value */
315 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
316
Jon Masondb791eb2016-07-07 19:08:56 -0400317 if (bgmac->feature_flags & BGMAC_FEAT_RX_MASK_SETUP) {
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100318 ctl &= ~BGMAC_DMA_RX_BL_MASK;
319 ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
320
321 ctl &= ~BGMAC_DMA_RX_PC_MASK;
322 ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
323
324 ctl &= ~BGMAC_DMA_RX_PT_MASK;
325 ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
326 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000327 ctl |= BGMAC_DMA_RX_ENABLE;
328 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
329 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
330 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
331 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
332}
333
334static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
335 struct bgmac_slot_info *slot)
336{
Jon Masona0b68482016-07-07 19:08:54 -0400337 struct device *dma_dev = bgmac->dma_dev;
Nathan Hintzb757a622013-10-29 19:32:01 -0700338 dma_addr_t dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000339 struct bgmac_rx_header *rx;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100340 void *buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000341
342 /* Alloc skb */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100343 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
344 if (!buf)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000345 return -ENOMEM;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000346
347 /* Poison - if everything goes fine, hardware will overwrite it */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200348 rx = buf + BGMAC_RX_BUF_OFFSET;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000349 rx->len = cpu_to_le16(0xdead);
350 rx->flags = cpu_to_le16(0xbeef);
351
352 /* Map skb for the DMA */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200353 dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
354 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
Nathan Hintzb757a622013-10-29 19:32:01 -0700355 if (dma_mapping_error(dma_dev, dma_addr)) {
Jon Masond00a8282016-07-07 19:08:53 -0400356 netdev_err(bgmac->net_dev, "DMA mapping error\n");
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100357 put_page(virt_to_head_page(buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000358 return -ENOMEM;
359 }
Nathan Hintzb757a622013-10-29 19:32:01 -0700360
361 /* Update the slot */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100362 slot->buf = buf;
Nathan Hintzb757a622013-10-29 19:32:01 -0700363 slot->dma_addr = dma_addr;
364
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000365 return 0;
366}
367
Felix Fietkau4668ae12015-04-14 12:08:01 +0200368static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
369 struct bgmac_dma_ring *ring)
370{
371 dma_wmb();
372
373 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
374 ring->index_base +
375 ring->end * sizeof(struct bgmac_dma_desc));
376}
377
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100378static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
379 struct bgmac_dma_ring *ring, int desc_idx)
380{
381 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
382 u32 ctl0 = 0, ctl1 = 0;
383
Felix Fietkau29ba8772015-04-14 12:08:02 +0200384 if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100385 ctl0 |= BGMAC_DESC_CTL0_EOT;
386 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
387 /* Is there any BGMAC device that requires extension? */
388 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
389 * B43_DMA64_DCTL1_ADDREXT_MASK;
390 */
391
392 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
393 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
394 dma_desc->ctl0 = cpu_to_le32(ctl0);
395 dma_desc->ctl1 = cpu_to_le32(ctl1);
Felix Fietkau4668ae12015-04-14 12:08:01 +0200396
397 ring->end = desc_idx;
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100398}
399
Felix Fietkau56faacd2015-04-14 12:07:57 +0200400static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
401 struct bgmac_slot_info *slot)
402{
403 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
404
405 dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
406 DMA_FROM_DEVICE);
407 rx->len = cpu_to_le16(0xdead);
408 rx->flags = cpu_to_le16(0xbeef);
409 dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
410 DMA_FROM_DEVICE);
411}
412
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000413static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
414 int weight)
415{
416 u32 end_slot;
417 int handled = 0;
418
419 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
420 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200421 end_slot -= ring->index_base;
422 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000423 end_slot /= sizeof(struct bgmac_dma_desc);
424
Felix Fietkau4668ae12015-04-14 12:08:01 +0200425 while (ring->start != end_slot) {
Jon Masona0b68482016-07-07 19:08:54 -0400426 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000427 struct bgmac_slot_info *slot = &ring->slots[ring->start];
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200428 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100429 struct sk_buff *skb;
430 void *buf = slot->buf;
Felix Fietkau56faacd2015-04-14 12:07:57 +0200431 dma_addr_t dma_addr = slot->dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000432 u16 len, flags;
433
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100434 do {
Felix Fietkau56faacd2015-04-14 12:07:57 +0200435 /* Prepare new skb as replacement */
436 if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
437 bgmac_dma_rx_poison_buf(dma_dev, slot);
438 break;
439 }
440
441 /* Unmap buffer to make it accessible to the CPU */
442 dma_unmap_single(dma_dev, dma_addr,
443 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
444
445 /* Get info from the header */
446 len = le16_to_cpu(rx->len);
447 flags = le16_to_cpu(rx->flags);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100448
449 /* Check for poison and drop or pass the packet */
450 if (len == 0xdead && flags == 0xbeef) {
Jon Masond00a8282016-07-07 19:08:53 -0400451 netdev_err(bgmac->net_dev, "Found poisoned packet at slot %d, DMA issue!\n",
452 ring->start);
Felix Fietkau56faacd2015-04-14 12:07:57 +0200453 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700454 bgmac->net_dev->stats.rx_errors++;
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100455 break;
456 }
457
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200458 if (len > BGMAC_RX_ALLOC_SIZE) {
Jon Masond00a8282016-07-07 19:08:53 -0400459 netdev_err(bgmac->net_dev, "Found oversized packet at slot %d, DMA issue!\n",
460 ring->start);
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200461 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700462 bgmac->net_dev->stats.rx_length_errors++;
463 bgmac->net_dev->stats.rx_errors++;
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200464 break;
465 }
466
Hauke Mehrtens02e71122013-02-28 07:16:54 +0000467 /* Omit CRC. */
468 len -= ETH_FCS_LEN;
469
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100470 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
David S. Miller750afbf2016-01-15 16:07:13 -0500471 if (unlikely(!skb)) {
Jon Masond00a8282016-07-07 19:08:53 -0400472 netdev_err(bgmac->net_dev, "build_skb failed\n");
wangweidongf1640c32016-01-13 11:06:41 +0800473 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700474 bgmac->net_dev->stats.rx_errors++;
wangweidongf1640c32016-01-13 11:06:41 +0800475 break;
476 }
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200477 skb_put(skb, BGMAC_RX_FRAME_OFFSET +
478 BGMAC_RX_BUF_OFFSET + len);
479 skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
480 BGMAC_RX_BUF_OFFSET);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100481
482 skb_checksum_none_assert(skb);
483 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
Florian Fainelli6d490f622016-06-07 15:06:15 -0700484 bgmac->net_dev->stats.rx_bytes += len;
485 bgmac->net_dev->stats.rx_packets++;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100486 napi_gro_receive(&bgmac->napi, skb);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100487 handled++;
488 } while (0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000489
Felix Fietkau56faacd2015-04-14 12:07:57 +0200490 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
491
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000492 if (++ring->start >= BGMAC_RX_RING_SLOTS)
493 ring->start = 0;
494
495 if (handled >= weight) /* Should never be greater */
496 break;
497 }
498
Felix Fietkau4668ae12015-04-14 12:08:01 +0200499 bgmac_dma_rx_update_index(bgmac, ring);
500
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000501 return handled;
502}
503
504/* Does ring support unaligned addressing? */
505static bool bgmac_dma_unaligned(struct bgmac *bgmac,
506 struct bgmac_dma_ring *ring,
507 enum bgmac_dma_ring_type ring_type)
508{
509 switch (ring_type) {
510 case BGMAC_DMA_RING_TX:
511 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
512 0xff0);
513 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
514 return true;
515 break;
516 case BGMAC_DMA_RING_RX:
517 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
518 0xff0);
519 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
520 return true;
521 break;
522 }
523 return false;
524}
525
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100526static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
527 struct bgmac_dma_ring *ring)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000528{
Jon Masona0b68482016-07-07 19:08:54 -0400529 struct device *dma_dev = bgmac->dma_dev;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100530 struct bgmac_dma_desc *dma_desc = ring->cpu_base;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000531 struct bgmac_slot_info *slot;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000532 int i;
533
Felix Fietkau29ba8772015-04-14 12:08:02 +0200534 for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100535 int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN;
536
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000537 slot = &ring->slots[i];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100538 dev_kfree_skb(slot->skb);
539
540 if (!slot->dma_addr)
541 continue;
542
543 if (slot->skb)
544 dma_unmap_single(dma_dev, slot->dma_addr,
545 len, DMA_TO_DEVICE);
546 else
547 dma_unmap_page(dma_dev, slot->dma_addr,
548 len, DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000549 }
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100550}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000551
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100552static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
553 struct bgmac_dma_ring *ring)
554{
Jon Masona0b68482016-07-07 19:08:54 -0400555 struct device *dma_dev = bgmac->dma_dev;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100556 struct bgmac_slot_info *slot;
557 int i;
558
Felix Fietkau29ba8772015-04-14 12:08:02 +0200559 for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100560 slot = &ring->slots[i];
Felix Fietkau56faacd2015-04-14 12:07:57 +0200561 if (!slot->dma_addr)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100562 continue;
563
Felix Fietkau56faacd2015-04-14 12:07:57 +0200564 dma_unmap_single(dma_dev, slot->dma_addr,
565 BGMAC_RX_BUF_SIZE,
566 DMA_FROM_DEVICE);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100567 put_page(virt_to_head_page(slot->buf));
Felix Fietkau56faacd2015-04-14 12:07:57 +0200568 slot->dma_addr = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000569 }
570}
571
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100572static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
Felix Fietkau29ba8772015-04-14 12:08:02 +0200573 struct bgmac_dma_ring *ring,
574 int num_slots)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100575{
Jon Masona0b68482016-07-07 19:08:54 -0400576 struct device *dma_dev = bgmac->dma_dev;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100577 int size;
578
579 if (!ring->cpu_base)
580 return;
581
582 /* Free ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200583 size = num_slots * sizeof(struct bgmac_dma_desc);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100584 dma_free_coherent(dma_dev, size, ring->cpu_base,
585 ring->dma_base);
586}
587
Felix Fietkau74b6f292015-04-14 12:08:00 +0200588static void bgmac_dma_cleanup(struct bgmac *bgmac)
589{
590 int i;
591
592 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
593 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
594
595 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
596 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
597}
598
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000599static void bgmac_dma_free(struct bgmac *bgmac)
600{
601 int i;
602
Felix Fietkau74b6f292015-04-14 12:08:00 +0200603 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200604 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
605 BGMAC_TX_RING_SLOTS);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200606
607 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200608 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
609 BGMAC_RX_RING_SLOTS);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000610}
611
612static int bgmac_dma_alloc(struct bgmac *bgmac)
613{
Jon Masona0b68482016-07-07 19:08:54 -0400614 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000615 struct bgmac_dma_ring *ring;
616 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
617 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
618 int size; /* ring size: different for Tx and Rx */
619 int err;
620 int i;
621
622 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
623 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
624
Abhishek Shaha163bdb2017-07-14 00:34:08 +0530625 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
626 if (!(bgmac_idm_read(bgmac, BCMA_IOST) & BCMA_IOST_DMA64)) {
627 dev_err(bgmac->dev, "Core does not report 64-bit DMA\n");
628 return -ENOTSUPP;
629 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000630 }
631
632 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
633 ring = &bgmac->tx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000634 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000635
636 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200637 size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000638 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
639 &ring->dma_base,
640 GFP_KERNEL);
641 if (!ring->cpu_base) {
Jon Masond00a8282016-07-07 19:08:53 -0400642 dev_err(bgmac->dev, "Allocation of TX ring 0x%X failed\n",
643 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000644 goto err_dma_free;
645 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000646
Rafał Miłecki99003032013-09-15 23:13:18 +0200647 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
648 BGMAC_DMA_RING_TX);
649 if (ring->unaligned)
650 ring->index_base = lower_32_bits(ring->dma_base);
651 else
652 ring->index_base = 0;
653
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000654 /* No need to alloc TX slots yet */
655 }
656
657 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
658 ring = &bgmac->rx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000659 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000660
661 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200662 size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000663 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
664 &ring->dma_base,
665 GFP_KERNEL);
666 if (!ring->cpu_base) {
Jon Masond00a8282016-07-07 19:08:53 -0400667 dev_err(bgmac->dev, "Allocation of RX ring 0x%X failed\n",
668 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000669 err = -ENOMEM;
670 goto err_dma_free;
671 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000672
Rafał Miłecki99003032013-09-15 23:13:18 +0200673 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
674 BGMAC_DMA_RING_RX);
675 if (ring->unaligned)
676 ring->index_base = lower_32_bits(ring->dma_base);
677 else
678 ring->index_base = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000679 }
680
681 return 0;
682
683err_dma_free:
684 bgmac_dma_free(bgmac);
685 return -ENOMEM;
686}
687
Felix Fietkau74b6f292015-04-14 12:08:00 +0200688static int bgmac_dma_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000689{
690 struct bgmac_dma_ring *ring;
Felix Fietkau74b6f292015-04-14 12:08:00 +0200691 int i, err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000692
693 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
694 ring = &bgmac->tx_ring[i];
695
Rafał Miłecki99003032013-09-15 23:13:18 +0200696 if (!ring->unaligned)
697 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000698 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
699 lower_32_bits(ring->dma_base));
700 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
701 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200702 if (ring->unaligned)
703 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000704
705 ring->start = 0;
706 ring->end = 0; /* Points the slot that should *not* be read */
707 }
708
709 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000710 int j;
711
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000712 ring = &bgmac->rx_ring[i];
713
Rafał Miłecki99003032013-09-15 23:13:18 +0200714 if (!ring->unaligned)
715 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000716 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
717 lower_32_bits(ring->dma_base));
718 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
719 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200720 if (ring->unaligned)
721 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000722
Felix Fietkau4668ae12015-04-14 12:08:01 +0200723 ring->start = 0;
724 ring->end = 0;
Felix Fietkau29ba8772015-04-14 12:08:02 +0200725 for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
Felix Fietkau74b6f292015-04-14 12:08:00 +0200726 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
727 if (err)
728 goto error;
729
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100730 bgmac_dma_rx_setup_desc(bgmac, ring, j);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200731 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000732
Felix Fietkau4668ae12015-04-14 12:08:01 +0200733 bgmac_dma_rx_update_index(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000734 }
Felix Fietkau74b6f292015-04-14 12:08:00 +0200735
736 return 0;
737
738error:
739 bgmac_dma_cleanup(bgmac);
740 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000741}
742
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000743
744/**************************************************
745 * Chip ops
746 **************************************************/
747
748/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
749 * nothing to change? Try if after stabilizng driver.
750 */
751static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
752 bool force)
753{
754 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
755 u32 new_val = (cmdcfg & mask) | set;
Jon Masondb791eb2016-07-07 19:08:56 -0400756 u32 cmdcfg_sr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000757
Jon Masondb791eb2016-07-07 19:08:56 -0400758 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
759 cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
760 else
761 cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
762
763 bgmac_set(bgmac, BGMAC_CMDCFG, cmdcfg_sr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000764 udelay(2);
765
766 if (new_val != cmdcfg || force)
767 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
768
Jon Masondb791eb2016-07-07 19:08:56 -0400769 bgmac_mask(bgmac, BGMAC_CMDCFG, ~cmdcfg_sr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000770 udelay(2);
771}
772
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000773static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
774{
775 u32 tmp;
776
777 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
778 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
779 tmp = (addr[4] << 8) | addr[5];
780 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
781}
782
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000783static void bgmac_set_rx_mode(struct net_device *net_dev)
784{
785 struct bgmac *bgmac = netdev_priv(net_dev);
786
787 if (net_dev->flags & IFF_PROMISC)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000788 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000789 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000790 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000791}
792
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000793#if 0 /* We don't use that regs yet */
794static void bgmac_chip_stats_update(struct bgmac *bgmac)
795{
796 int i;
797
Jon Masondb791eb2016-07-07 19:08:56 -0400798 if (!(bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000799 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
800 bgmac->mib_tx_regs[i] =
801 bgmac_read(bgmac,
802 BGMAC_TX_GOOD_OCTETS + (i * 4));
803 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
804 bgmac->mib_rx_regs[i] =
805 bgmac_read(bgmac,
806 BGMAC_RX_GOOD_OCTETS + (i * 4));
807 }
808
809 /* TODO: what else? how to handle BCM4706? Specs are needed */
810}
811#endif
812
813static void bgmac_clear_mib(struct bgmac *bgmac)
814{
815 int i;
816
Jon Masondb791eb2016-07-07 19:08:56 -0400817 if (bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000818 return;
819
820 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
821 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
822 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
823 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
824 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
825}
826
827/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100828static void bgmac_mac_speed(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000829{
830 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
831 u32 set = 0;
832
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100833 switch (bgmac->mac_speed) {
834 case SPEED_10:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000835 set |= BGMAC_CMDCFG_ES_10;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100836 break;
837 case SPEED_100:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000838 set |= BGMAC_CMDCFG_ES_100;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100839 break;
840 case SPEED_1000:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000841 set |= BGMAC_CMDCFG_ES_1000;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100842 break;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100843 case SPEED_2500:
844 set |= BGMAC_CMDCFG_ES_2500;
845 break;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100846 default:
Jon Masond00a8282016-07-07 19:08:53 -0400847 dev_err(bgmac->dev, "Unsupported speed: %d\n",
848 bgmac->mac_speed);
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100849 }
850
851 if (bgmac->mac_duplex == DUPLEX_HALF)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000852 set |= BGMAC_CMDCFG_HD;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100853
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000854 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
855}
856
857static void bgmac_miiconfig(struct bgmac *bgmac)
858{
Jon Masondb791eb2016-07-07 19:08:56 -0400859 if (bgmac->feature_flags & BGMAC_FEAT_FORCE_SPEED_2500) {
Abhishek Shaha163bdb2017-07-14 00:34:08 +0530860 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
861 bgmac_idm_write(bgmac, BCMA_IOCTL,
862 bgmac_idm_read(bgmac, BCMA_IOCTL) |
863 0x40 | BGMAC_BCMA_IOCTL_SW_CLKEN);
864 }
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100865 bgmac->mac_speed = SPEED_2500;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100866 bgmac->mac_duplex = DUPLEX_FULL;
867 bgmac_mac_speed(bgmac);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100868 } else {
Jon Masondb791eb2016-07-07 19:08:56 -0400869 u8 imode;
870
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100871 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
872 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
873 if (imode == 0 || imode == 1) {
874 bgmac->mac_speed = SPEED_100;
875 bgmac->mac_duplex = DUPLEX_FULL;
876 bgmac_mac_speed(bgmac);
877 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000878 }
879}
880
Abhishek Shaha163bdb2017-07-14 00:34:08 +0530881static void bgmac_chip_reset_idm_config(struct bgmac *bgmac)
882{
883 u32 iost;
884
885 iost = bgmac_idm_read(bgmac, BCMA_IOST);
886 if (bgmac->feature_flags & BGMAC_FEAT_IOST_ATTACHED)
887 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
888
889 /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
890 if (!(bgmac->feature_flags & BGMAC_FEAT_NO_RESET)) {
891 u32 flags = 0;
892
893 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
894 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
895 if (!bgmac->has_robosw)
896 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
897 }
898 bgmac_clk_enable(bgmac, flags);
899 }
900
901 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
902 bgmac_idm_write(bgmac, BCMA_IOCTL,
903 bgmac_idm_read(bgmac, BCMA_IOCTL) &
904 ~BGMAC_BCMA_IOCTL_SW_RESET);
905}
906
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000907/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
908static void bgmac_chip_reset(struct bgmac *bgmac)
909{
Jon Masondb791eb2016-07-07 19:08:56 -0400910 u32 cmdcfg_sr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000911 int i;
912
Jon Masonf6a95a22016-07-07 19:08:57 -0400913 if (bgmac_clk_enabled(bgmac)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000914 if (!bgmac->stats_grabbed) {
915 /* bgmac_chip_stats_update(bgmac); */
916 bgmac->stats_grabbed = true;
917 }
918
919 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
920 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
921
922 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
923 udelay(1);
924
925 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
926 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
927
928 /* TODO: Clear software multicast filter list */
929 }
930
Abhishek Shaha163bdb2017-07-14 00:34:08 +0530931 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK))
932 bgmac_chip_reset_idm_config(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000933
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100934 /* Request Misc PLL for corerev > 2 */
Jon Masondb791eb2016-07-07 19:08:56 -0400935 if (bgmac->feature_flags & BGMAC_FEAT_MISC_PLL_REQ) {
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100936 bgmac_set(bgmac, BCMA_CLKCTLST,
937 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
Jon Masonf6a95a22016-07-07 19:08:57 -0400938 bgmac_wait_value(bgmac, BCMA_CLKCTLST,
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100939 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
940 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000941 1000);
942 }
943
Jon Masondb791eb2016-07-07 19:08:56 -0400944 if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_PHY) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000945 u8 et_swtype = 0;
946 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
Rafał Miłecki6a391e72013-09-15 00:22:47 +0200947 BGMAC_CHIPCTL_1_IF_TYPE_MII;
Hauke Mehrtens36472682013-09-15 22:49:08 +0200948 char buf[4];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000949
Hauke Mehrtens36472682013-09-15 22:49:08 +0200950 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000951 if (kstrtou8(buf, 0, &et_swtype))
Jon Masond00a8282016-07-07 19:08:53 -0400952 dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
953 buf);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000954 et_swtype &= 0x0f;
955 et_swtype <<= 4;
956 sw_type = et_swtype;
Jon Masondb791eb2016-07-07 19:08:56 -0400957 } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_EPHYRMII) {
Rafał Miłeckie2d8f642016-08-17 23:11:52 +0200958 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RMII |
959 BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Jon Masondb791eb2016-07-07 19:08:56 -0400960 } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_RGMII) {
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +0000961 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
962 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000963 }
Jon Masonf6a95a22016-07-07 19:08:57 -0400964 bgmac_cco_ctl_maskset(bgmac, 1, ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
965 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
966 sw_type);
Rafał Miłecki1cb94db2016-08-17 23:00:30 +0200967 } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE) {
968 u32 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_MII |
969 BGMAC_CHIPCTL_4_SW_TYPE_EPHY;
970 u8 et_swtype = 0;
971 char buf[4];
972
973 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
974 if (kstrtou8(buf, 0, &et_swtype))
975 dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
976 buf);
977 sw_type = (et_swtype & 0x0f) << 12;
978 } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII) {
979 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_RGMII |
980 BGMAC_CHIPCTL_4_SW_TYPE_RGMII;
981 }
982 bgmac_cco_ctl_maskset(bgmac, 4, ~(BGMAC_CHIPCTL_4_IF_TYPE_MASK |
983 BGMAC_CHIPCTL_4_SW_TYPE_MASK),
984 sw_type);
985 } else if (bgmac->feature_flags & BGMAC_FEAT_CC7_IF_TYPE_RGMII) {
986 bgmac_cco_ctl_maskset(bgmac, 7, ~BGMAC_CHIPCTL_7_IF_TYPE_MASK,
987 BGMAC_CHIPCTL_7_IF_TYPE_RGMII);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000988 }
989
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000990 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
991 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
992 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
993 * be keps until taking MAC out of the reset.
994 */
Jon Masondb791eb2016-07-07 19:08:56 -0400995 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
996 cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
997 else
998 cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
999
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001000 bgmac_cmdcfg_maskset(bgmac,
1001 ~(BGMAC_CMDCFG_TE |
1002 BGMAC_CMDCFG_RE |
1003 BGMAC_CMDCFG_RPI |
1004 BGMAC_CMDCFG_TAI |
1005 BGMAC_CMDCFG_HD |
1006 BGMAC_CMDCFG_ML |
1007 BGMAC_CMDCFG_CFE |
1008 BGMAC_CMDCFG_RL |
1009 BGMAC_CMDCFG_RED |
1010 BGMAC_CMDCFG_PE |
1011 BGMAC_CMDCFG_TPI |
1012 BGMAC_CMDCFG_PAD_EN |
1013 BGMAC_CMDCFG_PF),
1014 BGMAC_CMDCFG_PROM |
1015 BGMAC_CMDCFG_NLC |
1016 BGMAC_CMDCFG_CFE |
Jon Masondb791eb2016-07-07 19:08:56 -04001017 cmdcfg_sr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001018 false);
Rafał Miłeckid4699622013-12-11 07:44:14 +01001019 bgmac->mac_speed = SPEED_UNKNOWN;
1020 bgmac->mac_duplex = DUPLEX_UNKNOWN;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001021
1022 bgmac_clear_mib(bgmac);
Jon Masondb791eb2016-07-07 19:08:56 -04001023 if (bgmac->feature_flags & BGMAC_FEAT_CMN_PHY_CTL)
Jon Masonf6a95a22016-07-07 19:08:57 -04001024 bgmac_cmn_maskset32(bgmac, BCMA_GMAC_CMN_PHY_CTL, ~0,
1025 BCMA_GMAC_CMN_PC_MTE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001026 else
1027 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1028 bgmac_miiconfig(bgmac);
Jon Mason55954f32016-07-07 19:08:55 -04001029 if (bgmac->mii_bus)
1030 bgmac->mii_bus->reset(bgmac->mii_bus);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001031
Hauke Mehrtens49a467b2013-09-29 13:54:58 +02001032 netdev_reset_queue(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001033}
1034
1035static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1036{
1037 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1038}
1039
1040static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1041{
1042 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
Nathan Hintz41608152013-02-13 19:14:10 +00001043 bgmac_read(bgmac, BGMAC_INT_MASK);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001044}
1045
1046/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1047static void bgmac_enable(struct bgmac *bgmac)
1048{
Jon Masondb791eb2016-07-07 19:08:56 -04001049 u32 cmdcfg_sr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001050 u32 cmdcfg;
1051 u32 mode;
Jon Masondb791eb2016-07-07 19:08:56 -04001052
1053 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
1054 cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
1055 else
1056 cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001057
1058 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
1059 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
Jon Masondb791eb2016-07-07 19:08:56 -04001060 cmdcfg_sr, true);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001061 udelay(2);
1062 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1063 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1064
1065 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1066 BGMAC_DS_MM_SHIFT;
Rafał Miłeckicdb26d32016-11-07 13:53:27 +01001067 if (bgmac->feature_flags & BGMAC_FEAT_CLKCTLST || mode != 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001068 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
Rafał Miłeckicdb26d32016-11-07 13:53:27 +01001069 if (!(bgmac->feature_flags & BGMAC_FEAT_CLKCTLST) && mode == 2)
Jon Masonf6a95a22016-07-07 19:08:57 -04001070 bgmac_cco_ctl_maskset(bgmac, 1, ~0,
1071 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001072
Jon Masondb791eb2016-07-07 19:08:56 -04001073 if (bgmac->feature_flags & (BGMAC_FEAT_FLW_CTRL1 |
1074 BGMAC_FEAT_FLW_CTRL2)) {
1075 u32 fl_ctl;
1076
1077 if (bgmac->feature_flags & BGMAC_FEAT_FLW_CTRL1)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001078 fl_ctl = 0x2300e1;
Jon Masondb791eb2016-07-07 19:08:56 -04001079 else
1080 fl_ctl = 0x03cb04cb;
1081
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001082 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1083 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001084 }
1085
Jon Masondb791eb2016-07-07 19:08:56 -04001086 if (bgmac->feature_flags & BGMAC_FEAT_SET_RXQ_CLK) {
1087 u32 rxq_ctl;
1088 u16 bp_clk;
1089 u8 mdp;
1090
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001091 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1092 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
Jon Masonf6a95a22016-07-07 19:08:57 -04001093 bp_clk = bgmac_get_bus_clock(bgmac) / 1000000;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001094 mdp = (bp_clk * 128 / 1000) - 3;
1095 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1096 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1097 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001098}
1099
1100/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001101static void bgmac_chip_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001102{
Jon Masondd5c5d02016-11-04 01:11:01 -04001103 /* Clear any erroneously pending interrupts */
1104 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
1105
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001106 /* 1 interrupt per received frame */
1107 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1108
1109 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1110 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1111
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001112 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001113
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001114 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001115
1116 if (bgmac->loopback)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001117 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001118 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001119 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001120
1121 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1122
Felix Fietkau74b6f292015-04-14 12:08:00 +02001123 bgmac_chip_intrs_on(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001124
1125 bgmac_enable(bgmac);
1126}
1127
1128static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1129{
1130 struct bgmac *bgmac = netdev_priv(dev_id);
1131
1132 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1133 int_status &= bgmac->int_mask;
1134
1135 if (!int_status)
1136 return IRQ_NONE;
1137
Felix Fietkaueb64e292015-04-14 12:07:55 +02001138 int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1139 if (int_status)
Jon Masond00a8282016-07-07 19:08:53 -04001140 dev_err(bgmac->dev, "Unknown IRQs: 0x%08X\n", int_status);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001141
1142 /* Disable new interrupts until handling existing ones */
1143 bgmac_chip_intrs_off(bgmac);
1144
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001145 napi_schedule(&bgmac->napi);
1146
1147 return IRQ_HANDLED;
1148}
1149
1150static int bgmac_poll(struct napi_struct *napi, int weight)
1151{
1152 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001153 int handled = 0;
1154
Felix Fietkaueb64e292015-04-14 12:07:55 +02001155 /* Ack */
1156 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001157
Felix Fietkaueb64e292015-04-14 12:07:55 +02001158 bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1159 handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001160
Felix Fietkaueb64e292015-04-14 12:07:55 +02001161 /* Poll again if more events arrived in the meantime */
1162 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
Rafał Miłeckie5802672015-04-23 20:56:29 +02001163 return weight;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001164
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001165 if (handled < weight) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08001166 napi_complete_done(napi, handled);
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001167 bgmac_chip_intrs_on(bgmac);
1168 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001169
1170 return handled;
1171}
1172
1173/**************************************************
1174 * net_device_ops
1175 **************************************************/
1176
1177static int bgmac_open(struct net_device *net_dev)
1178{
1179 struct bgmac *bgmac = netdev_priv(net_dev);
1180 int err = 0;
1181
1182 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001183
1184 err = bgmac_dma_init(bgmac);
1185 if (err)
1186 return err;
1187
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001188 /* Specs say about reclaiming rings here, but we do that in DMA init */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001189 bgmac_chip_init(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001190
Jon Masonf6a95a22016-07-07 19:08:57 -04001191 err = request_irq(bgmac->irq, bgmac_interrupt, IRQF_SHARED,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001192 KBUILD_MODNAME, net_dev);
1193 if (err < 0) {
Jon Masond00a8282016-07-07 19:08:53 -04001194 dev_err(bgmac->dev, "IRQ request error: %d!\n", err);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001195 bgmac_dma_cleanup(bgmac);
1196 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001197 }
1198 napi_enable(&bgmac->napi);
1199
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001200 phy_start(net_dev->phydev);
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001201
Florian Fainellic3897f22016-06-23 14:25:32 -07001202 netif_start_queue(net_dev);
1203
Felix Fietkau74b6f292015-04-14 12:08:00 +02001204 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001205}
1206
1207static int bgmac_stop(struct net_device *net_dev)
1208{
1209 struct bgmac *bgmac = netdev_priv(net_dev);
1210
1211 netif_carrier_off(net_dev);
1212
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001213 phy_stop(net_dev->phydev);
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001214
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001215 napi_disable(&bgmac->napi);
1216 bgmac_chip_intrs_off(bgmac);
Jon Masonf6a95a22016-07-07 19:08:57 -04001217 free_irq(bgmac->irq, net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001218
1219 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001220 bgmac_dma_cleanup(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001221
1222 return 0;
1223}
1224
1225static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1226 struct net_device *net_dev)
1227{
1228 struct bgmac *bgmac = netdev_priv(net_dev);
1229 struct bgmac_dma_ring *ring;
1230
1231 /* No QOS support yet */
1232 ring = &bgmac->tx_ring[0];
1233 return bgmac_dma_tx_add(bgmac, ring, skb);
1234}
1235
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001236static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1237{
1238 struct bgmac *bgmac = netdev_priv(net_dev);
Hari Vyasfa422452017-03-02 17:59:57 -05001239 struct sockaddr *sa = addr;
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001240 int ret;
1241
1242 ret = eth_prepare_mac_addr_change(net_dev, addr);
1243 if (ret < 0)
1244 return ret;
Hari Vyasfa422452017-03-02 17:59:57 -05001245
1246 ether_addr_copy(net_dev->dev_addr, sa->sa_data);
1247 bgmac_write_mac_address(bgmac, net_dev->dev_addr);
1248
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001249 eth_commit_mac_addr_change(net_dev, addr);
1250 return 0;
1251}
1252
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001253static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1254{
Hauke Mehrtens69c58852013-12-20 15:34:45 +01001255 if (!netif_running(net_dev))
1256 return -EINVAL;
1257
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001258 return phy_mii_ioctl(net_dev->phydev, ifr, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001259}
1260
1261static const struct net_device_ops bgmac_netdev_ops = {
1262 .ndo_open = bgmac_open,
1263 .ndo_stop = bgmac_stop,
1264 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001265 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001266 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001267 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001268 .ndo_do_ioctl = bgmac_ioctl,
1269};
1270
1271/**************************************************
1272 * ethtool_ops
1273 **************************************************/
1274
Florian Fainellif6613d42016-06-07 15:06:14 -07001275struct bgmac_stat {
1276 u8 size;
1277 u32 offset;
1278 const char *name;
1279};
1280
1281static struct bgmac_stat bgmac_get_strings_stats[] = {
1282 { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
1283 { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
1284 { 8, BGMAC_TX_OCTETS, "tx_octets" },
1285 { 4, BGMAC_TX_PKTS, "tx_pkts" },
1286 { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
1287 { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
1288 { 4, BGMAC_TX_LEN_64, "tx_64" },
1289 { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
1290 { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
1291 { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
1292 { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
1293 { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
1294 { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
1295 { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
1296 { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
1297 { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
1298 { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
1299 { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
1300 { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
1301 { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
1302 { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
1303 { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
1304 { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
1305 { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
1306 { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
1307 { 4, BGMAC_TX_DEFERED, "tx_defered" },
1308 { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
1309 { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
1310 { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
1311 { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
1312 { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
1313 { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
1314 { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
1315 { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
1316 { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
1317 { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
1318 { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
1319 { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
1320 { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
1321 { 8, BGMAC_RX_OCTETS, "rx_octets" },
1322 { 4, BGMAC_RX_PKTS, "rx_pkts" },
1323 { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
1324 { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
1325 { 4, BGMAC_RX_LEN_64, "rx_64" },
1326 { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
1327 { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
1328 { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
1329 { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
1330 { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
1331 { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
1332 { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
1333 { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
1334 { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
1335 { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
1336 { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
1337 { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
1338 { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
1339 { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
1340 { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
1341 { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
1342 { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
1343 { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
1344 { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
1345 { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
1346 { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
1347 { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
1348};
1349
1350#define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
1351
1352static int bgmac_get_sset_count(struct net_device *dev, int string_set)
1353{
1354 switch (string_set) {
1355 case ETH_SS_STATS:
1356 return BGMAC_STATS_LEN;
1357 }
1358
1359 return -EOPNOTSUPP;
1360}
1361
1362static void bgmac_get_strings(struct net_device *dev, u32 stringset,
1363 u8 *data)
1364{
1365 int i;
1366
1367 if (stringset != ETH_SS_STATS)
1368 return;
1369
1370 for (i = 0; i < BGMAC_STATS_LEN; i++)
1371 strlcpy(data + i * ETH_GSTRING_LEN,
1372 bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
1373}
1374
1375static void bgmac_get_ethtool_stats(struct net_device *dev,
1376 struct ethtool_stats *ss, uint64_t *data)
1377{
1378 struct bgmac *bgmac = netdev_priv(dev);
1379 const struct bgmac_stat *s;
1380 unsigned int i;
1381 u64 val;
1382
1383 if (!netif_running(dev))
1384 return;
1385
1386 for (i = 0; i < BGMAC_STATS_LEN; i++) {
1387 s = &bgmac_get_strings_stats[i];
1388 val = 0;
1389 if (s->size == 8)
1390 val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
1391 val |= bgmac_read(bgmac, s->offset);
1392 data[i] = val;
1393 }
1394}
1395
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001396static void bgmac_get_drvinfo(struct net_device *net_dev,
1397 struct ethtool_drvinfo *info)
1398{
1399 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jon Masonf6a95a22016-07-07 19:08:57 -04001400 strlcpy(info->bus_info, "AXI", sizeof(info->bus_info));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001401}
1402
1403static const struct ethtool_ops bgmac_ethtool_ops = {
Florian Fainellif6613d42016-06-07 15:06:14 -07001404 .get_strings = bgmac_get_strings,
1405 .get_sset_count = bgmac_get_sset_count,
1406 .get_ethtool_stats = bgmac_get_ethtool_stats,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001407 .get_drvinfo = bgmac_get_drvinfo,
Philippe Reynes904632a2016-06-19 22:37:06 +02001408 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1409 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001410};
1411
1412/**************************************************
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001413 * MII
1414 **************************************************/
1415
Jon Mason1676aba5ef2016-11-04 01:11:00 -04001416void bgmac_adjust_link(struct net_device *net_dev)
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001417{
1418 struct bgmac *bgmac = netdev_priv(net_dev);
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001419 struct phy_device *phy_dev = net_dev->phydev;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001420 bool update = false;
1421
1422 if (phy_dev->link) {
1423 if (phy_dev->speed != bgmac->mac_speed) {
1424 bgmac->mac_speed = phy_dev->speed;
1425 update = true;
1426 }
1427
1428 if (phy_dev->duplex != bgmac->mac_duplex) {
1429 bgmac->mac_duplex = phy_dev->duplex;
1430 update = true;
1431 }
1432 }
1433
1434 if (update) {
1435 bgmac_mac_speed(bgmac);
1436 phy_print_status(phy_dev);
1437 }
1438}
Jon Mason1676aba5ef2016-11-04 01:11:00 -04001439EXPORT_SYMBOL_GPL(bgmac_adjust_link);
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001440
Jon Mason1676aba5ef2016-11-04 01:11:00 -04001441int bgmac_phy_connect_direct(struct bgmac *bgmac)
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001442{
1443 struct fixed_phy_status fphy_status = {
1444 .link = 1,
1445 .speed = SPEED_1000,
1446 .duplex = DUPLEX_FULL,
1447 };
1448 struct phy_device *phy_dev;
1449 int err;
1450
Fabio Estevam4db78d32015-09-02 13:25:59 -03001451 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001452 if (!phy_dev || IS_ERR(phy_dev)) {
Jon Masond00a8282016-07-07 19:08:53 -04001453 dev_err(bgmac->dev, "Failed to register fixed PHY device\n");
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001454 return -ENODEV;
1455 }
1456
1457 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1458 PHY_INTERFACE_MODE_MII);
1459 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001460 dev_err(bgmac->dev, "Connecting PHY failed\n");
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001461 return err;
1462 }
1463
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001464 return err;
1465}
Jon Mason1676aba5ef2016-11-04 01:11:00 -04001466EXPORT_SYMBOL_GPL(bgmac_phy_connect_direct);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001467
Rafał Miłecki34a51022017-01-31 19:37:54 +01001468struct bgmac *bgmac_alloc(struct device *dev)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001469{
1470 struct net_device *net_dev;
1471 struct bgmac *bgmac;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001472
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001473 /* Allocation and references */
Rafał Miłecki34a51022017-01-31 19:37:54 +01001474 net_dev = devm_alloc_etherdev(dev, sizeof(*bgmac));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001475 if (!net_dev)
Rafał Miłecki34a51022017-01-31 19:37:54 +01001476 return NULL;
Jon Masonf6a95a22016-07-07 19:08:57 -04001477
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001478 net_dev->netdev_ops = &bgmac_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001479 net_dev->ethtool_ops = &bgmac_ethtool_ops;
Rafał Miłecki34a51022017-01-31 19:37:54 +01001480
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001481 bgmac = netdev_priv(net_dev);
Rafał Miłecki34a51022017-01-31 19:37:54 +01001482 bgmac->dev = dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001483 bgmac->net_dev = net_dev;
Rafał Miłecki34a51022017-01-31 19:37:54 +01001484
1485 return bgmac;
1486}
1487EXPORT_SYMBOL_GPL(bgmac_alloc);
1488
1489int bgmac_enet_probe(struct bgmac *bgmac)
1490{
1491 struct net_device *net_dev = bgmac->net_dev;
1492 int err;
1493
Jon Masonf6a95a22016-07-07 19:08:57 -04001494 net_dev->irq = bgmac->irq;
1495 SET_NETDEV_DEV(net_dev, bgmac->dev);
Joey Zhongf3537b32017-02-28 13:51:01 -05001496 dev_set_drvdata(bgmac->dev, bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001497
Tobias Klauser6850f8b2017-02-16 15:11:19 +01001498 if (!is_valid_ether_addr(net_dev->dev_addr)) {
Jon Masonf6a95a22016-07-07 19:08:57 -04001499 dev_err(bgmac->dev, "Invalid MAC addr: %pM\n",
Tobias Klauser6850f8b2017-02-16 15:11:19 +01001500 net_dev->dev_addr);
1501 eth_hw_addr_random(net_dev);
Jon Masonf6a95a22016-07-07 19:08:57 -04001502 dev_warn(bgmac->dev, "Using random MAC: %pM\n",
Tobias Klauser6850f8b2017-02-16 15:11:19 +01001503 net_dev->dev_addr);
Jon Masonf6a95a22016-07-07 19:08:57 -04001504 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001505
Jon Masonf6a95a22016-07-07 19:08:57 -04001506 /* This (reset &) enable is not preset in specs or reference driver but
1507 * Broadcom does it in arch PCI code when enabling fake PCI device.
1508 */
1509 bgmac_clk_enable(bgmac, 0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001510
Rafał Miłecki1cb94db2016-08-17 23:00:30 +02001511 /* This seems to be fixing IRQ by assigning OOB #6 to the core */
Abhishek Shaha163bdb2017-07-14 00:34:08 +05301512 if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
1513 if (bgmac->feature_flags & BGMAC_FEAT_IRQ_ID_OOB_6)
1514 bgmac_idm_write(bgmac, BCMA_OOB_SEL_OUT_A30, 0x86);
1515 }
Rafał Miłecki1cb94db2016-08-17 23:00:30 +02001516
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001517 bgmac_chip_reset(bgmac);
1518
1519 err = bgmac_dma_alloc(bgmac);
1520 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001521 dev_err(bgmac->dev, "Unable to alloc memory for DMA\n");
Rafał Miłecki34a51022017-01-31 19:37:54 +01001522 goto err_out;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001523 }
1524
1525 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
Ralf Baechleedb15d82013-02-21 16:16:55 +01001526 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001527 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1528
Hauke Mehrtens62166422015-01-18 19:49:58 +01001529 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1530
Jon Mason1676aba5ef2016-11-04 01:11:00 -04001531 err = bgmac_phy_connect(bgmac);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001532 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001533 dev_err(bgmac->dev, "Cannot connect to phy\n");
Jon Masonf6a95a22016-07-07 19:08:57 -04001534 goto err_dma_free;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001535 }
1536
Felix Fietkau9cde9452015-03-23 12:35:37 +01001537 net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1538 net_dev->hw_features = net_dev->features;
1539 net_dev->vlan_features = net_dev->features;
1540
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001541 err = register_netdev(bgmac->net_dev);
1542 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001543 dev_err(bgmac->dev, "Cannot register net device\n");
Jon Mason55954f32016-07-07 19:08:55 -04001544 goto err_phy_disconnect;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001545 }
1546
1547 netif_carrier_off(net_dev);
1548
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001549 return 0;
1550
Jon Mason55954f32016-07-07 19:08:55 -04001551err_phy_disconnect:
1552 phy_disconnect(net_dev->phydev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001553err_dma_free:
1554 bgmac_dma_free(bgmac);
Rafał Miłecki34a51022017-01-31 19:37:54 +01001555err_out:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001556
1557 return err;
1558}
Jon Masonf6a95a22016-07-07 19:08:57 -04001559EXPORT_SYMBOL_GPL(bgmac_enet_probe);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001560
Jon Masonf6a95a22016-07-07 19:08:57 -04001561void bgmac_enet_remove(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001562{
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001563 unregister_netdev(bgmac->net_dev);
Jon Mason55954f32016-07-07 19:08:55 -04001564 phy_disconnect(bgmac->net_dev->phydev);
Hauke Mehrtens62166422015-01-18 19:49:58 +01001565 netif_napi_del(&bgmac->napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001566 bgmac_dma_free(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001567 free_netdev(bgmac->net_dev);
1568}
Jon Masonf6a95a22016-07-07 19:08:57 -04001569EXPORT_SYMBOL_GPL(bgmac_enet_remove);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001570
Joey Zhongf3537b32017-02-28 13:51:01 -05001571int bgmac_enet_suspend(struct bgmac *bgmac)
1572{
1573 if (!netif_running(bgmac->net_dev))
1574 return 0;
1575
1576 phy_stop(bgmac->net_dev->phydev);
1577
1578 netif_stop_queue(bgmac->net_dev);
1579
1580 napi_disable(&bgmac->napi);
1581
1582 netif_tx_lock(bgmac->net_dev);
1583 netif_device_detach(bgmac->net_dev);
1584 netif_tx_unlock(bgmac->net_dev);
1585
1586 bgmac_chip_intrs_off(bgmac);
1587 bgmac_chip_reset(bgmac);
1588 bgmac_dma_cleanup(bgmac);
1589
1590 return 0;
1591}
1592EXPORT_SYMBOL_GPL(bgmac_enet_suspend);
1593
1594int bgmac_enet_resume(struct bgmac *bgmac)
1595{
1596 int rc;
1597
1598 if (!netif_running(bgmac->net_dev))
1599 return 0;
1600
1601 rc = bgmac_dma_init(bgmac);
1602 if (rc)
1603 return rc;
1604
1605 bgmac_chip_init(bgmac);
1606
1607 napi_enable(&bgmac->napi);
1608
1609 netif_tx_lock(bgmac->net_dev);
1610 netif_device_attach(bgmac->net_dev);
1611 netif_tx_unlock(bgmac->net_dev);
1612
1613 netif_start_queue(bgmac->net_dev);
1614
1615 phy_start(bgmac->net_dev->phydev);
1616
1617 return 0;
1618}
1619EXPORT_SYMBOL_GPL(bgmac_enet_resume);
1620
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001621MODULE_AUTHOR("Rafał Miłecki");
1622MODULE_LICENSE("GPL");