blob: 25bbae5928d43a4d911d0d5852612d8524437cdb [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>
Rafał Miłecki138173d2014-12-01 07:58:18 +010020#include <linux/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
Rafał Miłecki387b75f2016-02-02 07:47:14 +010029static inline bool bgmac_is_bcm4707_family(struct bgmac *bgmac)
30{
31 switch (bgmac->core->bus->chipinfo.id) {
32 case BCMA_CHIP_ID_BCM4707:
Rafał Miłecki9e4e6202016-02-22 22:51:13 +010033 case BCMA_CHIP_ID_BCM47094:
Rafał Miłecki387b75f2016-02-02 07:47:14 +010034 case BCMA_CHIP_ID_BCM53018:
35 return true;
36 default:
37 return false;
38 }
39}
40
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000041static bool bgmac_wait_value(struct bcma_device *core, u16 reg, u32 mask,
42 u32 value, int timeout)
43{
44 u32 val;
45 int i;
46
47 for (i = 0; i < timeout / 10; i++) {
48 val = bcma_read32(core, reg);
49 if ((val & mask) == value)
50 return true;
51 udelay(10);
52 }
53 pr_err("Timeout waiting for reg 0x%X\n", reg);
54 return false;
55}
56
57/**************************************************
58 * DMA
59 **************************************************/
60
61static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
62{
63 u32 val;
64 int i;
65
66 if (!ring->mmio_base)
67 return;
68
69 /* Suspend DMA TX ring first.
70 * bgmac_wait_value doesn't support waiting for any of few values, so
71 * implement whole loop here.
72 */
73 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
74 BGMAC_DMA_TX_SUSPEND);
75 for (i = 0; i < 10000 / 10; i++) {
76 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
77 val &= BGMAC_DMA_TX_STAT;
78 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
79 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
80 val == BGMAC_DMA_TX_STAT_STOPPED) {
81 i = 0;
82 break;
83 }
84 udelay(10);
85 }
86 if (i)
87 bgmac_err(bgmac, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
88 ring->mmio_base, val);
89
90 /* Remove SUSPEND bit */
91 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
92 if (!bgmac_wait_value(bgmac->core,
93 ring->mmio_base + BGMAC_DMA_TX_STATUS,
94 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
95 10000)) {
96 bgmac_warn(bgmac, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
97 ring->mmio_base);
98 udelay(300);
99 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
100 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
101 bgmac_err(bgmac, "Reset of DMA TX ring 0x%X failed\n",
102 ring->mmio_base);
103 }
104}
105
106static void bgmac_dma_tx_enable(struct bgmac *bgmac,
107 struct bgmac_dma_ring *ring)
108{
109 u32 ctl;
110
111 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100112 if (bgmac->core->id.rev >= 4) {
113 ctl &= ~BGMAC_DMA_TX_BL_MASK;
114 ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
115
116 ctl &= ~BGMAC_DMA_TX_MR_MASK;
117 ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
118
119 ctl &= ~BGMAC_DMA_TX_PC_MASK;
120 ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
121
122 ctl &= ~BGMAC_DMA_TX_PT_MASK;
123 ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
124 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000125 ctl |= BGMAC_DMA_TX_ENABLE;
126 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
127 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
128}
129
Felix Fietkau9cde9452015-03-23 12:35:37 +0100130static void
131bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
132 int i, int len, u32 ctl0)
133{
134 struct bgmac_slot_info *slot;
135 struct bgmac_dma_desc *dma_desc;
136 u32 ctl1;
137
Felix Fietkau29ba8772015-04-14 12:08:02 +0200138 if (i == BGMAC_TX_RING_SLOTS - 1)
Felix Fietkau9cde9452015-03-23 12:35:37 +0100139 ctl0 |= BGMAC_DESC_CTL0_EOT;
140
141 ctl1 = len & BGMAC_DESC_CTL1_LEN;
142
143 slot = &ring->slots[i];
144 dma_desc = &ring->cpu_base[i];
145 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
146 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
147 dma_desc->ctl0 = cpu_to_le32(ctl0);
148 dma_desc->ctl1 = cpu_to_le32(ctl1);
149}
150
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000151static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
152 struct bgmac_dma_ring *ring,
153 struct sk_buff *skb)
154{
155 struct device *dma_dev = bgmac->core->dma_dev;
156 struct net_device *net_dev = bgmac->net_dev;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200157 int index = ring->end % BGMAC_TX_RING_SLOTS;
158 struct bgmac_slot_info *slot = &ring->slots[index];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100159 int nr_frags;
160 u32 flags;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100161 int i;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000162
163 if (skb->len > BGMAC_DESC_CTL1_LEN) {
164 bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100165 goto err_drop;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000166 }
167
Felix Fietkau9cde9452015-03-23 12:35:37 +0100168 if (skb->ip_summed == CHECKSUM_PARTIAL)
169 skb_checksum_help(skb);
170
171 nr_frags = skb_shinfo(skb)->nr_frags;
172
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200173 /* ring->end - ring->start will return the number of valid slots,
174 * even when ring->end overflows
175 */
176 if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000177 bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
178 netif_stop_queue(net_dev);
179 return NETDEV_TX_BUSY;
180 }
181
Felix Fietkau9cde9452015-03-23 12:35:37 +0100182 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000183 DMA_TO_DEVICE);
Felix Fietkau9cde9452015-03-23 12:35:37 +0100184 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
185 goto err_dma_head;
186
187 flags = BGMAC_DESC_CTL0_SOF;
188 if (!nr_frags)
189 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
190
191 bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
192 flags = 0;
193
194 for (i = 0; i < nr_frags; i++) {
195 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
196 int len = skb_frag_size(frag);
197
198 index = (index + 1) % BGMAC_TX_RING_SLOTS;
199 slot = &ring->slots[index];
200 slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
201 len, DMA_TO_DEVICE);
202 if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
203 goto err_dma;
204
205 if (i == nr_frags - 1)
206 flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
207
208 bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000209 }
210
Felix Fietkau9cde9452015-03-23 12:35:37 +0100211 slot->skb = skb;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200212 ring->end += nr_frags + 1;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200213 netdev_sent_queue(net_dev, skb->len);
214
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000215 wmb();
216
217 /* Increase ring->end to point empty slot. We tell hardware the first
218 * slot it should *not* read.
219 */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000220 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
Rafał Miłecki99003032013-09-15 23:13:18 +0200221 ring->index_base +
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200222 (ring->end % BGMAC_TX_RING_SLOTS) *
223 sizeof(struct bgmac_dma_desc));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000224
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200225 if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000226 netif_stop_queue(net_dev);
227
228 return NETDEV_TX_OK;
229
Felix Fietkau9cde9452015-03-23 12:35:37 +0100230err_dma:
231 dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
232 DMA_TO_DEVICE);
233
Florian Fainellie86663c2016-07-15 15:42:52 -0700234 while (i-- > 0) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100235 int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
236 struct bgmac_slot_info *slot = &ring->slots[index];
237 u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
238 int len = ctl1 & BGMAC_DESC_CTL1_LEN;
239
240 dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
241 }
242
243err_dma_head:
244 bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n",
245 ring->mmio_base);
246
247err_drop:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000248 dev_kfree_skb(skb);
249 return NETDEV_TX_OK;
250}
251
252/* Free transmitted packets */
253static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
254{
255 struct device *dma_dev = bgmac->core->dma_dev;
256 int empty_slot;
257 bool freed = false;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200258 unsigned bytes_compl = 0, pkts_compl = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000259
260 /* The last slot that hardware didn't consume yet */
261 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
262 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200263 empty_slot -= ring->index_base;
264 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000265 empty_slot /= sizeof(struct bgmac_dma_desc);
266
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200267 while (ring->start != ring->end) {
268 int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
269 struct bgmac_slot_info *slot = &ring->slots[slot_idx];
Florian Fainellid2b13232016-06-23 14:23:12 -0700270 u32 ctl0, ctl1;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200271 int len;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100272
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200273 if (slot_idx == empty_slot)
274 break;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100275
Florian Fainellid2b13232016-06-23 14:23:12 -0700276 ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0);
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200277 ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
278 len = ctl1 & BGMAC_DESC_CTL1_LEN;
Florian Fainellid2b13232016-06-23 14:23:12 -0700279 if (ctl0 & BGMAC_DESC_CTL0_SOF)
Felix Fietkau9cde9452015-03-23 12:35:37 +0100280 /* Unmap no longer used buffer */
281 dma_unmap_single(dma_dev, slot->dma_addr, len,
282 DMA_TO_DEVICE);
283 else
284 dma_unmap_page(dma_dev, slot->dma_addr, len,
285 DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000286
287 if (slot->skb) {
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200288 bytes_compl += slot->skb->len;
289 pkts_compl++;
290
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000291 /* Free memory! :) */
292 dev_kfree_skb(slot->skb);
293 slot->skb = NULL;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000294 }
295
Felix Fietkau9cde9452015-03-23 12:35:37 +0100296 slot->dma_addr = 0;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200297 ring->start++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000298 freed = true;
299 }
300
Felix Fietkau9cde9452015-03-23 12:35:37 +0100301 if (!pkts_compl)
302 return;
303
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200304 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
305
Felix Fietkau9cde9452015-03-23 12:35:37 +0100306 if (netif_queue_stopped(bgmac->net_dev))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000307 netif_wake_queue(bgmac->net_dev);
308}
309
310static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
311{
312 if (!ring->mmio_base)
313 return;
314
315 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
316 if (!bgmac_wait_value(bgmac->core,
317 ring->mmio_base + BGMAC_DMA_RX_STATUS,
318 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
319 10000))
320 bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
321 ring->mmio_base);
322}
323
324static void bgmac_dma_rx_enable(struct bgmac *bgmac,
325 struct bgmac_dma_ring *ring)
326{
327 u32 ctl;
328
329 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100330 if (bgmac->core->id.rev >= 4) {
331 ctl &= ~BGMAC_DMA_RX_BL_MASK;
332 ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
333
334 ctl &= ~BGMAC_DMA_RX_PC_MASK;
335 ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
336
337 ctl &= ~BGMAC_DMA_RX_PT_MASK;
338 ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
339 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000340 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
341 ctl |= BGMAC_DMA_RX_ENABLE;
342 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
343 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
344 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
345 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
346}
347
348static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
349 struct bgmac_slot_info *slot)
350{
351 struct device *dma_dev = bgmac->core->dma_dev;
Nathan Hintzb757a622013-10-29 19:32:01 -0700352 dma_addr_t dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000353 struct bgmac_rx_header *rx;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100354 void *buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000355
356 /* Alloc skb */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100357 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
358 if (!buf)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000359 return -ENOMEM;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000360
361 /* Poison - if everything goes fine, hardware will overwrite it */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200362 rx = buf + BGMAC_RX_BUF_OFFSET;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000363 rx->len = cpu_to_le16(0xdead);
364 rx->flags = cpu_to_le16(0xbeef);
365
366 /* Map skb for the DMA */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200367 dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
368 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
Nathan Hintzb757a622013-10-29 19:32:01 -0700369 if (dma_mapping_error(dma_dev, dma_addr)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000370 bgmac_err(bgmac, "DMA mapping error\n");
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100371 put_page(virt_to_head_page(buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000372 return -ENOMEM;
373 }
Nathan Hintzb757a622013-10-29 19:32:01 -0700374
375 /* Update the slot */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100376 slot->buf = buf;
Nathan Hintzb757a622013-10-29 19:32:01 -0700377 slot->dma_addr = dma_addr;
378
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000379 return 0;
380}
381
Felix Fietkau4668ae12015-04-14 12:08:01 +0200382static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
383 struct bgmac_dma_ring *ring)
384{
385 dma_wmb();
386
387 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
388 ring->index_base +
389 ring->end * sizeof(struct bgmac_dma_desc));
390}
391
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100392static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
393 struct bgmac_dma_ring *ring, int desc_idx)
394{
395 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
396 u32 ctl0 = 0, ctl1 = 0;
397
Felix Fietkau29ba8772015-04-14 12:08:02 +0200398 if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100399 ctl0 |= BGMAC_DESC_CTL0_EOT;
400 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
401 /* Is there any BGMAC device that requires extension? */
402 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
403 * B43_DMA64_DCTL1_ADDREXT_MASK;
404 */
405
406 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
407 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
408 dma_desc->ctl0 = cpu_to_le32(ctl0);
409 dma_desc->ctl1 = cpu_to_le32(ctl1);
Felix Fietkau4668ae12015-04-14 12:08:01 +0200410
411 ring->end = desc_idx;
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100412}
413
Felix Fietkau56faacd2015-04-14 12:07:57 +0200414static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
415 struct bgmac_slot_info *slot)
416{
417 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
418
419 dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
420 DMA_FROM_DEVICE);
421 rx->len = cpu_to_le16(0xdead);
422 rx->flags = cpu_to_le16(0xbeef);
423 dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
424 DMA_FROM_DEVICE);
425}
426
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000427static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
428 int weight)
429{
430 u32 end_slot;
431 int handled = 0;
432
433 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
434 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200435 end_slot -= ring->index_base;
436 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000437 end_slot /= sizeof(struct bgmac_dma_desc);
438
Felix Fietkau4668ae12015-04-14 12:08:01 +0200439 while (ring->start != end_slot) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000440 struct device *dma_dev = bgmac->core->dma_dev;
441 struct bgmac_slot_info *slot = &ring->slots[ring->start];
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200442 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100443 struct sk_buff *skb;
444 void *buf = slot->buf;
Felix Fietkau56faacd2015-04-14 12:07:57 +0200445 dma_addr_t dma_addr = slot->dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000446 u16 len, flags;
447
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100448 do {
Felix Fietkau56faacd2015-04-14 12:07:57 +0200449 /* Prepare new skb as replacement */
450 if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
451 bgmac_dma_rx_poison_buf(dma_dev, slot);
452 break;
453 }
454
455 /* Unmap buffer to make it accessible to the CPU */
456 dma_unmap_single(dma_dev, dma_addr,
457 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
458
459 /* Get info from the header */
460 len = le16_to_cpu(rx->len);
461 flags = le16_to_cpu(rx->flags);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100462
463 /* Check for poison and drop or pass the packet */
464 if (len == 0xdead && flags == 0xbeef) {
465 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
466 ring->start);
Felix Fietkau56faacd2015-04-14 12:07:57 +0200467 put_page(virt_to_head_page(buf));
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100468 break;
469 }
470
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200471 if (len > BGMAC_RX_ALLOC_SIZE) {
472 bgmac_err(bgmac, "Found oversized packet at slot %d, DMA issue!\n",
473 ring->start);
474 put_page(virt_to_head_page(buf));
475 break;
476 }
477
Hauke Mehrtens02e71122013-02-28 07:16:54 +0000478 /* Omit CRC. */
479 len -= ETH_FCS_LEN;
480
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100481 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
David S. Miller750afbf2016-01-15 16:07:13 -0500482 if (unlikely(!skb)) {
wangweidongf1640c32016-01-13 11:06:41 +0800483 bgmac_err(bgmac, "build_skb failed\n");
484 put_page(virt_to_head_page(buf));
485 break;
486 }
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200487 skb_put(skb, BGMAC_RX_FRAME_OFFSET +
488 BGMAC_RX_BUF_OFFSET + len);
489 skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
490 BGMAC_RX_BUF_OFFSET);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100491
492 skb_checksum_none_assert(skb);
493 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100494 napi_gro_receive(&bgmac->napi, skb);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100495 handled++;
496 } while (0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000497
Felix Fietkau56faacd2015-04-14 12:07:57 +0200498 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
499
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000500 if (++ring->start >= BGMAC_RX_RING_SLOTS)
501 ring->start = 0;
502
503 if (handled >= weight) /* Should never be greater */
504 break;
505 }
506
Felix Fietkau4668ae12015-04-14 12:08:01 +0200507 bgmac_dma_rx_update_index(bgmac, ring);
508
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000509 return handled;
510}
511
512/* Does ring support unaligned addressing? */
513static bool bgmac_dma_unaligned(struct bgmac *bgmac,
514 struct bgmac_dma_ring *ring,
515 enum bgmac_dma_ring_type ring_type)
516{
517 switch (ring_type) {
518 case BGMAC_DMA_RING_TX:
519 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
520 0xff0);
521 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
522 return true;
523 break;
524 case BGMAC_DMA_RING_RX:
525 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
526 0xff0);
527 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
528 return true;
529 break;
530 }
531 return false;
532}
533
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100534static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
535 struct bgmac_dma_ring *ring)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000536{
537 struct device *dma_dev = bgmac->core->dma_dev;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100538 struct bgmac_dma_desc *dma_desc = ring->cpu_base;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000539 struct bgmac_slot_info *slot;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000540 int i;
541
Felix Fietkau29ba8772015-04-14 12:08:02 +0200542 for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100543 int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN;
544
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000545 slot = &ring->slots[i];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100546 dev_kfree_skb(slot->skb);
547
548 if (!slot->dma_addr)
549 continue;
550
551 if (slot->skb)
552 dma_unmap_single(dma_dev, slot->dma_addr,
553 len, DMA_TO_DEVICE);
554 else
555 dma_unmap_page(dma_dev, slot->dma_addr,
556 len, DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000557 }
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100558}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000559
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100560static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
561 struct bgmac_dma_ring *ring)
562{
563 struct device *dma_dev = bgmac->core->dma_dev;
564 struct bgmac_slot_info *slot;
565 int i;
566
Felix Fietkau29ba8772015-04-14 12:08:02 +0200567 for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100568 slot = &ring->slots[i];
Felix Fietkau56faacd2015-04-14 12:07:57 +0200569 if (!slot->dma_addr)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100570 continue;
571
Felix Fietkau56faacd2015-04-14 12:07:57 +0200572 dma_unmap_single(dma_dev, slot->dma_addr,
573 BGMAC_RX_BUF_SIZE,
574 DMA_FROM_DEVICE);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100575 put_page(virt_to_head_page(slot->buf));
Felix Fietkau56faacd2015-04-14 12:07:57 +0200576 slot->dma_addr = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000577 }
578}
579
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100580static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
Felix Fietkau29ba8772015-04-14 12:08:02 +0200581 struct bgmac_dma_ring *ring,
582 int num_slots)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100583{
584 struct device *dma_dev = bgmac->core->dma_dev;
585 int size;
586
587 if (!ring->cpu_base)
588 return;
589
590 /* Free ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200591 size = num_slots * sizeof(struct bgmac_dma_desc);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100592 dma_free_coherent(dma_dev, size, ring->cpu_base,
593 ring->dma_base);
594}
595
Felix Fietkau74b6f292015-04-14 12:08:00 +0200596static void bgmac_dma_cleanup(struct bgmac *bgmac)
597{
598 int i;
599
600 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
601 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
602
603 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
604 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
605}
606
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000607static void bgmac_dma_free(struct bgmac *bgmac)
608{
609 int i;
610
Felix Fietkau74b6f292015-04-14 12:08:00 +0200611 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200612 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
613 BGMAC_TX_RING_SLOTS);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200614
615 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200616 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
617 BGMAC_RX_RING_SLOTS);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000618}
619
620static int bgmac_dma_alloc(struct bgmac *bgmac)
621{
622 struct device *dma_dev = bgmac->core->dma_dev;
623 struct bgmac_dma_ring *ring;
624 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
625 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
626 int size; /* ring size: different for Tx and Rx */
627 int err;
628 int i;
629
630 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
631 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
632
633 if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
634 bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
635 return -ENOTSUPP;
636 }
637
638 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
639 ring = &bgmac->tx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000640 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000641
642 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200643 size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000644 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
645 &ring->dma_base,
646 GFP_KERNEL);
647 if (!ring->cpu_base) {
648 bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
649 ring->mmio_base);
650 goto err_dma_free;
651 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000652
Rafał Miłecki99003032013-09-15 23:13:18 +0200653 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
654 BGMAC_DMA_RING_TX);
655 if (ring->unaligned)
656 ring->index_base = lower_32_bits(ring->dma_base);
657 else
658 ring->index_base = 0;
659
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000660 /* No need to alloc TX slots yet */
661 }
662
663 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
664 ring = &bgmac->rx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000665 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000666
667 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200668 size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000669 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
670 &ring->dma_base,
671 GFP_KERNEL);
672 if (!ring->cpu_base) {
673 bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
674 ring->mmio_base);
675 err = -ENOMEM;
676 goto err_dma_free;
677 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000678
Rafał Miłecki99003032013-09-15 23:13:18 +0200679 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
680 BGMAC_DMA_RING_RX);
681 if (ring->unaligned)
682 ring->index_base = lower_32_bits(ring->dma_base);
683 else
684 ring->index_base = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000685 }
686
687 return 0;
688
689err_dma_free:
690 bgmac_dma_free(bgmac);
691 return -ENOMEM;
692}
693
Felix Fietkau74b6f292015-04-14 12:08:00 +0200694static int bgmac_dma_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000695{
696 struct bgmac_dma_ring *ring;
Felix Fietkau74b6f292015-04-14 12:08:00 +0200697 int i, err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000698
699 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
700 ring = &bgmac->tx_ring[i];
701
Rafał Miłecki99003032013-09-15 23:13:18 +0200702 if (!ring->unaligned)
703 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000704 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
705 lower_32_bits(ring->dma_base));
706 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
707 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200708 if (ring->unaligned)
709 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000710
711 ring->start = 0;
712 ring->end = 0; /* Points the slot that should *not* be read */
713 }
714
715 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000716 int j;
717
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000718 ring = &bgmac->rx_ring[i];
719
Rafał Miłecki99003032013-09-15 23:13:18 +0200720 if (!ring->unaligned)
721 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000722 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
723 lower_32_bits(ring->dma_base));
724 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
725 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200726 if (ring->unaligned)
727 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000728
Felix Fietkau4668ae12015-04-14 12:08:01 +0200729 ring->start = 0;
730 ring->end = 0;
Felix Fietkau29ba8772015-04-14 12:08:02 +0200731 for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
Felix Fietkau74b6f292015-04-14 12:08:00 +0200732 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
733 if (err)
734 goto error;
735
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100736 bgmac_dma_rx_setup_desc(bgmac, ring, j);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200737 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000738
Felix Fietkau4668ae12015-04-14 12:08:01 +0200739 bgmac_dma_rx_update_index(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000740 }
Felix Fietkau74b6f292015-04-14 12:08:00 +0200741
742 return 0;
743
744error:
745 bgmac_dma_cleanup(bgmac);
746 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000747}
748
749/**************************************************
750 * PHY ops
751 **************************************************/
752
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000753static u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000754{
755 struct bcma_device *core;
756 u16 phy_access_addr;
757 u16 phy_ctl_addr;
758 u32 tmp;
759
760 BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
761 BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
762 BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
763 BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
764 BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
765 BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
766 BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
767 BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
768 BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
769 BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
770 BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
771
772 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
773 core = bgmac->core->bus->drv_gmac_cmn.core;
774 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
775 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
776 } else {
777 core = bgmac->core;
778 phy_access_addr = BGMAC_PHY_ACCESS;
779 phy_ctl_addr = BGMAC_PHY_CNTL;
780 }
781
782 tmp = bcma_read32(core, phy_ctl_addr);
783 tmp &= ~BGMAC_PC_EPA_MASK;
784 tmp |= phyaddr;
785 bcma_write32(core, phy_ctl_addr, tmp);
786
787 tmp = BGMAC_PA_START;
788 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
789 tmp |= reg << BGMAC_PA_REG_SHIFT;
790 bcma_write32(core, phy_access_addr, tmp);
791
792 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
793 bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
794 phyaddr, reg);
795 return 0xffff;
796 }
797
798 return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
799}
800
801/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000802static int bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000803{
804 struct bcma_device *core;
805 u16 phy_access_addr;
806 u16 phy_ctl_addr;
807 u32 tmp;
808
809 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
810 core = bgmac->core->bus->drv_gmac_cmn.core;
811 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
812 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
813 } else {
814 core = bgmac->core;
815 phy_access_addr = BGMAC_PHY_ACCESS;
816 phy_ctl_addr = BGMAC_PHY_CNTL;
817 }
818
819 tmp = bcma_read32(core, phy_ctl_addr);
820 tmp &= ~BGMAC_PC_EPA_MASK;
821 tmp |= phyaddr;
822 bcma_write32(core, phy_ctl_addr, tmp);
823
824 bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
825 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
826 bgmac_warn(bgmac, "Error setting MDIO int\n");
827
828 tmp = BGMAC_PA_START;
829 tmp |= BGMAC_PA_WRITE;
830 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
831 tmp |= reg << BGMAC_PA_REG_SHIFT;
832 tmp |= value;
833 bcma_write32(core, phy_access_addr, tmp);
834
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000835 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000836 bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
837 phyaddr, reg);
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000838 return -ETIMEDOUT;
839 }
840
841 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000842}
843
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000844/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
845static void bgmac_phy_init(struct bgmac *bgmac)
846{
847 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
848 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
849 u8 i;
850
851 if (ci->id == BCMA_CHIP_ID_BCM5356) {
852 for (i = 0; i < 5; i++) {
853 bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
854 bgmac_phy_write(bgmac, i, 0x15, 0x0100);
855 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
856 bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
857 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
858 }
859 }
860 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
861 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
862 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
863 bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
864 bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
865 for (i = 0; i < 5; i++) {
866 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
867 bgmac_phy_write(bgmac, i, 0x16, 0x5284);
868 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
869 bgmac_phy_write(bgmac, i, 0x17, 0x0010);
870 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
871 bgmac_phy_write(bgmac, i, 0x16, 0x5296);
872 bgmac_phy_write(bgmac, i, 0x17, 0x1073);
873 bgmac_phy_write(bgmac, i, 0x17, 0x9073);
874 bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
875 bgmac_phy_write(bgmac, i, 0x17, 0x9273);
876 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
877 }
878 }
879}
880
881/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
882static void bgmac_phy_reset(struct bgmac *bgmac)
883{
884 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
885 return;
886
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100887 bgmac_phy_write(bgmac, bgmac->phyaddr, MII_BMCR, BMCR_RESET);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000888 udelay(100);
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100889 if (bgmac_phy_read(bgmac, bgmac->phyaddr, MII_BMCR) & BMCR_RESET)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000890 bgmac_err(bgmac, "PHY reset failed\n");
891 bgmac_phy_init(bgmac);
892}
893
894/**************************************************
895 * Chip ops
896 **************************************************/
897
898/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
899 * nothing to change? Try if after stabilizng driver.
900 */
901static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
902 bool force)
903{
904 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
905 u32 new_val = (cmdcfg & mask) | set;
906
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100907 bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000908 udelay(2);
909
910 if (new_val != cmdcfg || force)
911 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
912
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100913 bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000914 udelay(2);
915}
916
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000917static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
918{
919 u32 tmp;
920
921 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
922 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
923 tmp = (addr[4] << 8) | addr[5];
924 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
925}
926
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000927static void bgmac_set_rx_mode(struct net_device *net_dev)
928{
929 struct bgmac *bgmac = netdev_priv(net_dev);
930
931 if (net_dev->flags & IFF_PROMISC)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000932 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000933 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000934 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000935}
936
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000937#if 0 /* We don't use that regs yet */
938static void bgmac_chip_stats_update(struct bgmac *bgmac)
939{
940 int i;
941
942 if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
943 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
944 bgmac->mib_tx_regs[i] =
945 bgmac_read(bgmac,
946 BGMAC_TX_GOOD_OCTETS + (i * 4));
947 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
948 bgmac->mib_rx_regs[i] =
949 bgmac_read(bgmac,
950 BGMAC_RX_GOOD_OCTETS + (i * 4));
951 }
952
953 /* TODO: what else? how to handle BCM4706? Specs are needed */
954}
955#endif
956
957static void bgmac_clear_mib(struct bgmac *bgmac)
958{
959 int i;
960
961 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
962 return;
963
964 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
965 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
966 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
967 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
968 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
969}
970
971/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100972static void bgmac_mac_speed(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000973{
974 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
975 u32 set = 0;
976
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100977 switch (bgmac->mac_speed) {
978 case SPEED_10:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000979 set |= BGMAC_CMDCFG_ES_10;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100980 break;
981 case SPEED_100:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000982 set |= BGMAC_CMDCFG_ES_100;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100983 break;
984 case SPEED_1000:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000985 set |= BGMAC_CMDCFG_ES_1000;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100986 break;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100987 case SPEED_2500:
988 set |= BGMAC_CMDCFG_ES_2500;
989 break;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100990 default:
991 bgmac_err(bgmac, "Unsupported speed: %d\n", bgmac->mac_speed);
992 }
993
994 if (bgmac->mac_duplex == DUPLEX_HALF)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000995 set |= BGMAC_CMDCFG_HD;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100996
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000997 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
998}
999
1000static void bgmac_miiconfig(struct bgmac *bgmac)
1001{
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001002 struct bcma_device *core = bgmac->core;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001003 u8 imode;
1004
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001005 if (bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001006 bcma_awrite32(core, BCMA_IOCTL,
1007 bcma_aread32(core, BCMA_IOCTL) | 0x40 |
1008 BGMAC_BCMA_IOCTL_SW_CLKEN);
1009 bgmac->mac_speed = SPEED_2500;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001010 bgmac->mac_duplex = DUPLEX_FULL;
1011 bgmac_mac_speed(bgmac);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001012 } else {
1013 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
1014 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
1015 if (imode == 0 || imode == 1) {
1016 bgmac->mac_speed = SPEED_100;
1017 bgmac->mac_duplex = DUPLEX_FULL;
1018 bgmac_mac_speed(bgmac);
1019 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001020 }
1021}
1022
1023/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
1024static void bgmac_chip_reset(struct bgmac *bgmac)
1025{
1026 struct bcma_device *core = bgmac->core;
1027 struct bcma_bus *bus = core->bus;
1028 struct bcma_chipinfo *ci = &bus->chipinfo;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001029 u32 flags;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001030 u32 iost;
1031 int i;
1032
1033 if (bcma_core_is_enabled(core)) {
1034 if (!bgmac->stats_grabbed) {
1035 /* bgmac_chip_stats_update(bgmac); */
1036 bgmac->stats_grabbed = true;
1037 }
1038
1039 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
1040 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
1041
1042 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
1043 udelay(1);
1044
1045 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
1046 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
1047
1048 /* TODO: Clear software multicast filter list */
1049 }
1050
1051 iost = bcma_aread32(core, BCMA_IOST);
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001052 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001053 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001054 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001055 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
1056
Rafał Miłecki9e4e6202016-02-22 22:51:13 +01001057 /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
1058 if (ci->id != BCMA_CHIP_ID_BCM4707 &&
1059 ci->id != BCMA_CHIP_ID_BCM47094) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001060 flags = 0;
1061 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
1062 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
1063 if (!bgmac->has_robosw)
1064 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
1065 }
1066 bcma_core_enable(core, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001067 }
1068
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001069 /* Request Misc PLL for corerev > 2 */
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001070 if (core->id.rev > 2 && !bgmac_is_bcm4707_family(bgmac)) {
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001071 bgmac_set(bgmac, BCMA_CLKCTLST,
1072 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
1073 bgmac_wait_value(bgmac->core, BCMA_CLKCTLST,
1074 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
1075 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001076 1000);
1077 }
1078
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001079 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1080 ci->id == BCMA_CHIP_ID_BCM4749 ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001081 ci->id == BCMA_CHIP_ID_BCM53572) {
1082 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
1083 u8 et_swtype = 0;
1084 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
Rafał Miłecki6a391e72013-09-15 00:22:47 +02001085 BGMAC_CHIPCTL_1_IF_TYPE_MII;
Hauke Mehrtens36472682013-09-15 22:49:08 +02001086 char buf[4];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001087
Hauke Mehrtens36472682013-09-15 22:49:08 +02001088 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001089 if (kstrtou8(buf, 0, &et_swtype))
1090 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
1091 buf);
1092 et_swtype &= 0x0f;
1093 et_swtype <<= 4;
1094 sw_type = et_swtype;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001095 } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM5358) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001096 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001097 } else if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
1098 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
1099 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188)) {
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +00001100 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
1101 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001102 }
1103 bcma_chipco_chipctl_maskset(cc, 1,
1104 ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
1105 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
1106 sw_type);
1107 }
1108
1109 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
1110 bcma_awrite32(core, BCMA_IOCTL,
1111 bcma_aread32(core, BCMA_IOCTL) &
1112 ~BGMAC_BCMA_IOCTL_SW_RESET);
1113
1114 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
1115 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
1116 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
1117 * be keps until taking MAC out of the reset.
1118 */
1119 bgmac_cmdcfg_maskset(bgmac,
1120 ~(BGMAC_CMDCFG_TE |
1121 BGMAC_CMDCFG_RE |
1122 BGMAC_CMDCFG_RPI |
1123 BGMAC_CMDCFG_TAI |
1124 BGMAC_CMDCFG_HD |
1125 BGMAC_CMDCFG_ML |
1126 BGMAC_CMDCFG_CFE |
1127 BGMAC_CMDCFG_RL |
1128 BGMAC_CMDCFG_RED |
1129 BGMAC_CMDCFG_PE |
1130 BGMAC_CMDCFG_TPI |
1131 BGMAC_CMDCFG_PAD_EN |
1132 BGMAC_CMDCFG_PF),
1133 BGMAC_CMDCFG_PROM |
1134 BGMAC_CMDCFG_NLC |
1135 BGMAC_CMDCFG_CFE |
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001136 BGMAC_CMDCFG_SR(core->id.rev),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001137 false);
Rafał Miłeckid4699622013-12-11 07:44:14 +01001138 bgmac->mac_speed = SPEED_UNKNOWN;
1139 bgmac->mac_duplex = DUPLEX_UNKNOWN;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001140
1141 bgmac_clear_mib(bgmac);
1142 if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
1143 bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
1144 BCMA_GMAC_CMN_PC_MTE);
1145 else
1146 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1147 bgmac_miiconfig(bgmac);
1148 bgmac_phy_init(bgmac);
1149
Hauke Mehrtens49a467b2013-09-29 13:54:58 +02001150 netdev_reset_queue(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001151}
1152
1153static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1154{
1155 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1156}
1157
1158static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1159{
1160 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
Nathan Hintz41608152013-02-13 19:14:10 +00001161 bgmac_read(bgmac, BGMAC_INT_MASK);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001162}
1163
1164/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1165static void bgmac_enable(struct bgmac *bgmac)
1166{
1167 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
1168 u32 cmdcfg;
1169 u32 mode;
1170 u32 rxq_ctl;
1171 u32 fl_ctl;
1172 u16 bp_clk;
1173 u8 mdp;
1174
1175 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
1176 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001177 BGMAC_CMDCFG_SR(bgmac->core->id.rev), true);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001178 udelay(2);
1179 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1180 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1181
1182 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1183 BGMAC_DS_MM_SHIFT;
1184 if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
1185 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1186 if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
1187 bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
1188 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1189
1190 switch (ci->id) {
1191 case BCMA_CHIP_ID_BCM5357:
1192 case BCMA_CHIP_ID_BCM4749:
1193 case BCMA_CHIP_ID_BCM53572:
1194 case BCMA_CHIP_ID_BCM4716:
1195 case BCMA_CHIP_ID_BCM47162:
1196 fl_ctl = 0x03cb04cb;
1197 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1198 ci->id == BCMA_CHIP_ID_BCM4749 ||
1199 ci->id == BCMA_CHIP_ID_BCM53572)
1200 fl_ctl = 0x2300e1;
1201 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1202 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
1203 break;
1204 }
1205
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001206 if (!bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001207 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1208 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1209 bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) /
1210 1000000;
1211 mdp = (bp_clk * 128 / 1000) - 3;
1212 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1213 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1214 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001215}
1216
1217/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001218static void bgmac_chip_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001219{
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001220 /* 1 interrupt per received frame */
1221 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1222
1223 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1224 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1225
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001226 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001227
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001228 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001229
1230 if (bgmac->loopback)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001231 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001232 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001233 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001234
1235 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1236
Felix Fietkau74b6f292015-04-14 12:08:00 +02001237 bgmac_chip_intrs_on(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001238
1239 bgmac_enable(bgmac);
1240}
1241
1242static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1243{
1244 struct bgmac *bgmac = netdev_priv(dev_id);
1245
1246 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1247 int_status &= bgmac->int_mask;
1248
1249 if (!int_status)
1250 return IRQ_NONE;
1251
Felix Fietkaueb64e292015-04-14 12:07:55 +02001252 int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1253 if (int_status)
1254 bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", int_status);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001255
1256 /* Disable new interrupts until handling existing ones */
1257 bgmac_chip_intrs_off(bgmac);
1258
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001259 napi_schedule(&bgmac->napi);
1260
1261 return IRQ_HANDLED;
1262}
1263
1264static int bgmac_poll(struct napi_struct *napi, int weight)
1265{
1266 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001267 int handled = 0;
1268
Felix Fietkaueb64e292015-04-14 12:07:55 +02001269 /* Ack */
1270 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001271
Felix Fietkaueb64e292015-04-14 12:07:55 +02001272 bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1273 handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001274
Felix Fietkaueb64e292015-04-14 12:07:55 +02001275 /* Poll again if more events arrived in the meantime */
1276 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
Rafał Miłeckie5802672015-04-23 20:56:29 +02001277 return weight;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001278
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001279 if (handled < weight) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001280 napi_complete(napi);
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001281 bgmac_chip_intrs_on(bgmac);
1282 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001283
1284 return handled;
1285}
1286
1287/**************************************************
1288 * net_device_ops
1289 **************************************************/
1290
1291static int bgmac_open(struct net_device *net_dev)
1292{
1293 struct bgmac *bgmac = netdev_priv(net_dev);
1294 int err = 0;
1295
1296 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001297
1298 err = bgmac_dma_init(bgmac);
1299 if (err)
1300 return err;
1301
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001302 /* Specs say about reclaiming rings here, but we do that in DMA init */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001303 bgmac_chip_init(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001304
1305 err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
1306 KBUILD_MODNAME, net_dev);
1307 if (err < 0) {
1308 bgmac_err(bgmac, "IRQ request error: %d!\n", err);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001309 bgmac_dma_cleanup(bgmac);
1310 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001311 }
1312 napi_enable(&bgmac->napi);
1313
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001314 phy_start(bgmac->phy_dev);
1315
Florian Fainellic3897f22016-06-23 14:25:32 -07001316 netif_start_queue(net_dev);
1317
Felix Fietkau74b6f292015-04-14 12:08:00 +02001318 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001319}
1320
1321static int bgmac_stop(struct net_device *net_dev)
1322{
1323 struct bgmac *bgmac = netdev_priv(net_dev);
1324
1325 netif_carrier_off(net_dev);
1326
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001327 phy_stop(bgmac->phy_dev);
1328
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001329 napi_disable(&bgmac->napi);
1330 bgmac_chip_intrs_off(bgmac);
1331 free_irq(bgmac->core->irq, net_dev);
1332
1333 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001334 bgmac_dma_cleanup(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001335
1336 return 0;
1337}
1338
1339static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1340 struct net_device *net_dev)
1341{
1342 struct bgmac *bgmac = netdev_priv(net_dev);
1343 struct bgmac_dma_ring *ring;
1344
1345 /* No QOS support yet */
1346 ring = &bgmac->tx_ring[0];
1347 return bgmac_dma_tx_add(bgmac, ring, skb);
1348}
1349
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001350static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1351{
1352 struct bgmac *bgmac = netdev_priv(net_dev);
1353 int ret;
1354
1355 ret = eth_prepare_mac_addr_change(net_dev, addr);
1356 if (ret < 0)
1357 return ret;
1358 bgmac_write_mac_address(bgmac, (u8 *)addr);
1359 eth_commit_mac_addr_change(net_dev, addr);
1360 return 0;
1361}
1362
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001363static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1364{
1365 struct bgmac *bgmac = netdev_priv(net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001366
Hauke Mehrtens69c58852013-12-20 15:34:45 +01001367 if (!netif_running(net_dev))
1368 return -EINVAL;
1369
1370 return phy_mii_ioctl(bgmac->phy_dev, ifr, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001371}
1372
1373static const struct net_device_ops bgmac_netdev_ops = {
1374 .ndo_open = bgmac_open,
1375 .ndo_stop = bgmac_stop,
1376 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001377 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001378 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001379 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001380 .ndo_do_ioctl = bgmac_ioctl,
1381};
1382
1383/**************************************************
1384 * ethtool_ops
1385 **************************************************/
1386
1387static int bgmac_get_settings(struct net_device *net_dev,
1388 struct ethtool_cmd *cmd)
1389{
1390 struct bgmac *bgmac = netdev_priv(net_dev);
1391
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001392 return phy_ethtool_gset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001393}
1394
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001395static int bgmac_set_settings(struct net_device *net_dev,
1396 struct ethtool_cmd *cmd)
1397{
1398 struct bgmac *bgmac = netdev_priv(net_dev);
1399
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001400 return phy_ethtool_sset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001401}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001402
1403static void bgmac_get_drvinfo(struct net_device *net_dev,
1404 struct ethtool_drvinfo *info)
1405{
1406 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1407 strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
1408}
1409
1410static const struct ethtool_ops bgmac_ethtool_ops = {
1411 .get_settings = bgmac_get_settings,
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001412 .set_settings = bgmac_set_settings,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001413 .get_drvinfo = bgmac_get_drvinfo,
1414};
1415
1416/**************************************************
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001417 * MII
1418 **************************************************/
1419
1420static int bgmac_mii_read(struct mii_bus *bus, int mii_id, int regnum)
1421{
1422 return bgmac_phy_read(bus->priv, mii_id, regnum);
1423}
1424
1425static int bgmac_mii_write(struct mii_bus *bus, int mii_id, int regnum,
1426 u16 value)
1427{
1428 return bgmac_phy_write(bus->priv, mii_id, regnum, value);
1429}
1430
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001431static void bgmac_adjust_link(struct net_device *net_dev)
1432{
1433 struct bgmac *bgmac = netdev_priv(net_dev);
1434 struct phy_device *phy_dev = bgmac->phy_dev;
1435 bool update = false;
1436
1437 if (phy_dev->link) {
1438 if (phy_dev->speed != bgmac->mac_speed) {
1439 bgmac->mac_speed = phy_dev->speed;
1440 update = true;
1441 }
1442
1443 if (phy_dev->duplex != bgmac->mac_duplex) {
1444 bgmac->mac_duplex = phy_dev->duplex;
1445 update = true;
1446 }
1447 }
1448
1449 if (update) {
1450 bgmac_mac_speed(bgmac);
1451 phy_print_status(phy_dev);
1452 }
1453}
1454
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001455static int bgmac_fixed_phy_register(struct bgmac *bgmac)
1456{
1457 struct fixed_phy_status fphy_status = {
1458 .link = 1,
1459 .speed = SPEED_1000,
1460 .duplex = DUPLEX_FULL,
1461 };
1462 struct phy_device *phy_dev;
1463 int err;
1464
Fabio Estevam4db78d32015-09-02 13:25:59 -03001465 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001466 if (!phy_dev || IS_ERR(phy_dev)) {
1467 bgmac_err(bgmac, "Failed to register fixed PHY device\n");
1468 return -ENODEV;
1469 }
1470
1471 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1472 PHY_INTERFACE_MODE_MII);
1473 if (err) {
1474 bgmac_err(bgmac, "Connecting PHY failed\n");
1475 return err;
1476 }
1477
1478 bgmac->phy_dev = phy_dev;
1479
1480 return err;
1481}
1482
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001483static int bgmac_mii_register(struct bgmac *bgmac)
1484{
1485 struct mii_bus *mii_bus;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001486 struct phy_device *phy_dev;
1487 char bus_id[MII_BUS_ID_SIZE + 3];
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001488 int err = 0;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001489
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001490 if (bgmac_is_bcm4707_family(bgmac))
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001491 return bgmac_fixed_phy_register(bgmac);
1492
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001493 mii_bus = mdiobus_alloc();
1494 if (!mii_bus)
1495 return -ENOMEM;
1496
1497 mii_bus->name = "bgmac mii bus";
1498 sprintf(mii_bus->id, "%s-%d-%d", "bgmac", bgmac->core->bus->num,
1499 bgmac->core->core_unit);
1500 mii_bus->priv = bgmac;
1501 mii_bus->read = bgmac_mii_read;
1502 mii_bus->write = bgmac_mii_write;
1503 mii_bus->parent = &bgmac->core->dev;
1504 mii_bus->phy_mask = ~(1 << bgmac->phyaddr);
1505
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001506 err = mdiobus_register(mii_bus);
1507 if (err) {
1508 bgmac_err(bgmac, "Registration of mii bus failed\n");
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001509 goto err_free_bus;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001510 }
1511
1512 bgmac->mii_bus = mii_bus;
1513
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001514 /* Connect to the PHY */
1515 snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
1516 bgmac->phyaddr);
1517 phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
1518 PHY_INTERFACE_MODE_MII);
1519 if (IS_ERR(phy_dev)) {
Masanari Iidac01e0152016-04-20 00:27:33 +09001520 bgmac_err(bgmac, "PHY connection failed\n");
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001521 err = PTR_ERR(phy_dev);
1522 goto err_unregister_bus;
1523 }
1524 bgmac->phy_dev = phy_dev;
1525
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001526 return err;
1527
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001528err_unregister_bus:
1529 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001530err_free_bus:
1531 mdiobus_free(mii_bus);
1532 return err;
1533}
1534
1535static void bgmac_mii_unregister(struct bgmac *bgmac)
1536{
1537 struct mii_bus *mii_bus = bgmac->mii_bus;
1538
1539 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001540 mdiobus_free(mii_bus);
1541}
1542
1543/**************************************************
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001544 * BCMA bus ops
1545 **************************************************/
1546
1547/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
1548static int bgmac_probe(struct bcma_device *core)
1549{
1550 struct net_device *net_dev;
1551 struct bgmac *bgmac;
1552 struct ssb_sprom *sprom = &core->bus->sprom;
Rafał Miłecki538e4562015-08-26 17:53:45 +02001553 u8 *mac;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001554 int err;
1555
Rafał Miłecki538e4562015-08-26 17:53:45 +02001556 switch (core->core_unit) {
1557 case 0:
1558 mac = sprom->et0mac;
1559 break;
1560 case 1:
1561 mac = sprom->et1mac;
1562 break;
1563 case 2:
1564 mac = sprom->et2mac;
1565 break;
1566 default:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001567 pr_err("Unsupported core_unit %d\n", core->core_unit);
1568 return -ENOTSUPP;
1569 }
1570
Rafał Miłeckid166f212013-02-07 00:27:17 +00001571 if (!is_valid_ether_addr(mac)) {
1572 dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
1573 eth_random_addr(mac);
1574 dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
1575 }
1576
Rafał Miłeckib4dfd8e2016-04-12 13:30:45 +02001577 /* This (reset &) enable is not preset in specs or reference driver but
1578 * Broadcom does it in arch PCI code when enabling fake PCI device.
1579 */
1580 bcma_core_enable(core, 0);
1581
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001582 /* Allocation and references */
1583 net_dev = alloc_etherdev(sizeof(*bgmac));
1584 if (!net_dev)
1585 return -ENOMEM;
1586 net_dev->netdev_ops = &bgmac_netdev_ops;
1587 net_dev->irq = core->irq;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001588 net_dev->ethtool_ops = &bgmac_ethtool_ops;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001589 bgmac = netdev_priv(net_dev);
1590 bgmac->net_dev = net_dev;
1591 bgmac->core = core;
1592 bcma_set_drvdata(core, bgmac);
1593
1594 /* Defaults */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001595 memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
1596
1597 /* On BCM4706 we need common core to access PHY */
1598 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
1599 !core->bus->drv_gmac_cmn.core) {
1600 bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
1601 err = -ENODEV;
1602 goto err_netdev_free;
1603 }
1604 bgmac->cmn = core->bus->drv_gmac_cmn.core;
1605
Rafał Miłecki538e4562015-08-26 17:53:45 +02001606 switch (core->core_unit) {
1607 case 0:
1608 bgmac->phyaddr = sprom->et0phyaddr;
1609 break;
1610 case 1:
1611 bgmac->phyaddr = sprom->et1phyaddr;
1612 break;
1613 case 2:
1614 bgmac->phyaddr = sprom->et2phyaddr;
1615 break;
1616 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001617 bgmac->phyaddr &= BGMAC_PHY_MASK;
1618 if (bgmac->phyaddr == BGMAC_PHY_MASK) {
1619 bgmac_err(bgmac, "No PHY found\n");
1620 err = -ENODEV;
1621 goto err_netdev_free;
1622 }
1623 bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
1624 bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
1625
1626 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
1627 bgmac_err(bgmac, "PCI setup not implemented\n");
1628 err = -ENOTSUPP;
1629 goto err_netdev_free;
1630 }
1631
1632 bgmac_chip_reset(bgmac);
1633
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001634 /* For Northstar, we have to take all GMAC core out of reset */
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001635 if (bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001636 struct bcma_device *ns_core;
1637 int ns_gmac;
1638
1639 /* Northstar has 4 GMAC cores */
1640 for (ns_gmac = 0; ns_gmac < 4; ns_gmac++) {
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001641 /* As Northstar requirement, we have to reset all GMACs
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001642 * before accessing one. bgmac_chip_reset() call
1643 * bcma_core_enable() for this core. Then the other
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001644 * three GMACs didn't reset. We do it here.
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001645 */
1646 ns_core = bcma_find_core_unit(core->bus,
1647 BCMA_CORE_MAC_GBIT,
1648 ns_gmac);
1649 if (ns_core && !bcma_core_is_enabled(ns_core))
1650 bcma_core_enable(ns_core, 0);
1651 }
1652 }
1653
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001654 err = bgmac_dma_alloc(bgmac);
1655 if (err) {
1656 bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
1657 goto err_netdev_free;
1658 }
1659
1660 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
Ralf Baechleedb15d82013-02-21 16:16:55 +01001661 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001662 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1663
1664 /* TODO: reset the external phy. Specs are needed */
1665 bgmac_phy_reset(bgmac);
1666
1667 bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
1668 BGMAC_BFL_ENETROBO);
1669 if (bgmac->has_robosw)
1670 bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
1671
1672 if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
1673 bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
1674
Hauke Mehrtens62166422015-01-18 19:49:58 +01001675 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1676
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001677 err = bgmac_mii_register(bgmac);
1678 if (err) {
1679 bgmac_err(bgmac, "Cannot register MDIO\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001680 goto err_dma_free;
1681 }
1682
Felix Fietkau9cde9452015-03-23 12:35:37 +01001683 net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1684 net_dev->hw_features = net_dev->features;
1685 net_dev->vlan_features = net_dev->features;
1686
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001687 err = register_netdev(bgmac->net_dev);
1688 if (err) {
1689 bgmac_err(bgmac, "Cannot register net device\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001690 goto err_mii_unregister;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001691 }
1692
1693 netif_carrier_off(net_dev);
1694
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001695 return 0;
1696
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001697err_mii_unregister:
1698 bgmac_mii_unregister(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001699err_dma_free:
1700 bgmac_dma_free(bgmac);
1701
1702err_netdev_free:
1703 bcma_set_drvdata(core, NULL);
1704 free_netdev(net_dev);
1705
1706 return err;
1707}
1708
1709static void bgmac_remove(struct bcma_device *core)
1710{
1711 struct bgmac *bgmac = bcma_get_drvdata(core);
1712
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001713 unregister_netdev(bgmac->net_dev);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001714 bgmac_mii_unregister(bgmac);
Hauke Mehrtens62166422015-01-18 19:49:58 +01001715 netif_napi_del(&bgmac->napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001716 bgmac_dma_free(bgmac);
1717 bcma_set_drvdata(core, NULL);
1718 free_netdev(bgmac->net_dev);
1719}
1720
1721static struct bcma_driver bgmac_bcma_driver = {
1722 .name = KBUILD_MODNAME,
1723 .id_table = bgmac_bcma_tbl,
1724 .probe = bgmac_probe,
1725 .remove = bgmac_remove,
1726};
1727
1728static int __init bgmac_init(void)
1729{
1730 int err;
1731
1732 err = bcma_driver_register(&bgmac_bcma_driver);
1733 if (err)
1734 return err;
1735 pr_info("Broadcom 47xx GBit MAC driver loaded\n");
1736
1737 return 0;
1738}
1739
1740static void __exit bgmac_exit(void)
1741{
1742 bcma_driver_unregister(&bgmac_bcma_driver);
1743}
1744
1745module_init(bgmac_init)
1746module_exit(bgmac_exit)
1747
1748MODULE_AUTHOR("Rafał Miłecki");
1749MODULE_LICENSE("GPL");