blob: 9c3b2ff33e6308e6838657cc9e1d0b72319abfa6 [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
9#include "bgmac.h"
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
Rafał Miłecki11e5e762013-03-07 01:53:28 +000016#include <linux/phy.h>
Rafał Miłeckic25b23b2015-03-20 23:14:31 +010017#include <linux/phy_fixed.h>
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000018#include <linux/interrupt.h>
19#include <linux/dma-mapping.h>
Ralf Baechleedb15d82013-02-21 16:16:55 +010020#include <bcm47xx_nvram.h>
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000021
22static const struct bcma_device_id bgmac_bcma_tbl[] = {
23 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_4706_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
24 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
Joe Perchesf7219b52015-02-10 12:55:03 -080025 {},
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000026};
27MODULE_DEVICE_TABLE(bcma, bgmac_bcma_tbl);
28
29static bool bgmac_wait_value(struct bcma_device *core, u16 reg, u32 mask,
30 u32 value, int timeout)
31{
32 u32 val;
33 int i;
34
35 for (i = 0; i < timeout / 10; i++) {
36 val = bcma_read32(core, reg);
37 if ((val & mask) == value)
38 return true;
39 udelay(10);
40 }
41 pr_err("Timeout waiting for reg 0x%X\n", reg);
42 return false;
43}
44
45/**************************************************
46 * DMA
47 **************************************************/
48
49static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
50{
51 u32 val;
52 int i;
53
54 if (!ring->mmio_base)
55 return;
56
57 /* Suspend DMA TX ring first.
58 * bgmac_wait_value doesn't support waiting for any of few values, so
59 * implement whole loop here.
60 */
61 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
62 BGMAC_DMA_TX_SUSPEND);
63 for (i = 0; i < 10000 / 10; i++) {
64 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
65 val &= BGMAC_DMA_TX_STAT;
66 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
67 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
68 val == BGMAC_DMA_TX_STAT_STOPPED) {
69 i = 0;
70 break;
71 }
72 udelay(10);
73 }
74 if (i)
75 bgmac_err(bgmac, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
76 ring->mmio_base, val);
77
78 /* Remove SUSPEND bit */
79 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
80 if (!bgmac_wait_value(bgmac->core,
81 ring->mmio_base + BGMAC_DMA_TX_STATUS,
82 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
83 10000)) {
84 bgmac_warn(bgmac, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
85 ring->mmio_base);
86 udelay(300);
87 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
88 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
89 bgmac_err(bgmac, "Reset of DMA TX ring 0x%X failed\n",
90 ring->mmio_base);
91 }
92}
93
94static void bgmac_dma_tx_enable(struct bgmac *bgmac,
95 struct bgmac_dma_ring *ring)
96{
97 u32 ctl;
98
99 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100100 if (bgmac->core->id.rev >= 4) {
101 ctl &= ~BGMAC_DMA_TX_BL_MASK;
102 ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
103
104 ctl &= ~BGMAC_DMA_TX_MR_MASK;
105 ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
106
107 ctl &= ~BGMAC_DMA_TX_PC_MASK;
108 ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
109
110 ctl &= ~BGMAC_DMA_TX_PT_MASK;
111 ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
112 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000113 ctl |= BGMAC_DMA_TX_ENABLE;
114 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
115 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
116}
117
Felix Fietkau9cde9452015-03-23 12:35:37 +0100118static void
119bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
120 int i, int len, u32 ctl0)
121{
122 struct bgmac_slot_info *slot;
123 struct bgmac_dma_desc *dma_desc;
124 u32 ctl1;
125
126 if (i == ring->num_slots - 1)
127 ctl0 |= BGMAC_DESC_CTL0_EOT;
128
129 ctl1 = len & BGMAC_DESC_CTL1_LEN;
130
131 slot = &ring->slots[i];
132 dma_desc = &ring->cpu_base[i];
133 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
134 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
135 dma_desc->ctl0 = cpu_to_le32(ctl0);
136 dma_desc->ctl1 = cpu_to_le32(ctl1);
137}
138
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000139static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
140 struct bgmac_dma_ring *ring,
141 struct sk_buff *skb)
142{
143 struct device *dma_dev = bgmac->core->dma_dev;
144 struct net_device *net_dev = bgmac->net_dev;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200145 int index = ring->end % BGMAC_TX_RING_SLOTS;
146 struct bgmac_slot_info *slot = &ring->slots[index];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100147 int nr_frags;
148 u32 flags;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100149 int i;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000150
151 if (skb->len > BGMAC_DESC_CTL1_LEN) {
152 bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100153 goto err_drop;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000154 }
155
Felix Fietkau9cde9452015-03-23 12:35:37 +0100156 if (skb->ip_summed == CHECKSUM_PARTIAL)
157 skb_checksum_help(skb);
158
159 nr_frags = skb_shinfo(skb)->nr_frags;
160
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200161 /* ring->end - ring->start will return the number of valid slots,
162 * even when ring->end overflows
163 */
164 if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000165 bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
166 netif_stop_queue(net_dev);
167 return NETDEV_TX_BUSY;
168 }
169
Felix Fietkau9cde9452015-03-23 12:35:37 +0100170 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000171 DMA_TO_DEVICE);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100172 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
173 goto err_dma_head;
174
175 flags = BGMAC_DESC_CTL0_SOF;
176 if (!nr_frags)
177 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
178
179 bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
180 flags = 0;
181
182 for (i = 0; i < nr_frags; i++) {
183 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
184 int len = skb_frag_size(frag);
185
186 index = (index + 1) % BGMAC_TX_RING_SLOTS;
187 slot = &ring->slots[index];
188 slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
189 len, DMA_TO_DEVICE);
190 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
191 goto err_dma;
192
193 if (i == nr_frags - 1)
194 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
195
196 bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000197 }
198
Felix Fietkau9cde9452015-03-23 12:35:37 +0100199 slot->skb = skb;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200200 ring->end += nr_frags + 1;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200201 netdev_sent_queue(net_dev, skb->len);
202
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000203 wmb();
204
205 /* Increase ring->end to point empty slot. We tell hardware the first
206 * slot it should *not* read.
207 */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000208 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
Rafał Miłecki99003032013-09-15 23:13:18 +0200209 ring->index_base +
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200210 (ring->end % BGMAC_TX_RING_SLOTS) *
211 sizeof(struct bgmac_dma_desc));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000212
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200213 if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000214 netif_stop_queue(net_dev);
215
216 return NETDEV_TX_OK;
217
Felix Fietkau9cde9452015-03-23 12:35:37 +0100218err_dma:
219 dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
220 DMA_TO_DEVICE);
221
222 while (i > 0) {
223 int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
224 struct bgmac_slot_info *slot = &ring->slots[index];
225 u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
226 int len = ctl1 & BGMAC_DESC_CTL1_LEN;
227
228 dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
229 }
230
231err_dma_head:
232 bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n",
233 ring->mmio_base);
234
235err_drop:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000236 dev_kfree_skb(skb);
237 return NETDEV_TX_OK;
238}
239
240/* Free transmitted packets */
241static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
242{
243 struct device *dma_dev = bgmac->core->dma_dev;
244 int empty_slot;
245 bool freed = false;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200246 unsigned bytes_compl = 0, pkts_compl = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000247
248 /* The last slot that hardware didn't consume yet */
249 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
250 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200251 empty_slot -= ring->index_base;
252 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000253 empty_slot /= sizeof(struct bgmac_dma_desc);
254
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200255 while (ring->start != ring->end) {
256 int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
257 struct bgmac_slot_info *slot = &ring->slots[slot_idx];
258 u32 ctl1;
259 int len;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100260
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200261 if (slot_idx == empty_slot)
262 break;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100263
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200264 ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
265 len = ctl1 & BGMAC_DESC_CTL1_LEN;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100266 if (ctl1 & BGMAC_DESC_CTL0_SOF)
267 /* Unmap no longer used buffer */
268 dma_unmap_single(dma_dev, slot->dma_addr, len,
269 DMA_TO_DEVICE);
270 else
271 dma_unmap_page(dma_dev, slot->dma_addr, len,
272 DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000273
274 if (slot->skb) {
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200275 bytes_compl += slot->skb->len;
276 pkts_compl++;
277
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000278 /* Free memory! :) */
279 dev_kfree_skb(slot->skb);
280 slot->skb = NULL;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000281 }
282
Felix Fietkau9cde9452015-03-23 12:35:37 +0100283 slot->dma_addr = 0;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200284 ring->start++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000285 freed = true;
286 }
287
Felix Fietkau9cde9452015-03-23 12:35:37 +0100288 if (!pkts_compl)
289 return;
290
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200291 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
292
Felix Fietkau9cde9452015-03-23 12:35:37 +0100293 if (netif_queue_stopped(bgmac->net_dev))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000294 netif_wake_queue(bgmac->net_dev);
295}
296
297static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
298{
299 if (!ring->mmio_base)
300 return;
301
302 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
303 if (!bgmac_wait_value(bgmac->core,
304 ring->mmio_base + BGMAC_DMA_RX_STATUS,
305 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
306 10000))
307 bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
308 ring->mmio_base);
309}
310
311static void bgmac_dma_rx_enable(struct bgmac *bgmac,
312 struct bgmac_dma_ring *ring)
313{
314 u32 ctl;
315
316 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100317 if (bgmac->core->id.rev >= 4) {
318 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_ADDREXT_MASK;
328 ctl |= BGMAC_DMA_RX_ENABLE;
329 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
330 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
331 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
332 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
333}
334
335static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
336 struct bgmac_slot_info *slot)
337{
338 struct device *dma_dev = bgmac->core->dma_dev;
Nathan Hintzb757a622013-10-29 19:32:01 -0700339 dma_addr_t dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000340 struct bgmac_rx_header *rx;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100341 void *buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000342
343 /* Alloc skb */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100344 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
345 if (!buf)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000346 return -ENOMEM;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000347
348 /* Poison - if everything goes fine, hardware will overwrite it */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100349 rx = buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000350 rx->len = cpu_to_le16(0xdead);
351 rx->flags = cpu_to_le16(0xbeef);
352
353 /* Map skb for the DMA */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100354 dma_addr = dma_map_single(dma_dev, buf, BGMAC_RX_BUF_SIZE,
355 DMA_FROM_DEVICE);
Nathan Hintzb757a622013-10-29 19:32:01 -0700356 if (dma_mapping_error(dma_dev, dma_addr)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000357 bgmac_err(bgmac, "DMA mapping error\n");
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100358 put_page(virt_to_head_page(buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000359 return -ENOMEM;
360 }
Nathan Hintzb757a622013-10-29 19:32:01 -0700361
362 /* Update the slot */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100363 slot->buf = buf;
Nathan Hintzb757a622013-10-29 19:32:01 -0700364 slot->dma_addr = dma_addr;
365
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000366 return 0;
367}
368
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100369static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
370 struct bgmac_dma_ring *ring, int desc_idx)
371{
372 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
373 u32 ctl0 = 0, ctl1 = 0;
374
375 if (desc_idx == ring->num_slots - 1)
376 ctl0 |= BGMAC_DESC_CTL0_EOT;
377 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
378 /* Is there any BGMAC device that requires extension? */
379 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
380 * B43_DMA64_DCTL1_ADDREXT_MASK;
381 */
382
383 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
384 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
385 dma_desc->ctl0 = cpu_to_le32(ctl0);
386 dma_desc->ctl1 = cpu_to_le32(ctl1);
387}
388
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000389static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
390 int weight)
391{
392 u32 end_slot;
393 int handled = 0;
394
395 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
396 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200397 end_slot -= ring->index_base;
398 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000399 end_slot /= sizeof(struct bgmac_dma_desc);
400
401 ring->end = end_slot;
402
403 while (ring->start != ring->end) {
404 struct device *dma_dev = bgmac->core->dma_dev;
405 struct bgmac_slot_info *slot = &ring->slots[ring->start];
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100406 struct bgmac_rx_header *rx = slot->buf;
407 struct sk_buff *skb;
408 void *buf = slot->buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000409 u16 len, flags;
410
411 /* Unmap buffer to make it accessible to the CPU */
412 dma_sync_single_for_cpu(dma_dev, slot->dma_addr,
413 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
414
415 /* Get info from the header */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000416 len = le16_to_cpu(rx->len);
417 flags = le16_to_cpu(rx->flags);
418
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100419 do {
420 dma_addr_t old_dma_addr = slot->dma_addr;
421 int err;
422
423 /* Check for poison and drop or pass the packet */
424 if (len == 0xdead && flags == 0xbeef) {
425 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
426 ring->start);
427 dma_sync_single_for_device(dma_dev,
428 slot->dma_addr,
429 BGMAC_RX_BUF_SIZE,
430 DMA_FROM_DEVICE);
431 break;
432 }
433
Hauke Mehrtens02e71122013-02-28 07:16:54 +0000434 /* Omit CRC. */
435 len -= ETH_FCS_LEN;
436
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100437 /* Prepare new skb as replacement */
438 err = bgmac_dma_rx_skb_for_slot(bgmac, slot);
439 if (err) {
440 /* Poison the old skb */
441 rx->len = cpu_to_le16(0xdead);
442 rx->flags = cpu_to_le16(0xbeef);
443
444 dma_sync_single_for_device(dma_dev,
445 slot->dma_addr,
446 BGMAC_RX_BUF_SIZE,
447 DMA_FROM_DEVICE);
448 break;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000449 }
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100450 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000451
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100452 /* Unmap old skb, we'll pass it to the netfif */
453 dma_unmap_single(dma_dev, old_dma_addr,
454 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000455
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100456 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100457 skb_put(skb, BGMAC_RX_FRAME_OFFSET + len);
458 skb_pull(skb, BGMAC_RX_FRAME_OFFSET);
459
460 skb_checksum_none_assert(skb);
461 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100462 napi_gro_receive(&bgmac->napi, skb);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100463 handled++;
464 } while (0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000465
466 if (++ring->start >= BGMAC_RX_RING_SLOTS)
467 ring->start = 0;
468
469 if (handled >= weight) /* Should never be greater */
470 break;
471 }
472
473 return handled;
474}
475
476/* Does ring support unaligned addressing? */
477static bool bgmac_dma_unaligned(struct bgmac *bgmac,
478 struct bgmac_dma_ring *ring,
479 enum bgmac_dma_ring_type ring_type)
480{
481 switch (ring_type) {
482 case BGMAC_DMA_RING_TX:
483 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
484 0xff0);
485 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
486 return true;
487 break;
488 case BGMAC_DMA_RING_RX:
489 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
490 0xff0);
491 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
492 return true;
493 break;
494 }
495 return false;
496}
497
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100498static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
499 struct bgmac_dma_ring *ring)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000500{
501 struct device *dma_dev = bgmac->core->dma_dev;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100502 struct bgmac_dma_desc *dma_desc = ring->cpu_base;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000503 struct bgmac_slot_info *slot;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000504 int i;
505
506 for (i = 0; i < ring->num_slots; i++) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100507 int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN;
508
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000509 slot = &ring->slots[i];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100510 dev_kfree_skb(slot->skb);
511
512 if (!slot->dma_addr)
513 continue;
514
515 if (slot->skb)
516 dma_unmap_single(dma_dev, slot->dma_addr,
517 len, DMA_TO_DEVICE);
518 else
519 dma_unmap_page(dma_dev, slot->dma_addr,
520 len, DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000521 }
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100522}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000523
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100524static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
525 struct bgmac_dma_ring *ring)
526{
527 struct device *dma_dev = bgmac->core->dma_dev;
528 struct bgmac_slot_info *slot;
529 int i;
530
531 for (i = 0; i < ring->num_slots; i++) {
532 slot = &ring->slots[i];
533 if (!slot->buf)
534 continue;
535
536 if (slot->dma_addr)
537 dma_unmap_single(dma_dev, slot->dma_addr,
538 BGMAC_RX_BUF_SIZE,
539 DMA_FROM_DEVICE);
540 put_page(virt_to_head_page(slot->buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000541 }
542}
543
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100544static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
545 struct bgmac_dma_ring *ring)
546{
547 struct device *dma_dev = bgmac->core->dma_dev;
548 int size;
549
550 if (!ring->cpu_base)
551 return;
552
553 /* Free ring of descriptors */
554 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
555 dma_free_coherent(dma_dev, size, ring->cpu_base,
556 ring->dma_base);
557}
558
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000559static void bgmac_dma_free(struct bgmac *bgmac)
560{
561 int i;
562
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100563 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
564 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
565 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i]);
566 }
567 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
568 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
569 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i]);
570 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000571}
572
573static int bgmac_dma_alloc(struct bgmac *bgmac)
574{
575 struct device *dma_dev = bgmac->core->dma_dev;
576 struct bgmac_dma_ring *ring;
577 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
578 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
579 int size; /* ring size: different for Tx and Rx */
580 int err;
581 int i;
582
583 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
584 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
585
586 if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
587 bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
588 return -ENOTSUPP;
589 }
590
591 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
592 ring = &bgmac->tx_ring[i];
593 ring->num_slots = BGMAC_TX_RING_SLOTS;
594 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000595
596 /* Alloc ring of descriptors */
597 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
598 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
599 &ring->dma_base,
600 GFP_KERNEL);
601 if (!ring->cpu_base) {
602 bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
603 ring->mmio_base);
604 goto err_dma_free;
605 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000606
Rafał Miłecki99003032013-09-15 23:13:18 +0200607 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
608 BGMAC_DMA_RING_TX);
609 if (ring->unaligned)
610 ring->index_base = lower_32_bits(ring->dma_base);
611 else
612 ring->index_base = 0;
613
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000614 /* No need to alloc TX slots yet */
615 }
616
617 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000618 int j;
619
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000620 ring = &bgmac->rx_ring[i];
621 ring->num_slots = BGMAC_RX_RING_SLOTS;
622 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000623
624 /* Alloc ring of descriptors */
625 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
626 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
627 &ring->dma_base,
628 GFP_KERNEL);
629 if (!ring->cpu_base) {
630 bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
631 ring->mmio_base);
632 err = -ENOMEM;
633 goto err_dma_free;
634 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000635
Rafał Miłecki99003032013-09-15 23:13:18 +0200636 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
637 BGMAC_DMA_RING_RX);
638 if (ring->unaligned)
639 ring->index_base = lower_32_bits(ring->dma_base);
640 else
641 ring->index_base = 0;
642
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000643 /* Alloc RX slots */
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000644 for (j = 0; j < ring->num_slots; j++) {
645 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000646 if (err) {
647 bgmac_err(bgmac, "Can't allocate skb for slot in RX ring\n");
648 goto err_dma_free;
649 }
650 }
651 }
652
653 return 0;
654
655err_dma_free:
656 bgmac_dma_free(bgmac);
657 return -ENOMEM;
658}
659
660static void bgmac_dma_init(struct bgmac *bgmac)
661{
662 struct bgmac_dma_ring *ring;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000663 int i;
664
665 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
666 ring = &bgmac->tx_ring[i];
667
Rafał Miłecki99003032013-09-15 23:13:18 +0200668 if (!ring->unaligned)
669 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000670 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
671 lower_32_bits(ring->dma_base));
672 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
673 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200674 if (ring->unaligned)
675 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000676
677 ring->start = 0;
678 ring->end = 0; /* Points the slot that should *not* be read */
679 }
680
681 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000682 int j;
683
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000684 ring = &bgmac->rx_ring[i];
685
Rafał Miłecki99003032013-09-15 23:13:18 +0200686 if (!ring->unaligned)
687 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000688 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
689 lower_32_bits(ring->dma_base));
690 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
691 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200692 if (ring->unaligned)
693 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000694
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100695 for (j = 0; j < ring->num_slots; j++)
696 bgmac_dma_rx_setup_desc(bgmac, ring, j);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000697
698 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
Rafał Miłecki99003032013-09-15 23:13:18 +0200699 ring->index_base +
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000700 ring->num_slots * sizeof(struct bgmac_dma_desc));
701
702 ring->start = 0;
703 ring->end = 0;
704 }
705}
706
707/**************************************************
708 * PHY ops
709 **************************************************/
710
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000711static u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000712{
713 struct bcma_device *core;
714 u16 phy_access_addr;
715 u16 phy_ctl_addr;
716 u32 tmp;
717
718 BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
719 BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
720 BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
721 BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
722 BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
723 BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
724 BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
725 BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
726 BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
727 BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
728 BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
729
730 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
731 core = bgmac->core->bus->drv_gmac_cmn.core;
732 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
733 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
734 } else {
735 core = bgmac->core;
736 phy_access_addr = BGMAC_PHY_ACCESS;
737 phy_ctl_addr = BGMAC_PHY_CNTL;
738 }
739
740 tmp = bcma_read32(core, phy_ctl_addr);
741 tmp &= ~BGMAC_PC_EPA_MASK;
742 tmp |= phyaddr;
743 bcma_write32(core, phy_ctl_addr, tmp);
744
745 tmp = BGMAC_PA_START;
746 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
747 tmp |= reg << BGMAC_PA_REG_SHIFT;
748 bcma_write32(core, phy_access_addr, tmp);
749
750 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
751 bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
752 phyaddr, reg);
753 return 0xffff;
754 }
755
756 return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
757}
758
759/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000760static int bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000761{
762 struct bcma_device *core;
763 u16 phy_access_addr;
764 u16 phy_ctl_addr;
765 u32 tmp;
766
767 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
768 core = bgmac->core->bus->drv_gmac_cmn.core;
769 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
770 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
771 } else {
772 core = bgmac->core;
773 phy_access_addr = BGMAC_PHY_ACCESS;
774 phy_ctl_addr = BGMAC_PHY_CNTL;
775 }
776
777 tmp = bcma_read32(core, phy_ctl_addr);
778 tmp &= ~BGMAC_PC_EPA_MASK;
779 tmp |= phyaddr;
780 bcma_write32(core, phy_ctl_addr, tmp);
781
782 bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
783 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
784 bgmac_warn(bgmac, "Error setting MDIO int\n");
785
786 tmp = BGMAC_PA_START;
787 tmp |= BGMAC_PA_WRITE;
788 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
789 tmp |= reg << BGMAC_PA_REG_SHIFT;
790 tmp |= value;
791 bcma_write32(core, phy_access_addr, tmp);
792
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000793 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000794 bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
795 phyaddr, reg);
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000796 return -ETIMEDOUT;
797 }
798
799 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000800}
801
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000802/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
803static void bgmac_phy_init(struct bgmac *bgmac)
804{
805 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
806 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
807 u8 i;
808
809 if (ci->id == BCMA_CHIP_ID_BCM5356) {
810 for (i = 0; i < 5; i++) {
811 bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
812 bgmac_phy_write(bgmac, i, 0x15, 0x0100);
813 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
814 bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
815 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
816 }
817 }
818 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
819 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
820 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
821 bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
822 bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
823 for (i = 0; i < 5; i++) {
824 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
825 bgmac_phy_write(bgmac, i, 0x16, 0x5284);
826 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
827 bgmac_phy_write(bgmac, i, 0x17, 0x0010);
828 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
829 bgmac_phy_write(bgmac, i, 0x16, 0x5296);
830 bgmac_phy_write(bgmac, i, 0x17, 0x1073);
831 bgmac_phy_write(bgmac, i, 0x17, 0x9073);
832 bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
833 bgmac_phy_write(bgmac, i, 0x17, 0x9273);
834 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
835 }
836 }
837}
838
839/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
840static void bgmac_phy_reset(struct bgmac *bgmac)
841{
842 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
843 return;
844
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100845 bgmac_phy_write(bgmac, bgmac->phyaddr, MII_BMCR, BMCR_RESET);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000846 udelay(100);
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100847 if (bgmac_phy_read(bgmac, bgmac->phyaddr, MII_BMCR) & BMCR_RESET)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000848 bgmac_err(bgmac, "PHY reset failed\n");
849 bgmac_phy_init(bgmac);
850}
851
852/**************************************************
853 * Chip ops
854 **************************************************/
855
856/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
857 * nothing to change? Try if after stabilizng driver.
858 */
859static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
860 bool force)
861{
862 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
863 u32 new_val = (cmdcfg & mask) | set;
864
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100865 bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000866 udelay(2);
867
868 if (new_val != cmdcfg || force)
869 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
870
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100871 bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000872 udelay(2);
873}
874
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000875static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
876{
877 u32 tmp;
878
879 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
880 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
881 tmp = (addr[4] << 8) | addr[5];
882 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
883}
884
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000885static void bgmac_set_rx_mode(struct net_device *net_dev)
886{
887 struct bgmac *bgmac = netdev_priv(net_dev);
888
889 if (net_dev->flags & IFF_PROMISC)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000890 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000891 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000892 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000893}
894
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000895#if 0 /* We don't use that regs yet */
896static void bgmac_chip_stats_update(struct bgmac *bgmac)
897{
898 int i;
899
900 if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
901 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
902 bgmac->mib_tx_regs[i] =
903 bgmac_read(bgmac,
904 BGMAC_TX_GOOD_OCTETS + (i * 4));
905 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
906 bgmac->mib_rx_regs[i] =
907 bgmac_read(bgmac,
908 BGMAC_RX_GOOD_OCTETS + (i * 4));
909 }
910
911 /* TODO: what else? how to handle BCM4706? Specs are needed */
912}
913#endif
914
915static void bgmac_clear_mib(struct bgmac *bgmac)
916{
917 int i;
918
919 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
920 return;
921
922 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
923 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
924 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
925 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
926 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
927}
928
929/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100930static void bgmac_mac_speed(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000931{
932 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
933 u32 set = 0;
934
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100935 switch (bgmac->mac_speed) {
936 case SPEED_10:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000937 set |= BGMAC_CMDCFG_ES_10;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100938 break;
939 case SPEED_100:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000940 set |= BGMAC_CMDCFG_ES_100;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100941 break;
942 case SPEED_1000:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000943 set |= BGMAC_CMDCFG_ES_1000;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100944 break;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100945 case SPEED_2500:
946 set |= BGMAC_CMDCFG_ES_2500;
947 break;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100948 default:
949 bgmac_err(bgmac, "Unsupported speed: %d\n", bgmac->mac_speed);
950 }
951
952 if (bgmac->mac_duplex == DUPLEX_HALF)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000953 set |= BGMAC_CMDCFG_HD;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100954
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000955 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
956}
957
958static void bgmac_miiconfig(struct bgmac *bgmac)
959{
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100960 struct bcma_device *core = bgmac->core;
961 struct bcma_chipinfo *ci = &core->bus->chipinfo;
962 u8 imode;
963
964 if (ci->id == BCMA_CHIP_ID_BCM4707 ||
965 ci->id == BCMA_CHIP_ID_BCM53018) {
966 bcma_awrite32(core, BCMA_IOCTL,
967 bcma_aread32(core, BCMA_IOCTL) | 0x40 |
968 BGMAC_BCMA_IOCTL_SW_CLKEN);
969 bgmac->mac_speed = SPEED_2500;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100970 bgmac->mac_duplex = DUPLEX_FULL;
971 bgmac_mac_speed(bgmac);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100972 } else {
973 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
974 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
975 if (imode == 0 || imode == 1) {
976 bgmac->mac_speed = SPEED_100;
977 bgmac->mac_duplex = DUPLEX_FULL;
978 bgmac_mac_speed(bgmac);
979 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000980 }
981}
982
983/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
984static void bgmac_chip_reset(struct bgmac *bgmac)
985{
986 struct bcma_device *core = bgmac->core;
987 struct bcma_bus *bus = core->bus;
988 struct bcma_chipinfo *ci = &bus->chipinfo;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100989 u32 flags;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000990 u32 iost;
991 int i;
992
993 if (bcma_core_is_enabled(core)) {
994 if (!bgmac->stats_grabbed) {
995 /* bgmac_chip_stats_update(bgmac); */
996 bgmac->stats_grabbed = true;
997 }
998
999 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
1000 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
1001
1002 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
1003 udelay(1);
1004
1005 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
1006 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
1007
1008 /* TODO: Clear software multicast filter list */
1009 }
1010
1011 iost = bcma_aread32(core, BCMA_IOST);
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001012 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001013 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001014 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001015 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
1016
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001017 /* 3GMAC: for BCM4707, only do core reset at bgmac_probe() */
1018 if (ci->id != BCMA_CHIP_ID_BCM4707) {
1019 flags = 0;
1020 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
1021 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
1022 if (!bgmac->has_robosw)
1023 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
1024 }
1025 bcma_core_enable(core, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001026 }
1027
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001028 /* Request Misc PLL for corerev > 2 */
1029 if (core->id.rev > 2 &&
1030 ci->id != BCMA_CHIP_ID_BCM4707 &&
1031 ci->id != BCMA_CHIP_ID_BCM53018) {
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001032 bgmac_set(bgmac, BCMA_CLKCTLST,
1033 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
1034 bgmac_wait_value(bgmac->core, BCMA_CLKCTLST,
1035 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
1036 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001037 1000);
1038 }
1039
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001040 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1041 ci->id == BCMA_CHIP_ID_BCM4749 ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001042 ci->id == BCMA_CHIP_ID_BCM53572) {
1043 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
1044 u8 et_swtype = 0;
1045 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
Rafał Miłecki6a391e72013-09-15 00:22:47 +02001046 BGMAC_CHIPCTL_1_IF_TYPE_MII;
Hauke Mehrtens36472682013-09-15 22:49:08 +02001047 char buf[4];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001048
Hauke Mehrtens36472682013-09-15 22:49:08 +02001049 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001050 if (kstrtou8(buf, 0, &et_swtype))
1051 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
1052 buf);
1053 et_swtype &= 0x0f;
1054 et_swtype <<= 4;
1055 sw_type = et_swtype;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001056 } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM5358) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001057 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001058 } else if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
1059 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
1060 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188)) {
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +00001061 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
1062 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001063 }
1064 bcma_chipco_chipctl_maskset(cc, 1,
1065 ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
1066 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
1067 sw_type);
1068 }
1069
1070 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
1071 bcma_awrite32(core, BCMA_IOCTL,
1072 bcma_aread32(core, BCMA_IOCTL) &
1073 ~BGMAC_BCMA_IOCTL_SW_RESET);
1074
1075 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
1076 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
1077 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
1078 * be keps until taking MAC out of the reset.
1079 */
1080 bgmac_cmdcfg_maskset(bgmac,
1081 ~(BGMAC_CMDCFG_TE |
1082 BGMAC_CMDCFG_RE |
1083 BGMAC_CMDCFG_RPI |
1084 BGMAC_CMDCFG_TAI |
1085 BGMAC_CMDCFG_HD |
1086 BGMAC_CMDCFG_ML |
1087 BGMAC_CMDCFG_CFE |
1088 BGMAC_CMDCFG_RL |
1089 BGMAC_CMDCFG_RED |
1090 BGMAC_CMDCFG_PE |
1091 BGMAC_CMDCFG_TPI |
1092 BGMAC_CMDCFG_PAD_EN |
1093 BGMAC_CMDCFG_PF),
1094 BGMAC_CMDCFG_PROM |
1095 BGMAC_CMDCFG_NLC |
1096 BGMAC_CMDCFG_CFE |
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001097 BGMAC_CMDCFG_SR(core->id.rev),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001098 false);
Rafał Miłeckid4699622013-12-11 07:44:14 +01001099 bgmac->mac_speed = SPEED_UNKNOWN;
1100 bgmac->mac_duplex = DUPLEX_UNKNOWN;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001101
1102 bgmac_clear_mib(bgmac);
1103 if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
1104 bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
1105 BCMA_GMAC_CMN_PC_MTE);
1106 else
1107 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1108 bgmac_miiconfig(bgmac);
1109 bgmac_phy_init(bgmac);
1110
Hauke Mehrtens49a467b2013-09-29 13:54:58 +02001111 netdev_reset_queue(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001112}
1113
1114static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1115{
1116 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1117}
1118
1119static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1120{
1121 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
Nathan Hintz41608152013-02-13 19:14:10 +00001122 bgmac_read(bgmac, BGMAC_INT_MASK);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001123}
1124
1125/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1126static void bgmac_enable(struct bgmac *bgmac)
1127{
1128 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
1129 u32 cmdcfg;
1130 u32 mode;
1131 u32 rxq_ctl;
1132 u32 fl_ctl;
1133 u16 bp_clk;
1134 u8 mdp;
1135
1136 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
1137 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001138 BGMAC_CMDCFG_SR(bgmac->core->id.rev), true);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001139 udelay(2);
1140 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1141 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1142
1143 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1144 BGMAC_DS_MM_SHIFT;
1145 if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
1146 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1147 if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
1148 bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
1149 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1150
1151 switch (ci->id) {
1152 case BCMA_CHIP_ID_BCM5357:
1153 case BCMA_CHIP_ID_BCM4749:
1154 case BCMA_CHIP_ID_BCM53572:
1155 case BCMA_CHIP_ID_BCM4716:
1156 case BCMA_CHIP_ID_BCM47162:
1157 fl_ctl = 0x03cb04cb;
1158 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1159 ci->id == BCMA_CHIP_ID_BCM4749 ||
1160 ci->id == BCMA_CHIP_ID_BCM53572)
1161 fl_ctl = 0x2300e1;
1162 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1163 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
1164 break;
1165 }
1166
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001167 if (ci->id != BCMA_CHIP_ID_BCM4707 &&
1168 ci->id != BCMA_CHIP_ID_BCM53018) {
1169 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1170 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1171 bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) /
1172 1000000;
1173 mdp = (bp_clk * 128 / 1000) - 3;
1174 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1175 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1176 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001177}
1178
1179/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
1180static void bgmac_chip_init(struct bgmac *bgmac, bool full_init)
1181{
1182 struct bgmac_dma_ring *ring;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001183 int i;
1184
1185 /* 1 interrupt per received frame */
1186 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1187
1188 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1189 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1190
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001191 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001192
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001193 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001194
1195 if (bgmac->loopback)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001196 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001197 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001198 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001199
1200 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1201
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001202 if (full_init) {
1203 bgmac_dma_init(bgmac);
1204 if (1) /* FIXME: is there any case we don't want IRQs? */
1205 bgmac_chip_intrs_on(bgmac);
1206 } else {
1207 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
1208 ring = &bgmac->rx_ring[i];
1209 bgmac_dma_rx_enable(bgmac, ring);
1210 }
1211 }
1212
1213 bgmac_enable(bgmac);
1214}
1215
1216static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1217{
1218 struct bgmac *bgmac = netdev_priv(dev_id);
1219
1220 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1221 int_status &= bgmac->int_mask;
1222
1223 if (!int_status)
1224 return IRQ_NONE;
1225
Felix Fietkaueb64e292015-04-14 12:07:55 +02001226 int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1227 if (int_status)
1228 bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", int_status);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001229
1230 /* Disable new interrupts until handling existing ones */
1231 bgmac_chip_intrs_off(bgmac);
1232
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001233 napi_schedule(&bgmac->napi);
1234
1235 return IRQ_HANDLED;
1236}
1237
1238static int bgmac_poll(struct napi_struct *napi, int weight)
1239{
1240 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001241 int handled = 0;
1242
Felix Fietkaueb64e292015-04-14 12:07:55 +02001243 /* Ack */
1244 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001245
Felix Fietkaueb64e292015-04-14 12:07:55 +02001246 bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1247 handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001248
Felix Fietkaueb64e292015-04-14 12:07:55 +02001249 /* Poll again if more events arrived in the meantime */
1250 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
1251 return handled;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001252
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001253 if (handled < weight) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001254 napi_complete(napi);
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001255 bgmac_chip_intrs_on(bgmac);
1256 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001257
1258 return handled;
1259}
1260
1261/**************************************************
1262 * net_device_ops
1263 **************************************************/
1264
1265static int bgmac_open(struct net_device *net_dev)
1266{
1267 struct bgmac *bgmac = netdev_priv(net_dev);
1268 int err = 0;
1269
1270 bgmac_chip_reset(bgmac);
1271 /* Specs say about reclaiming rings here, but we do that in DMA init */
1272 bgmac_chip_init(bgmac, true);
1273
1274 err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
1275 KBUILD_MODNAME, net_dev);
1276 if (err < 0) {
1277 bgmac_err(bgmac, "IRQ request error: %d!\n", err);
1278 goto err_out;
1279 }
1280 napi_enable(&bgmac->napi);
1281
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001282 phy_start(bgmac->phy_dev);
1283
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001284 netif_carrier_on(net_dev);
1285
1286err_out:
1287 return err;
1288}
1289
1290static int bgmac_stop(struct net_device *net_dev)
1291{
1292 struct bgmac *bgmac = netdev_priv(net_dev);
1293
1294 netif_carrier_off(net_dev);
1295
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001296 phy_stop(bgmac->phy_dev);
1297
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001298 napi_disable(&bgmac->napi);
1299 bgmac_chip_intrs_off(bgmac);
1300 free_irq(bgmac->core->irq, net_dev);
1301
1302 bgmac_chip_reset(bgmac);
1303
1304 return 0;
1305}
1306
1307static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1308 struct net_device *net_dev)
1309{
1310 struct bgmac *bgmac = netdev_priv(net_dev);
1311 struct bgmac_dma_ring *ring;
1312
1313 /* No QOS support yet */
1314 ring = &bgmac->tx_ring[0];
1315 return bgmac_dma_tx_add(bgmac, ring, skb);
1316}
1317
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001318static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1319{
1320 struct bgmac *bgmac = netdev_priv(net_dev);
1321 int ret;
1322
1323 ret = eth_prepare_mac_addr_change(net_dev, addr);
1324 if (ret < 0)
1325 return ret;
1326 bgmac_write_mac_address(bgmac, (u8 *)addr);
1327 eth_commit_mac_addr_change(net_dev, addr);
1328 return 0;
1329}
1330
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001331static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1332{
1333 struct bgmac *bgmac = netdev_priv(net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001334
Hauke Mehrtens69c58852013-12-20 15:34:45 +01001335 if (!netif_running(net_dev))
1336 return -EINVAL;
1337
1338 return phy_mii_ioctl(bgmac->phy_dev, ifr, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001339}
1340
1341static const struct net_device_ops bgmac_netdev_ops = {
1342 .ndo_open = bgmac_open,
1343 .ndo_stop = bgmac_stop,
1344 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001345 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001346 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001347 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001348 .ndo_do_ioctl = bgmac_ioctl,
1349};
1350
1351/**************************************************
1352 * ethtool_ops
1353 **************************************************/
1354
1355static int bgmac_get_settings(struct net_device *net_dev,
1356 struct ethtool_cmd *cmd)
1357{
1358 struct bgmac *bgmac = netdev_priv(net_dev);
1359
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001360 return phy_ethtool_gset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001361}
1362
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001363static int bgmac_set_settings(struct net_device *net_dev,
1364 struct ethtool_cmd *cmd)
1365{
1366 struct bgmac *bgmac = netdev_priv(net_dev);
1367
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001368 return phy_ethtool_sset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001369}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001370
1371static void bgmac_get_drvinfo(struct net_device *net_dev,
1372 struct ethtool_drvinfo *info)
1373{
1374 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1375 strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
1376}
1377
1378static const struct ethtool_ops bgmac_ethtool_ops = {
1379 .get_settings = bgmac_get_settings,
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001380 .set_settings = bgmac_set_settings,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001381 .get_drvinfo = bgmac_get_drvinfo,
1382};
1383
1384/**************************************************
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001385 * MII
1386 **************************************************/
1387
1388static int bgmac_mii_read(struct mii_bus *bus, int mii_id, int regnum)
1389{
1390 return bgmac_phy_read(bus->priv, mii_id, regnum);
1391}
1392
1393static int bgmac_mii_write(struct mii_bus *bus, int mii_id, int regnum,
1394 u16 value)
1395{
1396 return bgmac_phy_write(bus->priv, mii_id, regnum, value);
1397}
1398
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001399static void bgmac_adjust_link(struct net_device *net_dev)
1400{
1401 struct bgmac *bgmac = netdev_priv(net_dev);
1402 struct phy_device *phy_dev = bgmac->phy_dev;
1403 bool update = false;
1404
1405 if (phy_dev->link) {
1406 if (phy_dev->speed != bgmac->mac_speed) {
1407 bgmac->mac_speed = phy_dev->speed;
1408 update = true;
1409 }
1410
1411 if (phy_dev->duplex != bgmac->mac_duplex) {
1412 bgmac->mac_duplex = phy_dev->duplex;
1413 update = true;
1414 }
1415 }
1416
1417 if (update) {
1418 bgmac_mac_speed(bgmac);
1419 phy_print_status(phy_dev);
1420 }
1421}
1422
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001423static int bgmac_fixed_phy_register(struct bgmac *bgmac)
1424{
1425 struct fixed_phy_status fphy_status = {
1426 .link = 1,
1427 .speed = SPEED_1000,
1428 .duplex = DUPLEX_FULL,
1429 };
1430 struct phy_device *phy_dev;
1431 int err;
1432
1433 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
1434 if (!phy_dev || IS_ERR(phy_dev)) {
1435 bgmac_err(bgmac, "Failed to register fixed PHY device\n");
1436 return -ENODEV;
1437 }
1438
1439 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1440 PHY_INTERFACE_MODE_MII);
1441 if (err) {
1442 bgmac_err(bgmac, "Connecting PHY failed\n");
1443 return err;
1444 }
1445
1446 bgmac->phy_dev = phy_dev;
1447
1448 return err;
1449}
1450
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001451static int bgmac_mii_register(struct bgmac *bgmac)
1452{
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001453 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001454 struct mii_bus *mii_bus;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001455 struct phy_device *phy_dev;
1456 char bus_id[MII_BUS_ID_SIZE + 3];
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001457 int i, err = 0;
1458
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001459 if (ci->id == BCMA_CHIP_ID_BCM4707 ||
1460 ci->id == BCMA_CHIP_ID_BCM53018)
1461 return bgmac_fixed_phy_register(bgmac);
1462
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001463 mii_bus = mdiobus_alloc();
1464 if (!mii_bus)
1465 return -ENOMEM;
1466
1467 mii_bus->name = "bgmac mii bus";
1468 sprintf(mii_bus->id, "%s-%d-%d", "bgmac", bgmac->core->bus->num,
1469 bgmac->core->core_unit);
1470 mii_bus->priv = bgmac;
1471 mii_bus->read = bgmac_mii_read;
1472 mii_bus->write = bgmac_mii_write;
1473 mii_bus->parent = &bgmac->core->dev;
1474 mii_bus->phy_mask = ~(1 << bgmac->phyaddr);
1475
1476 mii_bus->irq = kmalloc_array(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
1477 if (!mii_bus->irq) {
1478 err = -ENOMEM;
1479 goto err_free_bus;
1480 }
1481 for (i = 0; i < PHY_MAX_ADDR; i++)
1482 mii_bus->irq[i] = PHY_POLL;
1483
1484 err = mdiobus_register(mii_bus);
1485 if (err) {
1486 bgmac_err(bgmac, "Registration of mii bus failed\n");
1487 goto err_free_irq;
1488 }
1489
1490 bgmac->mii_bus = mii_bus;
1491
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001492 /* Connect to the PHY */
1493 snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
1494 bgmac->phyaddr);
1495 phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
1496 PHY_INTERFACE_MODE_MII);
1497 if (IS_ERR(phy_dev)) {
1498 bgmac_err(bgmac, "PHY connecton failed\n");
1499 err = PTR_ERR(phy_dev);
1500 goto err_unregister_bus;
1501 }
1502 bgmac->phy_dev = phy_dev;
1503
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001504 return err;
1505
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001506err_unregister_bus:
1507 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001508err_free_irq:
1509 kfree(mii_bus->irq);
1510err_free_bus:
1511 mdiobus_free(mii_bus);
1512 return err;
1513}
1514
1515static void bgmac_mii_unregister(struct bgmac *bgmac)
1516{
1517 struct mii_bus *mii_bus = bgmac->mii_bus;
1518
1519 mdiobus_unregister(mii_bus);
1520 kfree(mii_bus->irq);
1521 mdiobus_free(mii_bus);
1522}
1523
1524/**************************************************
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001525 * BCMA bus ops
1526 **************************************************/
1527
1528/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
1529static int bgmac_probe(struct bcma_device *core)
1530{
Rafał Miłecki21697332015-02-11 18:06:34 +01001531 struct bcma_chipinfo *ci = &core->bus->chipinfo;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001532 struct net_device *net_dev;
1533 struct bgmac *bgmac;
1534 struct ssb_sprom *sprom = &core->bus->sprom;
1535 u8 *mac = core->core_unit ? sprom->et1mac : sprom->et0mac;
1536 int err;
1537
1538 /* We don't support 2nd, 3rd, ... units, SPROM has to be adjusted */
1539 if (core->core_unit > 1) {
1540 pr_err("Unsupported core_unit %d\n", core->core_unit);
1541 return -ENOTSUPP;
1542 }
1543
Rafał Miłeckid166f212013-02-07 00:27:17 +00001544 if (!is_valid_ether_addr(mac)) {
1545 dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
1546 eth_random_addr(mac);
1547 dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
1548 }
1549
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001550 /* Allocation and references */
1551 net_dev = alloc_etherdev(sizeof(*bgmac));
1552 if (!net_dev)
1553 return -ENOMEM;
1554 net_dev->netdev_ops = &bgmac_netdev_ops;
1555 net_dev->irq = core->irq;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001556 net_dev->ethtool_ops = &bgmac_ethtool_ops;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001557 bgmac = netdev_priv(net_dev);
1558 bgmac->net_dev = net_dev;
1559 bgmac->core = core;
1560 bcma_set_drvdata(core, bgmac);
1561
1562 /* Defaults */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001563 memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
1564
1565 /* On BCM4706 we need common core to access PHY */
1566 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
1567 !core->bus->drv_gmac_cmn.core) {
1568 bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
1569 err = -ENODEV;
1570 goto err_netdev_free;
1571 }
1572 bgmac->cmn = core->bus->drv_gmac_cmn.core;
1573
1574 bgmac->phyaddr = core->core_unit ? sprom->et1phyaddr :
1575 sprom->et0phyaddr;
1576 bgmac->phyaddr &= BGMAC_PHY_MASK;
1577 if (bgmac->phyaddr == BGMAC_PHY_MASK) {
1578 bgmac_err(bgmac, "No PHY found\n");
1579 err = -ENODEV;
1580 goto err_netdev_free;
1581 }
1582 bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
1583 bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
1584
1585 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
1586 bgmac_err(bgmac, "PCI setup not implemented\n");
1587 err = -ENOTSUPP;
1588 goto err_netdev_free;
1589 }
1590
1591 bgmac_chip_reset(bgmac);
1592
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001593 /* For Northstar, we have to take all GMAC core out of reset */
Rafał Miłecki21697332015-02-11 18:06:34 +01001594 if (ci->id == BCMA_CHIP_ID_BCM4707 ||
1595 ci->id == BCMA_CHIP_ID_BCM53018) {
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001596 struct bcma_device *ns_core;
1597 int ns_gmac;
1598
1599 /* Northstar has 4 GMAC cores */
1600 for (ns_gmac = 0; ns_gmac < 4; ns_gmac++) {
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001601 /* As Northstar requirement, we have to reset all GMACs
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001602 * before accessing one. bgmac_chip_reset() call
1603 * bcma_core_enable() for this core. Then the other
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001604 * three GMACs didn't reset. We do it here.
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001605 */
1606 ns_core = bcma_find_core_unit(core->bus,
1607 BCMA_CORE_MAC_GBIT,
1608 ns_gmac);
1609 if (ns_core && !bcma_core_is_enabled(ns_core))
1610 bcma_core_enable(ns_core, 0);
1611 }
1612 }
1613
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001614 err = bgmac_dma_alloc(bgmac);
1615 if (err) {
1616 bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
1617 goto err_netdev_free;
1618 }
1619
1620 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
Ralf Baechleedb15d82013-02-21 16:16:55 +01001621 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001622 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1623
1624 /* TODO: reset the external phy. Specs are needed */
1625 bgmac_phy_reset(bgmac);
1626
1627 bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
1628 BGMAC_BFL_ENETROBO);
1629 if (bgmac->has_robosw)
1630 bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
1631
1632 if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
1633 bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
1634
Hauke Mehrtens62166422015-01-18 19:49:58 +01001635 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1636
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001637 err = bgmac_mii_register(bgmac);
1638 if (err) {
1639 bgmac_err(bgmac, "Cannot register MDIO\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001640 goto err_dma_free;
1641 }
1642
Felix Fietkau9cde9452015-03-23 12:35:37 +01001643 net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1644 net_dev->hw_features = net_dev->features;
1645 net_dev->vlan_features = net_dev->features;
1646
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001647 err = register_netdev(bgmac->net_dev);
1648 if (err) {
1649 bgmac_err(bgmac, "Cannot register net device\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001650 goto err_mii_unregister;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001651 }
1652
1653 netif_carrier_off(net_dev);
1654
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001655 return 0;
1656
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001657err_mii_unregister:
1658 bgmac_mii_unregister(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001659err_dma_free:
1660 bgmac_dma_free(bgmac);
1661
1662err_netdev_free:
1663 bcma_set_drvdata(core, NULL);
1664 free_netdev(net_dev);
1665
1666 return err;
1667}
1668
1669static void bgmac_remove(struct bcma_device *core)
1670{
1671 struct bgmac *bgmac = bcma_get_drvdata(core);
1672
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001673 unregister_netdev(bgmac->net_dev);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001674 bgmac_mii_unregister(bgmac);
Hauke Mehrtens62166422015-01-18 19:49:58 +01001675 netif_napi_del(&bgmac->napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001676 bgmac_dma_free(bgmac);
1677 bcma_set_drvdata(core, NULL);
1678 free_netdev(bgmac->net_dev);
1679}
1680
1681static struct bcma_driver bgmac_bcma_driver = {
1682 .name = KBUILD_MODNAME,
1683 .id_table = bgmac_bcma_tbl,
1684 .probe = bgmac_probe,
1685 .remove = bgmac_remove,
1686};
1687
1688static int __init bgmac_init(void)
1689{
1690 int err;
1691
1692 err = bcma_driver_register(&bgmac_bcma_driver);
1693 if (err)
1694 return err;
1695 pr_info("Broadcom 47xx GBit MAC driver loaded\n");
1696
1697 return 0;
1698}
1699
1700static void __exit bgmac_exit(void)
1701{
1702 bcma_driver_unregister(&bgmac_bcma_driver);
1703}
1704
1705module_init(bgmac_init)
1706module_exit(bgmac_exit)
1707
1708MODULE_AUTHOR("Rafał Miłecki");
1709MODULE_LICENSE("GPL");