blob: 49f4cafe543806ed51ed8690b271e309c5609b9f [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>
14#include <linux/bcm47xx_nvram.h>
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000015#include "bgmac.h"
16
Jon Masonf6a95a22016-07-07 19:08:57 -040017static bool bgmac_wait_value(struct bgmac *bgmac, u16 reg, u32 mask,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000018 u32 value, int timeout)
19{
20 u32 val;
21 int i;
22
23 for (i = 0; i < timeout / 10; i++) {
Jon Masonf6a95a22016-07-07 19:08:57 -040024 val = bgmac_read(bgmac, reg);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000025 if ((val & mask) == value)
26 return true;
27 udelay(10);
28 }
Jon Masonf6a95a22016-07-07 19:08:57 -040029 dev_err(bgmac->dev, "Timeout waiting for reg 0x%X\n", reg);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000030 return false;
31}
32
33/**************************************************
34 * DMA
35 **************************************************/
36
37static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
38{
39 u32 val;
40 int i;
41
42 if (!ring->mmio_base)
43 return;
44
45 /* Suspend DMA TX ring first.
46 * bgmac_wait_value doesn't support waiting for any of few values, so
47 * implement whole loop here.
48 */
49 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
50 BGMAC_DMA_TX_SUSPEND);
51 for (i = 0; i < 10000 / 10; i++) {
52 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
53 val &= BGMAC_DMA_TX_STAT;
54 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
55 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
56 val == BGMAC_DMA_TX_STAT_STOPPED) {
57 i = 0;
58 break;
59 }
60 udelay(10);
61 }
62 if (i)
Jon Masond00a8282016-07-07 19:08:53 -040063 dev_err(bgmac->dev, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
64 ring->mmio_base, val);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000065
66 /* Remove SUSPEND bit */
67 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
Jon Masonf6a95a22016-07-07 19:08:57 -040068 if (!bgmac_wait_value(bgmac,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000069 ring->mmio_base + BGMAC_DMA_TX_STATUS,
70 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
71 10000)) {
Jon Masond00a8282016-07-07 19:08:53 -040072 dev_warn(bgmac->dev, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
73 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000074 udelay(300);
75 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
76 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
Jon Masond00a8282016-07-07 19:08:53 -040077 dev_err(bgmac->dev, "Reset of DMA TX ring 0x%X failed\n",
78 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000079 }
80}
81
82static void bgmac_dma_tx_enable(struct bgmac *bgmac,
83 struct bgmac_dma_ring *ring)
84{
85 u32 ctl;
86
87 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
Jon Masondb791eb2016-07-07 19:08:56 -040088 if (bgmac->feature_flags & BGMAC_FEAT_TX_MASK_SETUP) {
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +010089 ctl &= ~BGMAC_DMA_TX_BL_MASK;
90 ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
91
92 ctl &= ~BGMAC_DMA_TX_MR_MASK;
93 ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
94
95 ctl &= ~BGMAC_DMA_TX_PC_MASK;
96 ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
97
98 ctl &= ~BGMAC_DMA_TX_PT_MASK;
99 ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
100 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000101 ctl |= BGMAC_DMA_TX_ENABLE;
102 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
103 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
104}
105
Felix Fietkau9cde9452015-03-23 12:35:37 +0100106static void
107bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
108 int i, int len, u32 ctl0)
109{
110 struct bgmac_slot_info *slot;
111 struct bgmac_dma_desc *dma_desc;
112 u32 ctl1;
113
Felix Fietkau29ba8772015-04-14 12:08:02 +0200114 if (i == BGMAC_TX_RING_SLOTS - 1)
Felix Fietkau9cde9452015-03-23 12:35:37 +0100115 ctl0 |= BGMAC_DESC_CTL0_EOT;
116
117 ctl1 = len & BGMAC_DESC_CTL1_LEN;
118
119 slot = &ring->slots[i];
120 dma_desc = &ring->cpu_base[i];
121 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
122 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
123 dma_desc->ctl0 = cpu_to_le32(ctl0);
124 dma_desc->ctl1 = cpu_to_le32(ctl1);
125}
126
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000127static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
128 struct bgmac_dma_ring *ring,
129 struct sk_buff *skb)
130{
Jon Masona0b68482016-07-07 19:08:54 -0400131 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000132 struct net_device *net_dev = bgmac->net_dev;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200133 int index = ring->end % BGMAC_TX_RING_SLOTS;
134 struct bgmac_slot_info *slot = &ring->slots[index];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100135 int nr_frags;
136 u32 flags;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100137 int i;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000138
139 if (skb->len > BGMAC_DESC_CTL1_LEN) {
Jon Masond00a8282016-07-07 19:08:53 -0400140 netdev_err(bgmac->net_dev, "Too long skb (%d)\n", skb->len);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100141 goto err_drop;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000142 }
143
Felix Fietkau9cde9452015-03-23 12:35:37 +0100144 if (skb->ip_summed == CHECKSUM_PARTIAL)
145 skb_checksum_help(skb);
146
147 nr_frags = skb_shinfo(skb)->nr_frags;
148
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200149 /* ring->end - ring->start will return the number of valid slots,
150 * even when ring->end overflows
151 */
152 if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
Jon Masond00a8282016-07-07 19:08:53 -0400153 netdev_err(bgmac->net_dev, "TX ring is full, queue should be stopped!\n");
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000154 netif_stop_queue(net_dev);
155 return NETDEV_TX_BUSY;
156 }
157
Felix Fietkau9cde9452015-03-23 12:35:37 +0100158 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000159 DMA_TO_DEVICE);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100160 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
161 goto err_dma_head;
162
163 flags = BGMAC_DESC_CTL0_SOF;
164 if (!nr_frags)
165 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
166
167 bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
168 flags = 0;
169
170 for (i = 0; i < nr_frags; i++) {
171 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
172 int len = skb_frag_size(frag);
173
174 index = (index + 1) % BGMAC_TX_RING_SLOTS;
175 slot = &ring->slots[index];
176 slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
177 len, DMA_TO_DEVICE);
178 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
179 goto err_dma;
180
181 if (i == nr_frags - 1)
182 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
183
184 bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000185 }
186
Felix Fietkau9cde9452015-03-23 12:35:37 +0100187 slot->skb = skb;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200188 ring->end += nr_frags + 1;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200189 netdev_sent_queue(net_dev, skb->len);
190
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000191 wmb();
192
193 /* Increase ring->end to point empty slot. We tell hardware the first
194 * slot it should *not* read.
195 */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000196 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
Rafał Miłecki99003032013-09-15 23:13:18 +0200197 ring->index_base +
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200198 (ring->end % BGMAC_TX_RING_SLOTS) *
199 sizeof(struct bgmac_dma_desc));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000200
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200201 if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000202 netif_stop_queue(net_dev);
203
204 return NETDEV_TX_OK;
205
Felix Fietkau9cde9452015-03-23 12:35:37 +0100206err_dma:
207 dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
208 DMA_TO_DEVICE);
209
Florian Fainellie86663c2016-07-15 15:42:52 -0700210 while (i-- > 0) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100211 int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
212 struct bgmac_slot_info *slot = &ring->slots[index];
213 u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
214 int len = ctl1 & BGMAC_DESC_CTL1_LEN;
215
216 dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
217 }
218
219err_dma_head:
Jon Masond00a8282016-07-07 19:08:53 -0400220 netdev_err(bgmac->net_dev, "Mapping error of skb on ring 0x%X\n",
221 ring->mmio_base);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100222
223err_drop:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000224 dev_kfree_skb(skb);
Florian Fainelli6d490f622016-06-07 15:06:15 -0700225 net_dev->stats.tx_dropped++;
226 net_dev->stats.tx_errors++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000227 return NETDEV_TX_OK;
228}
229
230/* Free transmitted packets */
231static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
232{
Jon Masona0b68482016-07-07 19:08:54 -0400233 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000234 int empty_slot;
235 bool freed = false;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200236 unsigned bytes_compl = 0, pkts_compl = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000237
238 /* The last slot that hardware didn't consume yet */
239 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
240 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200241 empty_slot -= ring->index_base;
242 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000243 empty_slot /= sizeof(struct bgmac_dma_desc);
244
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200245 while (ring->start != ring->end) {
246 int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
247 struct bgmac_slot_info *slot = &ring->slots[slot_idx];
Florian Fainellid2b13232016-06-23 14:23:12 -0700248 u32 ctl0, ctl1;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200249 int len;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100250
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200251 if (slot_idx == empty_slot)
252 break;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100253
Florian Fainellid2b13232016-06-23 14:23:12 -0700254 ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0);
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200255 ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
256 len = ctl1 & BGMAC_DESC_CTL1_LEN;
Florian Fainellid2b13232016-06-23 14:23:12 -0700257 if (ctl0 & BGMAC_DESC_CTL0_SOF)
Felix Fietkau9cde9452015-03-23 12:35:37 +0100258 /* Unmap no longer used buffer */
259 dma_unmap_single(dma_dev, slot->dma_addr, len,
260 DMA_TO_DEVICE);
261 else
262 dma_unmap_page(dma_dev, slot->dma_addr, len,
263 DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000264
265 if (slot->skb) {
Florian Fainelli6d490f622016-06-07 15:06:15 -0700266 bgmac->net_dev->stats.tx_bytes += slot->skb->len;
267 bgmac->net_dev->stats.tx_packets++;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200268 bytes_compl += slot->skb->len;
269 pkts_compl++;
270
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000271 /* Free memory! :) */
272 dev_kfree_skb(slot->skb);
273 slot->skb = NULL;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000274 }
275
Felix Fietkau9cde9452015-03-23 12:35:37 +0100276 slot->dma_addr = 0;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200277 ring->start++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000278 freed = true;
279 }
280
Felix Fietkau9cde9452015-03-23 12:35:37 +0100281 if (!pkts_compl)
282 return;
283
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200284 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
285
Felix Fietkau9cde9452015-03-23 12:35:37 +0100286 if (netif_queue_stopped(bgmac->net_dev))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000287 netif_wake_queue(bgmac->net_dev);
288}
289
290static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
291{
292 if (!ring->mmio_base)
293 return;
294
295 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
Jon Masonf6a95a22016-07-07 19:08:57 -0400296 if (!bgmac_wait_value(bgmac,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000297 ring->mmio_base + BGMAC_DMA_RX_STATUS,
298 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
299 10000))
Jon Masond00a8282016-07-07 19:08:53 -0400300 dev_err(bgmac->dev, "Reset of ring 0x%X RX failed\n",
301 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000302}
303
304static void bgmac_dma_rx_enable(struct bgmac *bgmac,
305 struct bgmac_dma_ring *ring)
306{
307 u32 ctl;
308
309 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
Andy Gospodarekfcdefcc2016-10-31 13:32:03 -0400310
311 /* preserve ONLY bits 16-17 from current hardware value */
312 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
313
Jon Masondb791eb2016-07-07 19:08:56 -0400314 if (bgmac->feature_flags & BGMAC_FEAT_RX_MASK_SETUP) {
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100315 ctl &= ~BGMAC_DMA_RX_BL_MASK;
316 ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
317
318 ctl &= ~BGMAC_DMA_RX_PC_MASK;
319 ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
320
321 ctl &= ~BGMAC_DMA_RX_PT_MASK;
322 ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
323 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000324 ctl |= BGMAC_DMA_RX_ENABLE;
325 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
326 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
327 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
328 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
329}
330
331static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
332 struct bgmac_slot_info *slot)
333{
Jon Masona0b68482016-07-07 19:08:54 -0400334 struct device *dma_dev = bgmac->dma_dev;
Nathan Hintzb757a622013-10-29 19:32:01 -0700335 dma_addr_t dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000336 struct bgmac_rx_header *rx;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100337 void *buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000338
339 /* Alloc skb */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100340 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
341 if (!buf)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000342 return -ENOMEM;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000343
344 /* Poison - if everything goes fine, hardware will overwrite it */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200345 rx = buf + BGMAC_RX_BUF_OFFSET;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000346 rx->len = cpu_to_le16(0xdead);
347 rx->flags = cpu_to_le16(0xbeef);
348
349 /* Map skb for the DMA */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200350 dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
351 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
Nathan Hintzb757a622013-10-29 19:32:01 -0700352 if (dma_mapping_error(dma_dev, dma_addr)) {
Jon Masond00a8282016-07-07 19:08:53 -0400353 netdev_err(bgmac->net_dev, "DMA mapping error\n");
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100354 put_page(virt_to_head_page(buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000355 return -ENOMEM;
356 }
Nathan Hintzb757a622013-10-29 19:32:01 -0700357
358 /* Update the slot */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100359 slot->buf = buf;
Nathan Hintzb757a622013-10-29 19:32:01 -0700360 slot->dma_addr = dma_addr;
361
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000362 return 0;
363}
364
Felix Fietkau4668ae12015-04-14 12:08:01 +0200365static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
366 struct bgmac_dma_ring *ring)
367{
368 dma_wmb();
369
370 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
371 ring->index_base +
372 ring->end * sizeof(struct bgmac_dma_desc));
373}
374
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100375static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
376 struct bgmac_dma_ring *ring, int desc_idx)
377{
378 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
379 u32 ctl0 = 0, ctl1 = 0;
380
Felix Fietkau29ba8772015-04-14 12:08:02 +0200381 if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100382 ctl0 |= BGMAC_DESC_CTL0_EOT;
383 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
384 /* Is there any BGMAC device that requires extension? */
385 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
386 * B43_DMA64_DCTL1_ADDREXT_MASK;
387 */
388
389 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
390 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
391 dma_desc->ctl0 = cpu_to_le32(ctl0);
392 dma_desc->ctl1 = cpu_to_le32(ctl1);
Felix Fietkau4668ae12015-04-14 12:08:01 +0200393
394 ring->end = desc_idx;
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100395}
396
Felix Fietkau56faacd2015-04-14 12:07:57 +0200397static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
398 struct bgmac_slot_info *slot)
399{
400 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
401
402 dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
403 DMA_FROM_DEVICE);
404 rx->len = cpu_to_le16(0xdead);
405 rx->flags = cpu_to_le16(0xbeef);
406 dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
407 DMA_FROM_DEVICE);
408}
409
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000410static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
411 int weight)
412{
413 u32 end_slot;
414 int handled = 0;
415
416 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
417 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200418 end_slot -= ring->index_base;
419 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000420 end_slot /= sizeof(struct bgmac_dma_desc);
421
Felix Fietkau4668ae12015-04-14 12:08:01 +0200422 while (ring->start != end_slot) {
Jon Masona0b68482016-07-07 19:08:54 -0400423 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000424 struct bgmac_slot_info *slot = &ring->slots[ring->start];
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200425 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100426 struct sk_buff *skb;
427 void *buf = slot->buf;
Felix Fietkau56faacd2015-04-14 12:07:57 +0200428 dma_addr_t dma_addr = slot->dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000429 u16 len, flags;
430
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100431 do {
Felix Fietkau56faacd2015-04-14 12:07:57 +0200432 /* Prepare new skb as replacement */
433 if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
434 bgmac_dma_rx_poison_buf(dma_dev, slot);
435 break;
436 }
437
438 /* Unmap buffer to make it accessible to the CPU */
439 dma_unmap_single(dma_dev, dma_addr,
440 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
441
442 /* Get info from the header */
443 len = le16_to_cpu(rx->len);
444 flags = le16_to_cpu(rx->flags);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100445
446 /* Check for poison and drop or pass the packet */
447 if (len == 0xdead && flags == 0xbeef) {
Jon Masond00a8282016-07-07 19:08:53 -0400448 netdev_err(bgmac->net_dev, "Found poisoned packet at slot %d, DMA issue!\n",
449 ring->start);
Felix Fietkau56faacd2015-04-14 12:07:57 +0200450 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700451 bgmac->net_dev->stats.rx_errors++;
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100452 break;
453 }
454
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200455 if (len > BGMAC_RX_ALLOC_SIZE) {
Jon Masond00a8282016-07-07 19:08:53 -0400456 netdev_err(bgmac->net_dev, "Found oversized packet at slot %d, DMA issue!\n",
457 ring->start);
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200458 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700459 bgmac->net_dev->stats.rx_length_errors++;
460 bgmac->net_dev->stats.rx_errors++;
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200461 break;
462 }
463
Hauke Mehrtens02e71122013-02-28 07:16:54 +0000464 /* Omit CRC. */
465 len -= ETH_FCS_LEN;
466
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100467 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
David S. Miller750afbf2016-01-15 16:07:13 -0500468 if (unlikely(!skb)) {
Jon Masond00a8282016-07-07 19:08:53 -0400469 netdev_err(bgmac->net_dev, "build_skb failed\n");
wangweidongf1640c32016-01-13 11:06:41 +0800470 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700471 bgmac->net_dev->stats.rx_errors++;
wangweidongf1640c32016-01-13 11:06:41 +0800472 break;
473 }
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200474 skb_put(skb, BGMAC_RX_FRAME_OFFSET +
475 BGMAC_RX_BUF_OFFSET + len);
476 skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
477 BGMAC_RX_BUF_OFFSET);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100478
479 skb_checksum_none_assert(skb);
480 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
Florian Fainelli6d490f622016-06-07 15:06:15 -0700481 bgmac->net_dev->stats.rx_bytes += len;
482 bgmac->net_dev->stats.rx_packets++;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100483 napi_gro_receive(&bgmac->napi, skb);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100484 handled++;
485 } while (0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000486
Felix Fietkau56faacd2015-04-14 12:07:57 +0200487 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
488
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000489 if (++ring->start >= BGMAC_RX_RING_SLOTS)
490 ring->start = 0;
491
492 if (handled >= weight) /* Should never be greater */
493 break;
494 }
495
Felix Fietkau4668ae12015-04-14 12:08:01 +0200496 bgmac_dma_rx_update_index(bgmac, ring);
497
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000498 return handled;
499}
500
501/* Does ring support unaligned addressing? */
502static bool bgmac_dma_unaligned(struct bgmac *bgmac,
503 struct bgmac_dma_ring *ring,
504 enum bgmac_dma_ring_type ring_type)
505{
506 switch (ring_type) {
507 case BGMAC_DMA_RING_TX:
508 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
509 0xff0);
510 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
511 return true;
512 break;
513 case BGMAC_DMA_RING_RX:
514 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
515 0xff0);
516 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
517 return true;
518 break;
519 }
520 return false;
521}
522
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100523static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
524 struct bgmac_dma_ring *ring)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000525{
Jon Masona0b68482016-07-07 19:08:54 -0400526 struct device *dma_dev = bgmac->dma_dev;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100527 struct bgmac_dma_desc *dma_desc = ring->cpu_base;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000528 struct bgmac_slot_info *slot;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000529 int i;
530
Felix Fietkau29ba8772015-04-14 12:08:02 +0200531 for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100532 int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN;
533
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000534 slot = &ring->slots[i];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100535 dev_kfree_skb(slot->skb);
536
537 if (!slot->dma_addr)
538 continue;
539
540 if (slot->skb)
541 dma_unmap_single(dma_dev, slot->dma_addr,
542 len, DMA_TO_DEVICE);
543 else
544 dma_unmap_page(dma_dev, slot->dma_addr,
545 len, DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000546 }
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100547}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000548
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100549static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
550 struct bgmac_dma_ring *ring)
551{
Jon Masona0b68482016-07-07 19:08:54 -0400552 struct device *dma_dev = bgmac->dma_dev;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100553 struct bgmac_slot_info *slot;
554 int i;
555
Felix Fietkau29ba8772015-04-14 12:08:02 +0200556 for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100557 slot = &ring->slots[i];
Felix Fietkau56faacd2015-04-14 12:07:57 +0200558 if (!slot->dma_addr)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100559 continue;
560
Felix Fietkau56faacd2015-04-14 12:07:57 +0200561 dma_unmap_single(dma_dev, slot->dma_addr,
562 BGMAC_RX_BUF_SIZE,
563 DMA_FROM_DEVICE);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100564 put_page(virt_to_head_page(slot->buf));
Felix Fietkau56faacd2015-04-14 12:07:57 +0200565 slot->dma_addr = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000566 }
567}
568
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100569static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
Felix Fietkau29ba8772015-04-14 12:08:02 +0200570 struct bgmac_dma_ring *ring,
571 int num_slots)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100572{
Jon Masona0b68482016-07-07 19:08:54 -0400573 struct device *dma_dev = bgmac->dma_dev;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100574 int size;
575
576 if (!ring->cpu_base)
577 return;
578
579 /* Free ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200580 size = num_slots * sizeof(struct bgmac_dma_desc);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100581 dma_free_coherent(dma_dev, size, ring->cpu_base,
582 ring->dma_base);
583}
584
Felix Fietkau74b6f292015-04-14 12:08:00 +0200585static void bgmac_dma_cleanup(struct bgmac *bgmac)
586{
587 int i;
588
589 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
590 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
591
592 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
593 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
594}
595
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000596static void bgmac_dma_free(struct bgmac *bgmac)
597{
598 int i;
599
Felix Fietkau74b6f292015-04-14 12:08:00 +0200600 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200601 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
602 BGMAC_TX_RING_SLOTS);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200603
604 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200605 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
606 BGMAC_RX_RING_SLOTS);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000607}
608
609static int bgmac_dma_alloc(struct bgmac *bgmac)
610{
Jon Masona0b68482016-07-07 19:08:54 -0400611 struct device *dma_dev = bgmac->dma_dev;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000612 struct bgmac_dma_ring *ring;
613 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
614 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
615 int size; /* ring size: different for Tx and Rx */
616 int err;
617 int i;
618
619 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
620 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
621
Jon Masonf6a95a22016-07-07 19:08:57 -0400622 if (!(bgmac_idm_read(bgmac, BCMA_IOST) & BCMA_IOST_DMA64)) {
Jon Masond00a8282016-07-07 19:08:53 -0400623 dev_err(bgmac->dev, "Core does not report 64-bit DMA\n");
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000624 return -ENOTSUPP;
625 }
626
627 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
628 ring = &bgmac->tx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000629 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000630
631 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200632 size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000633 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
634 &ring->dma_base,
635 GFP_KERNEL);
636 if (!ring->cpu_base) {
Jon Masond00a8282016-07-07 19:08:53 -0400637 dev_err(bgmac->dev, "Allocation of TX ring 0x%X failed\n",
638 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000639 goto err_dma_free;
640 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000641
Rafał Miłecki99003032013-09-15 23:13:18 +0200642 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
643 BGMAC_DMA_RING_TX);
644 if (ring->unaligned)
645 ring->index_base = lower_32_bits(ring->dma_base);
646 else
647 ring->index_base = 0;
648
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000649 /* No need to alloc TX slots yet */
650 }
651
652 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
653 ring = &bgmac->rx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000654 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000655
656 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200657 size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000658 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
659 &ring->dma_base,
660 GFP_KERNEL);
661 if (!ring->cpu_base) {
Jon Masond00a8282016-07-07 19:08:53 -0400662 dev_err(bgmac->dev, "Allocation of RX ring 0x%X failed\n",
663 ring->mmio_base);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000664 err = -ENOMEM;
665 goto err_dma_free;
666 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000667
Rafał Miłecki99003032013-09-15 23:13:18 +0200668 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
669 BGMAC_DMA_RING_RX);
670 if (ring->unaligned)
671 ring->index_base = lower_32_bits(ring->dma_base);
672 else
673 ring->index_base = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000674 }
675
676 return 0;
677
678err_dma_free:
679 bgmac_dma_free(bgmac);
680 return -ENOMEM;
681}
682
Felix Fietkau74b6f292015-04-14 12:08:00 +0200683static int bgmac_dma_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000684{
685 struct bgmac_dma_ring *ring;
Felix Fietkau74b6f292015-04-14 12:08:00 +0200686 int i, err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000687
688 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
689 ring = &bgmac->tx_ring[i];
690
Rafał Miłecki99003032013-09-15 23:13:18 +0200691 if (!ring->unaligned)
692 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000693 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
694 lower_32_bits(ring->dma_base));
695 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
696 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200697 if (ring->unaligned)
698 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000699
700 ring->start = 0;
701 ring->end = 0; /* Points the slot that should *not* be read */
702 }
703
704 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000705 int j;
706
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000707 ring = &bgmac->rx_ring[i];
708
Rafał Miłecki99003032013-09-15 23:13:18 +0200709 if (!ring->unaligned)
710 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000711 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
712 lower_32_bits(ring->dma_base));
713 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
714 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200715 if (ring->unaligned)
716 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000717
Felix Fietkau4668ae12015-04-14 12:08:01 +0200718 ring->start = 0;
719 ring->end = 0;
Felix Fietkau29ba8772015-04-14 12:08:02 +0200720 for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
Felix Fietkau74b6f292015-04-14 12:08:00 +0200721 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
722 if (err)
723 goto error;
724
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100725 bgmac_dma_rx_setup_desc(bgmac, ring, j);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200726 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000727
Felix Fietkau4668ae12015-04-14 12:08:01 +0200728 bgmac_dma_rx_update_index(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000729 }
Felix Fietkau74b6f292015-04-14 12:08:00 +0200730
731 return 0;
732
733error:
734 bgmac_dma_cleanup(bgmac);
735 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000736}
737
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000738
739/**************************************************
740 * Chip ops
741 **************************************************/
742
743/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
744 * nothing to change? Try if after stabilizng driver.
745 */
746static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
747 bool force)
748{
749 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
750 u32 new_val = (cmdcfg & mask) | set;
Jon Masondb791eb2016-07-07 19:08:56 -0400751 u32 cmdcfg_sr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000752
Jon Masondb791eb2016-07-07 19:08:56 -0400753 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
754 cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
755 else
756 cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
757
758 bgmac_set(bgmac, BGMAC_CMDCFG, cmdcfg_sr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000759 udelay(2);
760
761 if (new_val != cmdcfg || force)
762 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
763
Jon Masondb791eb2016-07-07 19:08:56 -0400764 bgmac_mask(bgmac, BGMAC_CMDCFG, ~cmdcfg_sr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000765 udelay(2);
766}
767
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000768static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
769{
770 u32 tmp;
771
772 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
773 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
774 tmp = (addr[4] << 8) | addr[5];
775 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
776}
777
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000778static void bgmac_set_rx_mode(struct net_device *net_dev)
779{
780 struct bgmac *bgmac = netdev_priv(net_dev);
781
782 if (net_dev->flags & IFF_PROMISC)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000783 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000784 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000785 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000786}
787
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000788#if 0 /* We don't use that regs yet */
789static void bgmac_chip_stats_update(struct bgmac *bgmac)
790{
791 int i;
792
Jon Masondb791eb2016-07-07 19:08:56 -0400793 if (!(bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000794 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
795 bgmac->mib_tx_regs[i] =
796 bgmac_read(bgmac,
797 BGMAC_TX_GOOD_OCTETS + (i * 4));
798 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
799 bgmac->mib_rx_regs[i] =
800 bgmac_read(bgmac,
801 BGMAC_RX_GOOD_OCTETS + (i * 4));
802 }
803
804 /* TODO: what else? how to handle BCM4706? Specs are needed */
805}
806#endif
807
808static void bgmac_clear_mib(struct bgmac *bgmac)
809{
810 int i;
811
Jon Masondb791eb2016-07-07 19:08:56 -0400812 if (bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000813 return;
814
815 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
816 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
817 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
818 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
819 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
820}
821
822/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100823static void bgmac_mac_speed(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000824{
825 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
826 u32 set = 0;
827
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100828 switch (bgmac->mac_speed) {
829 case SPEED_10:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000830 set |= BGMAC_CMDCFG_ES_10;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100831 break;
832 case SPEED_100:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000833 set |= BGMAC_CMDCFG_ES_100;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100834 break;
835 case SPEED_1000:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000836 set |= BGMAC_CMDCFG_ES_1000;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100837 break;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100838 case SPEED_2500:
839 set |= BGMAC_CMDCFG_ES_2500;
840 break;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100841 default:
Jon Masond00a8282016-07-07 19:08:53 -0400842 dev_err(bgmac->dev, "Unsupported speed: %d\n",
843 bgmac->mac_speed);
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100844 }
845
846 if (bgmac->mac_duplex == DUPLEX_HALF)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000847 set |= BGMAC_CMDCFG_HD;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100848
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000849 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
850}
851
852static void bgmac_miiconfig(struct bgmac *bgmac)
853{
Jon Masondb791eb2016-07-07 19:08:56 -0400854 if (bgmac->feature_flags & BGMAC_FEAT_FORCE_SPEED_2500) {
Jon Masonf6a95a22016-07-07 19:08:57 -0400855 bgmac_idm_write(bgmac, BCMA_IOCTL,
856 bgmac_idm_read(bgmac, BCMA_IOCTL) | 0x40 |
857 BGMAC_BCMA_IOCTL_SW_CLKEN);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100858 bgmac->mac_speed = SPEED_2500;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100859 bgmac->mac_duplex = DUPLEX_FULL;
860 bgmac_mac_speed(bgmac);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100861 } else {
Jon Masondb791eb2016-07-07 19:08:56 -0400862 u8 imode;
863
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100864 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
865 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
866 if (imode == 0 || imode == 1) {
867 bgmac->mac_speed = SPEED_100;
868 bgmac->mac_duplex = DUPLEX_FULL;
869 bgmac_mac_speed(bgmac);
870 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000871 }
872}
873
874/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
875static void bgmac_chip_reset(struct bgmac *bgmac)
876{
Jon Masondb791eb2016-07-07 19:08:56 -0400877 u32 cmdcfg_sr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000878 u32 iost;
879 int i;
880
Jon Masonf6a95a22016-07-07 19:08:57 -0400881 if (bgmac_clk_enabled(bgmac)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000882 if (!bgmac->stats_grabbed) {
883 /* bgmac_chip_stats_update(bgmac); */
884 bgmac->stats_grabbed = true;
885 }
886
887 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
888 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
889
890 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
891 udelay(1);
892
893 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
894 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
895
896 /* TODO: Clear software multicast filter list */
897 }
898
Jon Masonf6a95a22016-07-07 19:08:57 -0400899 iost = bgmac_idm_read(bgmac, BCMA_IOST);
Jon Masondb791eb2016-07-07 19:08:56 -0400900 if (bgmac->feature_flags & BGMAC_FEAT_IOST_ATTACHED)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000901 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
902
Rafał Miłecki9e4e6202016-02-22 22:51:13 +0100903 /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
Jon Masondb791eb2016-07-07 19:08:56 -0400904 if (!(bgmac->feature_flags & BGMAC_FEAT_NO_RESET)) {
905 u32 flags = 0;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100906 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
907 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
908 if (!bgmac->has_robosw)
909 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
910 }
Jon Masonf6a95a22016-07-07 19:08:57 -0400911 bgmac_clk_enable(bgmac, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000912 }
913
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100914 /* Request Misc PLL for corerev > 2 */
Jon Masondb791eb2016-07-07 19:08:56 -0400915 if (bgmac->feature_flags & BGMAC_FEAT_MISC_PLL_REQ) {
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100916 bgmac_set(bgmac, BCMA_CLKCTLST,
917 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
Jon Masonf6a95a22016-07-07 19:08:57 -0400918 bgmac_wait_value(bgmac, BCMA_CLKCTLST,
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100919 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
920 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000921 1000);
922 }
923
Jon Masondb791eb2016-07-07 19:08:56 -0400924 if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_PHY) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000925 u8 et_swtype = 0;
926 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
Rafał Miłecki6a391e72013-09-15 00:22:47 +0200927 BGMAC_CHIPCTL_1_IF_TYPE_MII;
Hauke Mehrtens36472682013-09-15 22:49:08 +0200928 char buf[4];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000929
Hauke Mehrtens36472682013-09-15 22:49:08 +0200930 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000931 if (kstrtou8(buf, 0, &et_swtype))
Jon Masond00a8282016-07-07 19:08:53 -0400932 dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
933 buf);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000934 et_swtype &= 0x0f;
935 et_swtype <<= 4;
936 sw_type = et_swtype;
Jon Masondb791eb2016-07-07 19:08:56 -0400937 } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_EPHYRMII) {
Rafał Miłeckie2d8f642016-08-17 23:11:52 +0200938 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RMII |
939 BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Jon Masondb791eb2016-07-07 19:08:56 -0400940 } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_RGMII) {
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +0000941 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
942 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000943 }
Jon Masonf6a95a22016-07-07 19:08:57 -0400944 bgmac_cco_ctl_maskset(bgmac, 1, ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
945 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
946 sw_type);
Rafał Miłecki1cb94db2016-08-17 23:00:30 +0200947 } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE) {
948 u32 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_MII |
949 BGMAC_CHIPCTL_4_SW_TYPE_EPHY;
950 u8 et_swtype = 0;
951 char buf[4];
952
953 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
954 if (kstrtou8(buf, 0, &et_swtype))
955 dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
956 buf);
957 sw_type = (et_swtype & 0x0f) << 12;
958 } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII) {
959 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_RGMII |
960 BGMAC_CHIPCTL_4_SW_TYPE_RGMII;
961 }
962 bgmac_cco_ctl_maskset(bgmac, 4, ~(BGMAC_CHIPCTL_4_IF_TYPE_MASK |
963 BGMAC_CHIPCTL_4_SW_TYPE_MASK),
964 sw_type);
965 } else if (bgmac->feature_flags & BGMAC_FEAT_CC7_IF_TYPE_RGMII) {
966 bgmac_cco_ctl_maskset(bgmac, 7, ~BGMAC_CHIPCTL_7_IF_TYPE_MASK,
967 BGMAC_CHIPCTL_7_IF_TYPE_RGMII);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000968 }
969
970 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
Jon Masonf6a95a22016-07-07 19:08:57 -0400971 bgmac_idm_write(bgmac, BCMA_IOCTL,
972 bgmac_idm_read(bgmac, BCMA_IOCTL) &
973 ~BGMAC_BCMA_IOCTL_SW_RESET);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000974
975 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
976 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
977 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
978 * be keps until taking MAC out of the reset.
979 */
Jon Masondb791eb2016-07-07 19:08:56 -0400980 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
981 cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
982 else
983 cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
984
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000985 bgmac_cmdcfg_maskset(bgmac,
986 ~(BGMAC_CMDCFG_TE |
987 BGMAC_CMDCFG_RE |
988 BGMAC_CMDCFG_RPI |
989 BGMAC_CMDCFG_TAI |
990 BGMAC_CMDCFG_HD |
991 BGMAC_CMDCFG_ML |
992 BGMAC_CMDCFG_CFE |
993 BGMAC_CMDCFG_RL |
994 BGMAC_CMDCFG_RED |
995 BGMAC_CMDCFG_PE |
996 BGMAC_CMDCFG_TPI |
997 BGMAC_CMDCFG_PAD_EN |
998 BGMAC_CMDCFG_PF),
999 BGMAC_CMDCFG_PROM |
1000 BGMAC_CMDCFG_NLC |
1001 BGMAC_CMDCFG_CFE |
Jon Masondb791eb2016-07-07 19:08:56 -04001002 cmdcfg_sr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001003 false);
Rafał Miłeckid4699622013-12-11 07:44:14 +01001004 bgmac->mac_speed = SPEED_UNKNOWN;
1005 bgmac->mac_duplex = DUPLEX_UNKNOWN;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001006
1007 bgmac_clear_mib(bgmac);
Jon Masondb791eb2016-07-07 19:08:56 -04001008 if (bgmac->feature_flags & BGMAC_FEAT_CMN_PHY_CTL)
Jon Masonf6a95a22016-07-07 19:08:57 -04001009 bgmac_cmn_maskset32(bgmac, BCMA_GMAC_CMN_PHY_CTL, ~0,
1010 BCMA_GMAC_CMN_PC_MTE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001011 else
1012 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1013 bgmac_miiconfig(bgmac);
Jon Mason55954f32016-07-07 19:08:55 -04001014 if (bgmac->mii_bus)
1015 bgmac->mii_bus->reset(bgmac->mii_bus);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001016
Hauke Mehrtens49a467b2013-09-29 13:54:58 +02001017 netdev_reset_queue(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001018}
1019
1020static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1021{
1022 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1023}
1024
1025static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1026{
1027 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
Nathan Hintz41608152013-02-13 19:14:10 +00001028 bgmac_read(bgmac, BGMAC_INT_MASK);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001029}
1030
1031/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1032static void bgmac_enable(struct bgmac *bgmac)
1033{
Jon Masondb791eb2016-07-07 19:08:56 -04001034 u32 cmdcfg_sr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001035 u32 cmdcfg;
1036 u32 mode;
Jon Masondb791eb2016-07-07 19:08:56 -04001037
1038 if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
1039 cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
1040 else
1041 cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001042
1043 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
1044 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
Jon Masondb791eb2016-07-07 19:08:56 -04001045 cmdcfg_sr, true);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001046 udelay(2);
1047 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1048 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1049
1050 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1051 BGMAC_DS_MM_SHIFT;
Rafał Miłeckicdb26d32016-11-07 13:53:27 +01001052 if (bgmac->feature_flags & BGMAC_FEAT_CLKCTLST || mode != 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001053 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
Rafał Miłeckicdb26d32016-11-07 13:53:27 +01001054 if (!(bgmac->feature_flags & BGMAC_FEAT_CLKCTLST) && mode == 2)
Jon Masonf6a95a22016-07-07 19:08:57 -04001055 bgmac_cco_ctl_maskset(bgmac, 1, ~0,
1056 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001057
Jon Masondb791eb2016-07-07 19:08:56 -04001058 if (bgmac->feature_flags & (BGMAC_FEAT_FLW_CTRL1 |
1059 BGMAC_FEAT_FLW_CTRL2)) {
1060 u32 fl_ctl;
1061
1062 if (bgmac->feature_flags & BGMAC_FEAT_FLW_CTRL1)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001063 fl_ctl = 0x2300e1;
Jon Masondb791eb2016-07-07 19:08:56 -04001064 else
1065 fl_ctl = 0x03cb04cb;
1066
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001067 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1068 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001069 }
1070
Jon Masondb791eb2016-07-07 19:08:56 -04001071 if (bgmac->feature_flags & BGMAC_FEAT_SET_RXQ_CLK) {
1072 u32 rxq_ctl;
1073 u16 bp_clk;
1074 u8 mdp;
1075
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001076 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1077 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
Jon Masonf6a95a22016-07-07 19:08:57 -04001078 bp_clk = bgmac_get_bus_clock(bgmac) / 1000000;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001079 mdp = (bp_clk * 128 / 1000) - 3;
1080 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1081 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1082 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001083}
1084
1085/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001086static void bgmac_chip_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001087{
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001088 /* 1 interrupt per received frame */
1089 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1090
1091 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1092 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1093
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001094 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001095
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001096 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001097
1098 if (bgmac->loopback)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001099 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001100 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001101 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001102
1103 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1104
Felix Fietkau74b6f292015-04-14 12:08:00 +02001105 bgmac_chip_intrs_on(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001106
1107 bgmac_enable(bgmac);
1108}
1109
1110static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1111{
1112 struct bgmac *bgmac = netdev_priv(dev_id);
1113
1114 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1115 int_status &= bgmac->int_mask;
1116
1117 if (!int_status)
1118 return IRQ_NONE;
1119
Felix Fietkaueb64e292015-04-14 12:07:55 +02001120 int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1121 if (int_status)
Jon Masond00a8282016-07-07 19:08:53 -04001122 dev_err(bgmac->dev, "Unknown IRQs: 0x%08X\n", int_status);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001123
1124 /* Disable new interrupts until handling existing ones */
1125 bgmac_chip_intrs_off(bgmac);
1126
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001127 napi_schedule(&bgmac->napi);
1128
1129 return IRQ_HANDLED;
1130}
1131
1132static int bgmac_poll(struct napi_struct *napi, int weight)
1133{
1134 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001135 int handled = 0;
1136
Felix Fietkaueb64e292015-04-14 12:07:55 +02001137 /* Ack */
1138 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001139
Felix Fietkaueb64e292015-04-14 12:07:55 +02001140 bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1141 handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001142
Felix Fietkaueb64e292015-04-14 12:07:55 +02001143 /* Poll again if more events arrived in the meantime */
1144 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
Rafał Miłeckie5802672015-04-23 20:56:29 +02001145 return weight;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001146
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001147 if (handled < weight) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001148 napi_complete(napi);
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001149 bgmac_chip_intrs_on(bgmac);
1150 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001151
1152 return handled;
1153}
1154
1155/**************************************************
1156 * net_device_ops
1157 **************************************************/
1158
1159static int bgmac_open(struct net_device *net_dev)
1160{
1161 struct bgmac *bgmac = netdev_priv(net_dev);
1162 int err = 0;
1163
1164 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001165
1166 err = bgmac_dma_init(bgmac);
1167 if (err)
1168 return err;
1169
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001170 /* Specs say about reclaiming rings here, but we do that in DMA init */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001171 bgmac_chip_init(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001172
Jon Masonf6a95a22016-07-07 19:08:57 -04001173 err = request_irq(bgmac->irq, bgmac_interrupt, IRQF_SHARED,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001174 KBUILD_MODNAME, net_dev);
1175 if (err < 0) {
Jon Masond00a8282016-07-07 19:08:53 -04001176 dev_err(bgmac->dev, "IRQ request error: %d!\n", err);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001177 bgmac_dma_cleanup(bgmac);
1178 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001179 }
1180 napi_enable(&bgmac->napi);
1181
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001182 phy_start(net_dev->phydev);
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001183
Florian Fainellic3897f22016-06-23 14:25:32 -07001184 netif_start_queue(net_dev);
1185
Felix Fietkau74b6f292015-04-14 12:08:00 +02001186 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001187}
1188
1189static int bgmac_stop(struct net_device *net_dev)
1190{
1191 struct bgmac *bgmac = netdev_priv(net_dev);
1192
1193 netif_carrier_off(net_dev);
1194
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001195 phy_stop(net_dev->phydev);
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001196
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001197 napi_disable(&bgmac->napi);
1198 bgmac_chip_intrs_off(bgmac);
Jon Masonf6a95a22016-07-07 19:08:57 -04001199 free_irq(bgmac->irq, net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001200
1201 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001202 bgmac_dma_cleanup(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001203
1204 return 0;
1205}
1206
1207static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1208 struct net_device *net_dev)
1209{
1210 struct bgmac *bgmac = netdev_priv(net_dev);
1211 struct bgmac_dma_ring *ring;
1212
1213 /* No QOS support yet */
1214 ring = &bgmac->tx_ring[0];
1215 return bgmac_dma_tx_add(bgmac, ring, skb);
1216}
1217
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001218static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1219{
1220 struct bgmac *bgmac = netdev_priv(net_dev);
1221 int ret;
1222
1223 ret = eth_prepare_mac_addr_change(net_dev, addr);
1224 if (ret < 0)
1225 return ret;
1226 bgmac_write_mac_address(bgmac, (u8 *)addr);
1227 eth_commit_mac_addr_change(net_dev, addr);
1228 return 0;
1229}
1230
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001231static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1232{
Hauke Mehrtens69c58852013-12-20 15:34:45 +01001233 if (!netif_running(net_dev))
1234 return -EINVAL;
1235
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001236 return phy_mii_ioctl(net_dev->phydev, ifr, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001237}
1238
1239static const struct net_device_ops bgmac_netdev_ops = {
1240 .ndo_open = bgmac_open,
1241 .ndo_stop = bgmac_stop,
1242 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001243 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001244 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001245 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001246 .ndo_do_ioctl = bgmac_ioctl,
1247};
1248
1249/**************************************************
1250 * ethtool_ops
1251 **************************************************/
1252
Florian Fainellif6613d42016-06-07 15:06:14 -07001253struct bgmac_stat {
1254 u8 size;
1255 u32 offset;
1256 const char *name;
1257};
1258
1259static struct bgmac_stat bgmac_get_strings_stats[] = {
1260 { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
1261 { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
1262 { 8, BGMAC_TX_OCTETS, "tx_octets" },
1263 { 4, BGMAC_TX_PKTS, "tx_pkts" },
1264 { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
1265 { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
1266 { 4, BGMAC_TX_LEN_64, "tx_64" },
1267 { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
1268 { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
1269 { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
1270 { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
1271 { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
1272 { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
1273 { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
1274 { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
1275 { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
1276 { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
1277 { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
1278 { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
1279 { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
1280 { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
1281 { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
1282 { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
1283 { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
1284 { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
1285 { 4, BGMAC_TX_DEFERED, "tx_defered" },
1286 { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
1287 { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
1288 { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
1289 { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
1290 { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
1291 { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
1292 { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
1293 { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
1294 { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
1295 { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
1296 { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
1297 { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
1298 { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
1299 { 8, BGMAC_RX_OCTETS, "rx_octets" },
1300 { 4, BGMAC_RX_PKTS, "rx_pkts" },
1301 { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
1302 { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
1303 { 4, BGMAC_RX_LEN_64, "rx_64" },
1304 { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
1305 { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
1306 { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
1307 { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
1308 { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
1309 { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
1310 { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
1311 { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
1312 { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
1313 { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
1314 { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
1315 { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
1316 { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
1317 { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
1318 { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
1319 { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
1320 { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
1321 { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
1322 { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
1323 { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
1324 { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
1325 { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
1326};
1327
1328#define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
1329
1330static int bgmac_get_sset_count(struct net_device *dev, int string_set)
1331{
1332 switch (string_set) {
1333 case ETH_SS_STATS:
1334 return BGMAC_STATS_LEN;
1335 }
1336
1337 return -EOPNOTSUPP;
1338}
1339
1340static void bgmac_get_strings(struct net_device *dev, u32 stringset,
1341 u8 *data)
1342{
1343 int i;
1344
1345 if (stringset != ETH_SS_STATS)
1346 return;
1347
1348 for (i = 0; i < BGMAC_STATS_LEN; i++)
1349 strlcpy(data + i * ETH_GSTRING_LEN,
1350 bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
1351}
1352
1353static void bgmac_get_ethtool_stats(struct net_device *dev,
1354 struct ethtool_stats *ss, uint64_t *data)
1355{
1356 struct bgmac *bgmac = netdev_priv(dev);
1357 const struct bgmac_stat *s;
1358 unsigned int i;
1359 u64 val;
1360
1361 if (!netif_running(dev))
1362 return;
1363
1364 for (i = 0; i < BGMAC_STATS_LEN; i++) {
1365 s = &bgmac_get_strings_stats[i];
1366 val = 0;
1367 if (s->size == 8)
1368 val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
1369 val |= bgmac_read(bgmac, s->offset);
1370 data[i] = val;
1371 }
1372}
1373
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001374static void bgmac_get_drvinfo(struct net_device *net_dev,
1375 struct ethtool_drvinfo *info)
1376{
1377 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jon Masonf6a95a22016-07-07 19:08:57 -04001378 strlcpy(info->bus_info, "AXI", sizeof(info->bus_info));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001379}
1380
1381static const struct ethtool_ops bgmac_ethtool_ops = {
Florian Fainellif6613d42016-06-07 15:06:14 -07001382 .get_strings = bgmac_get_strings,
1383 .get_sset_count = bgmac_get_sset_count,
1384 .get_ethtool_stats = bgmac_get_ethtool_stats,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001385 .get_drvinfo = bgmac_get_drvinfo,
Philippe Reynes904632a2016-06-19 22:37:06 +02001386 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1387 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001388};
1389
1390/**************************************************
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001391 * MII
1392 **************************************************/
1393
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001394static void bgmac_adjust_link(struct net_device *net_dev)
1395{
1396 struct bgmac *bgmac = netdev_priv(net_dev);
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001397 struct phy_device *phy_dev = net_dev->phydev;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001398 bool update = false;
1399
1400 if (phy_dev->link) {
1401 if (phy_dev->speed != bgmac->mac_speed) {
1402 bgmac->mac_speed = phy_dev->speed;
1403 update = true;
1404 }
1405
1406 if (phy_dev->duplex != bgmac->mac_duplex) {
1407 bgmac->mac_duplex = phy_dev->duplex;
1408 update = true;
1409 }
1410 }
1411
1412 if (update) {
1413 bgmac_mac_speed(bgmac);
1414 phy_print_status(phy_dev);
1415 }
1416}
1417
Jon Mason55954f32016-07-07 19:08:55 -04001418static int bgmac_phy_connect_direct(struct bgmac *bgmac)
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001419{
1420 struct fixed_phy_status fphy_status = {
1421 .link = 1,
1422 .speed = SPEED_1000,
1423 .duplex = DUPLEX_FULL,
1424 };
1425 struct phy_device *phy_dev;
1426 int err;
1427
Fabio Estevam4db78d32015-09-02 13:25:59 -03001428 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001429 if (!phy_dev || IS_ERR(phy_dev)) {
Jon Masond00a8282016-07-07 19:08:53 -04001430 dev_err(bgmac->dev, "Failed to register fixed PHY device\n");
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001431 return -ENODEV;
1432 }
1433
1434 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1435 PHY_INTERFACE_MODE_MII);
1436 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001437 dev_err(bgmac->dev, "Connecting PHY failed\n");
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001438 return err;
1439 }
1440
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001441 return err;
1442}
1443
Jon Mason55954f32016-07-07 19:08:55 -04001444static int bgmac_phy_connect(struct bgmac *bgmac)
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001445{
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001446 struct phy_device *phy_dev;
1447 char bus_id[MII_BUS_ID_SIZE + 3];
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001448
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001449 /* Connect to the PHY */
Jon Mason55954f32016-07-07 19:08:55 -04001450 snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, bgmac->mii_bus->id,
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001451 bgmac->phyaddr);
1452 phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
1453 PHY_INTERFACE_MODE_MII);
1454 if (IS_ERR(phy_dev)) {
Colin Ian Kingc121f72a2016-10-24 23:46:18 +01001455 dev_err(bgmac->dev, "PHY connection failed\n");
Jon Mason55954f32016-07-07 19:08:55 -04001456 return PTR_ERR(phy_dev);
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001457 }
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001458
Jon Mason55954f32016-07-07 19:08:55 -04001459 return 0;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001460}
1461
Jon Masonf6a95a22016-07-07 19:08:57 -04001462int bgmac_enet_probe(struct bgmac *info)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001463{
1464 struct net_device *net_dev;
1465 struct bgmac *bgmac;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001466 int err;
1467
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001468 /* Allocation and references */
1469 net_dev = alloc_etherdev(sizeof(*bgmac));
1470 if (!net_dev)
1471 return -ENOMEM;
Jon Masonf6a95a22016-07-07 19:08:57 -04001472
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001473 net_dev->netdev_ops = &bgmac_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001474 net_dev->ethtool_ops = &bgmac_ethtool_ops;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001475 bgmac = netdev_priv(net_dev);
Jon Masonf6a95a22016-07-07 19:08:57 -04001476 memcpy(bgmac, info, sizeof(*bgmac));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001477 bgmac->net_dev = net_dev;
Jon Masonf6a95a22016-07-07 19:08:57 -04001478 net_dev->irq = bgmac->irq;
1479 SET_NETDEV_DEV(net_dev, bgmac->dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001480
Jon Masonf6a95a22016-07-07 19:08:57 -04001481 if (!is_valid_ether_addr(bgmac->mac_addr)) {
1482 dev_err(bgmac->dev, "Invalid MAC addr: %pM\n",
1483 bgmac->mac_addr);
1484 eth_random_addr(bgmac->mac_addr);
1485 dev_warn(bgmac->dev, "Using random MAC: %pM\n",
1486 bgmac->mac_addr);
1487 }
1488 ether_addr_copy(net_dev->dev_addr, bgmac->mac_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001489
Jon Masonf6a95a22016-07-07 19:08:57 -04001490 /* This (reset &) enable is not preset in specs or reference driver but
1491 * Broadcom does it in arch PCI code when enabling fake PCI device.
1492 */
1493 bgmac_clk_enable(bgmac, 0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001494
Rafał Miłecki1cb94db2016-08-17 23:00:30 +02001495 /* This seems to be fixing IRQ by assigning OOB #6 to the core */
1496 if (bgmac->feature_flags & BGMAC_FEAT_IRQ_ID_OOB_6)
1497 bgmac_idm_write(bgmac, BCMA_OOB_SEL_OUT_A30, 0x86);
1498
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001499 bgmac_chip_reset(bgmac);
1500
1501 err = bgmac_dma_alloc(bgmac);
1502 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001503 dev_err(bgmac->dev, "Unable to alloc memory for DMA\n");
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001504 goto err_netdev_free;
1505 }
1506
1507 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
Ralf Baechleedb15d82013-02-21 16:16:55 +01001508 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001509 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1510
Hauke Mehrtens62166422015-01-18 19:49:58 +01001511 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1512
Jon Mason55954f32016-07-07 19:08:55 -04001513 if (!bgmac->mii_bus)
1514 err = bgmac_phy_connect_direct(bgmac);
1515 else
1516 err = bgmac_phy_connect(bgmac);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001517 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001518 dev_err(bgmac->dev, "Cannot connect to phy\n");
Jon Masonf6a95a22016-07-07 19:08:57 -04001519 goto err_dma_free;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001520 }
1521
Felix Fietkau9cde9452015-03-23 12:35:37 +01001522 net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1523 net_dev->hw_features = net_dev->features;
1524 net_dev->vlan_features = net_dev->features;
1525
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001526 err = register_netdev(bgmac->net_dev);
1527 if (err) {
Jon Masond00a8282016-07-07 19:08:53 -04001528 dev_err(bgmac->dev, "Cannot register net device\n");
Jon Mason55954f32016-07-07 19:08:55 -04001529 goto err_phy_disconnect;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001530 }
1531
1532 netif_carrier_off(net_dev);
1533
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001534 return 0;
1535
Jon Mason55954f32016-07-07 19:08:55 -04001536err_phy_disconnect:
1537 phy_disconnect(net_dev->phydev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001538err_dma_free:
1539 bgmac_dma_free(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001540err_netdev_free:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001541 free_netdev(net_dev);
1542
1543 return err;
1544}
Jon Masonf6a95a22016-07-07 19:08:57 -04001545EXPORT_SYMBOL_GPL(bgmac_enet_probe);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001546
Jon Masonf6a95a22016-07-07 19:08:57 -04001547void bgmac_enet_remove(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001548{
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001549 unregister_netdev(bgmac->net_dev);
Jon Mason55954f32016-07-07 19:08:55 -04001550 phy_disconnect(bgmac->net_dev->phydev);
Hauke Mehrtens62166422015-01-18 19:49:58 +01001551 netif_napi_del(&bgmac->napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001552 bgmac_dma_free(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001553 free_netdev(bgmac->net_dev);
1554}
Jon Masonf6a95a22016-07-07 19:08:57 -04001555EXPORT_SYMBOL_GPL(bgmac_enet_remove);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001556
1557MODULE_AUTHOR("Rafał Miłecki");
1558MODULE_LICENSE("GPL");