blob: e6e74ca86e606c03c5b56def74d6a8aa79d44a73 [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
234 while (i > 0) {
235 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);
Florian Fainelli6d490f622016-06-07 15:06:15 -0700249 net_dev->stats.tx_dropped++;
250 net_dev->stats.tx_errors++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000251 return NETDEV_TX_OK;
252}
253
254/* Free transmitted packets */
255static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
256{
257 struct device *dma_dev = bgmac->core->dma_dev;
258 int empty_slot;
259 bool freed = false;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200260 unsigned bytes_compl = 0, pkts_compl = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000261
262 /* The last slot that hardware didn't consume yet */
263 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
264 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200265 empty_slot -= ring->index_base;
266 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000267 empty_slot /= sizeof(struct bgmac_dma_desc);
268
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200269 while (ring->start != ring->end) {
270 int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
271 struct bgmac_slot_info *slot = &ring->slots[slot_idx];
272 u32 ctl1;
273 int len;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100274
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200275 if (slot_idx == empty_slot)
276 break;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100277
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200278 ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
279 len = ctl1 & BGMAC_DESC_CTL1_LEN;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100280 if (ctl1 & BGMAC_DESC_CTL0_SOF)
281 /* Unmap no longer used buffer */
282 dma_unmap_single(dma_dev, slot->dma_addr, len,
283 DMA_TO_DEVICE);
284 else
285 dma_unmap_page(dma_dev, slot->dma_addr, len,
286 DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000287
288 if (slot->skb) {
Florian Fainelli6d490f622016-06-07 15:06:15 -0700289 bgmac->net_dev->stats.tx_bytes += slot->skb->len;
290 bgmac->net_dev->stats.tx_packets++;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200291 bytes_compl += slot->skb->len;
292 pkts_compl++;
293
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000294 /* Free memory! :) */
295 dev_kfree_skb(slot->skb);
296 slot->skb = NULL;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000297 }
298
Felix Fietkau9cde9452015-03-23 12:35:37 +0100299 slot->dma_addr = 0;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200300 ring->start++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000301 freed = true;
302 }
303
Felix Fietkau9cde9452015-03-23 12:35:37 +0100304 if (!pkts_compl)
305 return;
306
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200307 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
308
Felix Fietkau9cde9452015-03-23 12:35:37 +0100309 if (netif_queue_stopped(bgmac->net_dev))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000310 netif_wake_queue(bgmac->net_dev);
311}
312
313static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
314{
315 if (!ring->mmio_base)
316 return;
317
318 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
319 if (!bgmac_wait_value(bgmac->core,
320 ring->mmio_base + BGMAC_DMA_RX_STATUS,
321 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
322 10000))
323 bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
324 ring->mmio_base);
325}
326
327static void bgmac_dma_rx_enable(struct bgmac *bgmac,
328 struct bgmac_dma_ring *ring)
329{
330 u32 ctl;
331
332 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100333 if (bgmac->core->id.rev >= 4) {
334 ctl &= ~BGMAC_DMA_RX_BL_MASK;
335 ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
336
337 ctl &= ~BGMAC_DMA_RX_PC_MASK;
338 ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
339
340 ctl &= ~BGMAC_DMA_RX_PT_MASK;
341 ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
342 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000343 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
344 ctl |= BGMAC_DMA_RX_ENABLE;
345 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
346 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
347 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
348 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
349}
350
351static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
352 struct bgmac_slot_info *slot)
353{
354 struct device *dma_dev = bgmac->core->dma_dev;
Nathan Hintzb757a622013-10-29 19:32:01 -0700355 dma_addr_t dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000356 struct bgmac_rx_header *rx;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100357 void *buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000358
359 /* Alloc skb */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100360 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
361 if (!buf)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000362 return -ENOMEM;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000363
364 /* Poison - if everything goes fine, hardware will overwrite it */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200365 rx = buf + BGMAC_RX_BUF_OFFSET;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000366 rx->len = cpu_to_le16(0xdead);
367 rx->flags = cpu_to_le16(0xbeef);
368
369 /* Map skb for the DMA */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200370 dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
371 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
Nathan Hintzb757a622013-10-29 19:32:01 -0700372 if (dma_mapping_error(dma_dev, dma_addr)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000373 bgmac_err(bgmac, "DMA mapping error\n");
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100374 put_page(virt_to_head_page(buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000375 return -ENOMEM;
376 }
Nathan Hintzb757a622013-10-29 19:32:01 -0700377
378 /* Update the slot */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100379 slot->buf = buf;
Nathan Hintzb757a622013-10-29 19:32:01 -0700380 slot->dma_addr = dma_addr;
381
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000382 return 0;
383}
384
Felix Fietkau4668ae12015-04-14 12:08:01 +0200385static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
386 struct bgmac_dma_ring *ring)
387{
388 dma_wmb();
389
390 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
391 ring->index_base +
392 ring->end * sizeof(struct bgmac_dma_desc));
393}
394
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100395static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
396 struct bgmac_dma_ring *ring, int desc_idx)
397{
398 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
399 u32 ctl0 = 0, ctl1 = 0;
400
Felix Fietkau29ba8772015-04-14 12:08:02 +0200401 if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100402 ctl0 |= BGMAC_DESC_CTL0_EOT;
403 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
404 /* Is there any BGMAC device that requires extension? */
405 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
406 * B43_DMA64_DCTL1_ADDREXT_MASK;
407 */
408
409 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
410 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
411 dma_desc->ctl0 = cpu_to_le32(ctl0);
412 dma_desc->ctl1 = cpu_to_le32(ctl1);
Felix Fietkau4668ae12015-04-14 12:08:01 +0200413
414 ring->end = desc_idx;
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100415}
416
Felix Fietkau56faacd2015-04-14 12:07:57 +0200417static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
418 struct bgmac_slot_info *slot)
419{
420 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
421
422 dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
423 DMA_FROM_DEVICE);
424 rx->len = cpu_to_le16(0xdead);
425 rx->flags = cpu_to_le16(0xbeef);
426 dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
427 DMA_FROM_DEVICE);
428}
429
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000430static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
431 int weight)
432{
433 u32 end_slot;
434 int handled = 0;
435
436 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
437 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200438 end_slot -= ring->index_base;
439 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000440 end_slot /= sizeof(struct bgmac_dma_desc);
441
Felix Fietkau4668ae12015-04-14 12:08:01 +0200442 while (ring->start != end_slot) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000443 struct device *dma_dev = bgmac->core->dma_dev;
444 struct bgmac_slot_info *slot = &ring->slots[ring->start];
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200445 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100446 struct sk_buff *skb;
447 void *buf = slot->buf;
Felix Fietkau56faacd2015-04-14 12:07:57 +0200448 dma_addr_t dma_addr = slot->dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000449 u16 len, flags;
450
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100451 do {
Felix Fietkau56faacd2015-04-14 12:07:57 +0200452 /* Prepare new skb as replacement */
453 if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
454 bgmac_dma_rx_poison_buf(dma_dev, slot);
455 break;
456 }
457
458 /* Unmap buffer to make it accessible to the CPU */
459 dma_unmap_single(dma_dev, dma_addr,
460 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
461
462 /* Get info from the header */
463 len = le16_to_cpu(rx->len);
464 flags = le16_to_cpu(rx->flags);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100465
466 /* Check for poison and drop or pass the packet */
467 if (len == 0xdead && flags == 0xbeef) {
468 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
469 ring->start);
Felix Fietkau56faacd2015-04-14 12:07:57 +0200470 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700471 bgmac->net_dev->stats.rx_errors++;
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100472 break;
473 }
474
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200475 if (len > BGMAC_RX_ALLOC_SIZE) {
476 bgmac_err(bgmac, "Found oversized packet at slot %d, DMA issue!\n",
477 ring->start);
478 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700479 bgmac->net_dev->stats.rx_length_errors++;
480 bgmac->net_dev->stats.rx_errors++;
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200481 break;
482 }
483
Hauke Mehrtens02e71122013-02-28 07:16:54 +0000484 /* Omit CRC. */
485 len -= ETH_FCS_LEN;
486
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100487 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
David S. Miller750afbf2016-01-15 16:07:13 -0500488 if (unlikely(!skb)) {
wangweidongf1640c32016-01-13 11:06:41 +0800489 bgmac_err(bgmac, "build_skb failed\n");
490 put_page(virt_to_head_page(buf));
Florian Fainelli6d490f622016-06-07 15:06:15 -0700491 bgmac->net_dev->stats.rx_errors++;
wangweidongf1640c32016-01-13 11:06:41 +0800492 break;
493 }
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200494 skb_put(skb, BGMAC_RX_FRAME_OFFSET +
495 BGMAC_RX_BUF_OFFSET + len);
496 skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
497 BGMAC_RX_BUF_OFFSET);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100498
499 skb_checksum_none_assert(skb);
500 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
Florian Fainelli6d490f622016-06-07 15:06:15 -0700501 bgmac->net_dev->stats.rx_bytes += len;
502 bgmac->net_dev->stats.rx_packets++;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100503 napi_gro_receive(&bgmac->napi, skb);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100504 handled++;
505 } while (0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000506
Felix Fietkau56faacd2015-04-14 12:07:57 +0200507 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
508
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000509 if (++ring->start >= BGMAC_RX_RING_SLOTS)
510 ring->start = 0;
511
512 if (handled >= weight) /* Should never be greater */
513 break;
514 }
515
Felix Fietkau4668ae12015-04-14 12:08:01 +0200516 bgmac_dma_rx_update_index(bgmac, ring);
517
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000518 return handled;
519}
520
521/* Does ring support unaligned addressing? */
522static bool bgmac_dma_unaligned(struct bgmac *bgmac,
523 struct bgmac_dma_ring *ring,
524 enum bgmac_dma_ring_type ring_type)
525{
526 switch (ring_type) {
527 case BGMAC_DMA_RING_TX:
528 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
529 0xff0);
530 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
531 return true;
532 break;
533 case BGMAC_DMA_RING_RX:
534 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
535 0xff0);
536 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
537 return true;
538 break;
539 }
540 return false;
541}
542
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100543static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
544 struct bgmac_dma_ring *ring)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000545{
546 struct device *dma_dev = bgmac->core->dma_dev;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100547 struct bgmac_dma_desc *dma_desc = ring->cpu_base;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000548 struct bgmac_slot_info *slot;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000549 int i;
550
Felix Fietkau29ba8772015-04-14 12:08:02 +0200551 for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100552 int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN;
553
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000554 slot = &ring->slots[i];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100555 dev_kfree_skb(slot->skb);
556
557 if (!slot->dma_addr)
558 continue;
559
560 if (slot->skb)
561 dma_unmap_single(dma_dev, slot->dma_addr,
562 len, DMA_TO_DEVICE);
563 else
564 dma_unmap_page(dma_dev, slot->dma_addr,
565 len, DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000566 }
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100567}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000568
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100569static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
570 struct bgmac_dma_ring *ring)
571{
572 struct device *dma_dev = bgmac->core->dma_dev;
573 struct bgmac_slot_info *slot;
574 int i;
575
Felix Fietkau29ba8772015-04-14 12:08:02 +0200576 for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100577 slot = &ring->slots[i];
Felix Fietkau56faacd2015-04-14 12:07:57 +0200578 if (!slot->dma_addr)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100579 continue;
580
Felix Fietkau56faacd2015-04-14 12:07:57 +0200581 dma_unmap_single(dma_dev, slot->dma_addr,
582 BGMAC_RX_BUF_SIZE,
583 DMA_FROM_DEVICE);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100584 put_page(virt_to_head_page(slot->buf));
Felix Fietkau56faacd2015-04-14 12:07:57 +0200585 slot->dma_addr = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000586 }
587}
588
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100589static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
Felix Fietkau29ba8772015-04-14 12:08:02 +0200590 struct bgmac_dma_ring *ring,
591 int num_slots)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100592{
593 struct device *dma_dev = bgmac->core->dma_dev;
594 int size;
595
596 if (!ring->cpu_base)
597 return;
598
599 /* Free ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200600 size = num_slots * sizeof(struct bgmac_dma_desc);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100601 dma_free_coherent(dma_dev, size, ring->cpu_base,
602 ring->dma_base);
603}
604
Felix Fietkau74b6f292015-04-14 12:08:00 +0200605static void bgmac_dma_cleanup(struct bgmac *bgmac)
606{
607 int i;
608
609 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
610 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
611
612 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
613 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
614}
615
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000616static void bgmac_dma_free(struct bgmac *bgmac)
617{
618 int i;
619
Felix Fietkau74b6f292015-04-14 12:08:00 +0200620 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200621 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
622 BGMAC_TX_RING_SLOTS);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200623
624 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200625 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
626 BGMAC_RX_RING_SLOTS);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000627}
628
629static int bgmac_dma_alloc(struct bgmac *bgmac)
630{
631 struct device *dma_dev = bgmac->core->dma_dev;
632 struct bgmac_dma_ring *ring;
633 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
634 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
635 int size; /* ring size: different for Tx and Rx */
636 int err;
637 int i;
638
639 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
640 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
641
642 if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
643 bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
644 return -ENOTSUPP;
645 }
646
647 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
648 ring = &bgmac->tx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000649 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000650
651 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200652 size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000653 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
654 &ring->dma_base,
655 GFP_KERNEL);
656 if (!ring->cpu_base) {
657 bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
658 ring->mmio_base);
659 goto err_dma_free;
660 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000661
Rafał Miłecki99003032013-09-15 23:13:18 +0200662 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
663 BGMAC_DMA_RING_TX);
664 if (ring->unaligned)
665 ring->index_base = lower_32_bits(ring->dma_base);
666 else
667 ring->index_base = 0;
668
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000669 /* No need to alloc TX slots yet */
670 }
671
672 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
673 ring = &bgmac->rx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000674 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000675
676 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200677 size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000678 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
679 &ring->dma_base,
680 GFP_KERNEL);
681 if (!ring->cpu_base) {
682 bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
683 ring->mmio_base);
684 err = -ENOMEM;
685 goto err_dma_free;
686 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000687
Rafał Miłecki99003032013-09-15 23:13:18 +0200688 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
689 BGMAC_DMA_RING_RX);
690 if (ring->unaligned)
691 ring->index_base = lower_32_bits(ring->dma_base);
692 else
693 ring->index_base = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000694 }
695
696 return 0;
697
698err_dma_free:
699 bgmac_dma_free(bgmac);
700 return -ENOMEM;
701}
702
Felix Fietkau74b6f292015-04-14 12:08:00 +0200703static int bgmac_dma_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000704{
705 struct bgmac_dma_ring *ring;
Felix Fietkau74b6f292015-04-14 12:08:00 +0200706 int i, err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000707
708 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
709 ring = &bgmac->tx_ring[i];
710
Rafał Miłecki99003032013-09-15 23:13:18 +0200711 if (!ring->unaligned)
712 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000713 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
714 lower_32_bits(ring->dma_base));
715 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
716 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200717 if (ring->unaligned)
718 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000719
720 ring->start = 0;
721 ring->end = 0; /* Points the slot that should *not* be read */
722 }
723
724 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000725 int j;
726
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000727 ring = &bgmac->rx_ring[i];
728
Rafał Miłecki99003032013-09-15 23:13:18 +0200729 if (!ring->unaligned)
730 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000731 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
732 lower_32_bits(ring->dma_base));
733 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
734 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200735 if (ring->unaligned)
736 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000737
Felix Fietkau4668ae12015-04-14 12:08:01 +0200738 ring->start = 0;
739 ring->end = 0;
Felix Fietkau29ba8772015-04-14 12:08:02 +0200740 for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
Felix Fietkau74b6f292015-04-14 12:08:00 +0200741 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
742 if (err)
743 goto error;
744
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100745 bgmac_dma_rx_setup_desc(bgmac, ring, j);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200746 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000747
Felix Fietkau4668ae12015-04-14 12:08:01 +0200748 bgmac_dma_rx_update_index(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000749 }
Felix Fietkau74b6f292015-04-14 12:08:00 +0200750
751 return 0;
752
753error:
754 bgmac_dma_cleanup(bgmac);
755 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000756}
757
758/**************************************************
759 * PHY ops
760 **************************************************/
761
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000762static u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000763{
764 struct bcma_device *core;
765 u16 phy_access_addr;
766 u16 phy_ctl_addr;
767 u32 tmp;
768
769 BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
770 BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
771 BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
772 BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
773 BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
774 BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
775 BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
776 BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
777 BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
778 BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
779 BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
780
781 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
782 core = bgmac->core->bus->drv_gmac_cmn.core;
783 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
784 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
785 } else {
786 core = bgmac->core;
787 phy_access_addr = BGMAC_PHY_ACCESS;
788 phy_ctl_addr = BGMAC_PHY_CNTL;
789 }
790
791 tmp = bcma_read32(core, phy_ctl_addr);
792 tmp &= ~BGMAC_PC_EPA_MASK;
793 tmp |= phyaddr;
794 bcma_write32(core, phy_ctl_addr, tmp);
795
796 tmp = BGMAC_PA_START;
797 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
798 tmp |= reg << BGMAC_PA_REG_SHIFT;
799 bcma_write32(core, phy_access_addr, tmp);
800
801 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
802 bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
803 phyaddr, reg);
804 return 0xffff;
805 }
806
807 return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
808}
809
810/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000811static int bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000812{
813 struct bcma_device *core;
814 u16 phy_access_addr;
815 u16 phy_ctl_addr;
816 u32 tmp;
817
818 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
819 core = bgmac->core->bus->drv_gmac_cmn.core;
820 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
821 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
822 } else {
823 core = bgmac->core;
824 phy_access_addr = BGMAC_PHY_ACCESS;
825 phy_ctl_addr = BGMAC_PHY_CNTL;
826 }
827
828 tmp = bcma_read32(core, phy_ctl_addr);
829 tmp &= ~BGMAC_PC_EPA_MASK;
830 tmp |= phyaddr;
831 bcma_write32(core, phy_ctl_addr, tmp);
832
833 bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
834 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
835 bgmac_warn(bgmac, "Error setting MDIO int\n");
836
837 tmp = BGMAC_PA_START;
838 tmp |= BGMAC_PA_WRITE;
839 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
840 tmp |= reg << BGMAC_PA_REG_SHIFT;
841 tmp |= value;
842 bcma_write32(core, phy_access_addr, tmp);
843
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000844 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000845 bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
846 phyaddr, reg);
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000847 return -ETIMEDOUT;
848 }
849
850 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000851}
852
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000853/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
854static void bgmac_phy_init(struct bgmac *bgmac)
855{
856 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
857 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
858 u8 i;
859
860 if (ci->id == BCMA_CHIP_ID_BCM5356) {
861 for (i = 0; i < 5; i++) {
862 bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
863 bgmac_phy_write(bgmac, i, 0x15, 0x0100);
864 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
865 bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
866 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
867 }
868 }
869 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
870 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
871 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
872 bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
873 bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
874 for (i = 0; i < 5; i++) {
875 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
876 bgmac_phy_write(bgmac, i, 0x16, 0x5284);
877 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
878 bgmac_phy_write(bgmac, i, 0x17, 0x0010);
879 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
880 bgmac_phy_write(bgmac, i, 0x16, 0x5296);
881 bgmac_phy_write(bgmac, i, 0x17, 0x1073);
882 bgmac_phy_write(bgmac, i, 0x17, 0x9073);
883 bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
884 bgmac_phy_write(bgmac, i, 0x17, 0x9273);
885 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
886 }
887 }
888}
889
890/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
891static void bgmac_phy_reset(struct bgmac *bgmac)
892{
893 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
894 return;
895
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100896 bgmac_phy_write(bgmac, bgmac->phyaddr, MII_BMCR, BMCR_RESET);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000897 udelay(100);
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100898 if (bgmac_phy_read(bgmac, bgmac->phyaddr, MII_BMCR) & BMCR_RESET)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000899 bgmac_err(bgmac, "PHY reset failed\n");
900 bgmac_phy_init(bgmac);
901}
902
903/**************************************************
904 * Chip ops
905 **************************************************/
906
907/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
908 * nothing to change? Try if after stabilizng driver.
909 */
910static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
911 bool force)
912{
913 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
914 u32 new_val = (cmdcfg & mask) | set;
915
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100916 bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000917 udelay(2);
918
919 if (new_val != cmdcfg || force)
920 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
921
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100922 bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000923 udelay(2);
924}
925
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000926static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
927{
928 u32 tmp;
929
930 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
931 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
932 tmp = (addr[4] << 8) | addr[5];
933 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
934}
935
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000936static void bgmac_set_rx_mode(struct net_device *net_dev)
937{
938 struct bgmac *bgmac = netdev_priv(net_dev);
939
940 if (net_dev->flags & IFF_PROMISC)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000941 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000942 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000943 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000944}
945
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000946#if 0 /* We don't use that regs yet */
947static void bgmac_chip_stats_update(struct bgmac *bgmac)
948{
949 int i;
950
951 if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
952 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
953 bgmac->mib_tx_regs[i] =
954 bgmac_read(bgmac,
955 BGMAC_TX_GOOD_OCTETS + (i * 4));
956 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
957 bgmac->mib_rx_regs[i] =
958 bgmac_read(bgmac,
959 BGMAC_RX_GOOD_OCTETS + (i * 4));
960 }
961
962 /* TODO: what else? how to handle BCM4706? Specs are needed */
963}
964#endif
965
966static void bgmac_clear_mib(struct bgmac *bgmac)
967{
968 int i;
969
970 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
971 return;
972
973 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
974 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
975 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
976 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
977 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
978}
979
980/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100981static void bgmac_mac_speed(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000982{
983 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
984 u32 set = 0;
985
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100986 switch (bgmac->mac_speed) {
987 case SPEED_10:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000988 set |= BGMAC_CMDCFG_ES_10;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100989 break;
990 case SPEED_100:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000991 set |= BGMAC_CMDCFG_ES_100;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100992 break;
993 case SPEED_1000:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000994 set |= BGMAC_CMDCFG_ES_1000;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100995 break;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100996 case SPEED_2500:
997 set |= BGMAC_CMDCFG_ES_2500;
998 break;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100999 default:
1000 bgmac_err(bgmac, "Unsupported speed: %d\n", bgmac->mac_speed);
1001 }
1002
1003 if (bgmac->mac_duplex == DUPLEX_HALF)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001004 set |= BGMAC_CMDCFG_HD;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001005
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001006 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
1007}
1008
1009static void bgmac_miiconfig(struct bgmac *bgmac)
1010{
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001011 struct bcma_device *core = bgmac->core;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001012 u8 imode;
1013
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001014 if (bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001015 bcma_awrite32(core, BCMA_IOCTL,
1016 bcma_aread32(core, BCMA_IOCTL) | 0x40 |
1017 BGMAC_BCMA_IOCTL_SW_CLKEN);
1018 bgmac->mac_speed = SPEED_2500;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001019 bgmac->mac_duplex = DUPLEX_FULL;
1020 bgmac_mac_speed(bgmac);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001021 } else {
1022 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
1023 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
1024 if (imode == 0 || imode == 1) {
1025 bgmac->mac_speed = SPEED_100;
1026 bgmac->mac_duplex = DUPLEX_FULL;
1027 bgmac_mac_speed(bgmac);
1028 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001029 }
1030}
1031
1032/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
1033static void bgmac_chip_reset(struct bgmac *bgmac)
1034{
1035 struct bcma_device *core = bgmac->core;
1036 struct bcma_bus *bus = core->bus;
1037 struct bcma_chipinfo *ci = &bus->chipinfo;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001038 u32 flags;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001039 u32 iost;
1040 int i;
1041
1042 if (bcma_core_is_enabled(core)) {
1043 if (!bgmac->stats_grabbed) {
1044 /* bgmac_chip_stats_update(bgmac); */
1045 bgmac->stats_grabbed = true;
1046 }
1047
1048 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
1049 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
1050
1051 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
1052 udelay(1);
1053
1054 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
1055 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
1056
1057 /* TODO: Clear software multicast filter list */
1058 }
1059
1060 iost = bcma_aread32(core, BCMA_IOST);
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001061 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001062 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001063 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001064 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
1065
Rafał Miłecki9e4e6202016-02-22 22:51:13 +01001066 /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
1067 if (ci->id != BCMA_CHIP_ID_BCM4707 &&
1068 ci->id != BCMA_CHIP_ID_BCM47094) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001069 flags = 0;
1070 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
1071 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
1072 if (!bgmac->has_robosw)
1073 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
1074 }
1075 bcma_core_enable(core, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001076 }
1077
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001078 /* Request Misc PLL for corerev > 2 */
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001079 if (core->id.rev > 2 && !bgmac_is_bcm4707_family(bgmac)) {
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001080 bgmac_set(bgmac, BCMA_CLKCTLST,
1081 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
1082 bgmac_wait_value(bgmac->core, BCMA_CLKCTLST,
1083 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
1084 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001085 1000);
1086 }
1087
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001088 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1089 ci->id == BCMA_CHIP_ID_BCM4749 ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001090 ci->id == BCMA_CHIP_ID_BCM53572) {
1091 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
1092 u8 et_swtype = 0;
1093 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
Rafał Miłecki6a391e72013-09-15 00:22:47 +02001094 BGMAC_CHIPCTL_1_IF_TYPE_MII;
Hauke Mehrtens36472682013-09-15 22:49:08 +02001095 char buf[4];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001096
Hauke Mehrtens36472682013-09-15 22:49:08 +02001097 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001098 if (kstrtou8(buf, 0, &et_swtype))
1099 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
1100 buf);
1101 et_swtype &= 0x0f;
1102 et_swtype <<= 4;
1103 sw_type = et_swtype;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001104 } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM5358) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001105 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001106 } else if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
1107 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
1108 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188)) {
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +00001109 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
1110 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001111 }
1112 bcma_chipco_chipctl_maskset(cc, 1,
1113 ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
1114 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
1115 sw_type);
1116 }
1117
1118 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
1119 bcma_awrite32(core, BCMA_IOCTL,
1120 bcma_aread32(core, BCMA_IOCTL) &
1121 ~BGMAC_BCMA_IOCTL_SW_RESET);
1122
1123 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
1124 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
1125 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
1126 * be keps until taking MAC out of the reset.
1127 */
1128 bgmac_cmdcfg_maskset(bgmac,
1129 ~(BGMAC_CMDCFG_TE |
1130 BGMAC_CMDCFG_RE |
1131 BGMAC_CMDCFG_RPI |
1132 BGMAC_CMDCFG_TAI |
1133 BGMAC_CMDCFG_HD |
1134 BGMAC_CMDCFG_ML |
1135 BGMAC_CMDCFG_CFE |
1136 BGMAC_CMDCFG_RL |
1137 BGMAC_CMDCFG_RED |
1138 BGMAC_CMDCFG_PE |
1139 BGMAC_CMDCFG_TPI |
1140 BGMAC_CMDCFG_PAD_EN |
1141 BGMAC_CMDCFG_PF),
1142 BGMAC_CMDCFG_PROM |
1143 BGMAC_CMDCFG_NLC |
1144 BGMAC_CMDCFG_CFE |
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001145 BGMAC_CMDCFG_SR(core->id.rev),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001146 false);
Rafał Miłeckid4699622013-12-11 07:44:14 +01001147 bgmac->mac_speed = SPEED_UNKNOWN;
1148 bgmac->mac_duplex = DUPLEX_UNKNOWN;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001149
1150 bgmac_clear_mib(bgmac);
1151 if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
1152 bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
1153 BCMA_GMAC_CMN_PC_MTE);
1154 else
1155 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1156 bgmac_miiconfig(bgmac);
1157 bgmac_phy_init(bgmac);
1158
Hauke Mehrtens49a467b2013-09-29 13:54:58 +02001159 netdev_reset_queue(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001160}
1161
1162static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1163{
1164 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1165}
1166
1167static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1168{
1169 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
Nathan Hintz41608152013-02-13 19:14:10 +00001170 bgmac_read(bgmac, BGMAC_INT_MASK);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001171}
1172
1173/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1174static void bgmac_enable(struct bgmac *bgmac)
1175{
1176 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
1177 u32 cmdcfg;
1178 u32 mode;
1179 u32 rxq_ctl;
1180 u32 fl_ctl;
1181 u16 bp_clk;
1182 u8 mdp;
1183
1184 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
1185 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001186 BGMAC_CMDCFG_SR(bgmac->core->id.rev), true);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001187 udelay(2);
1188 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1189 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1190
1191 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1192 BGMAC_DS_MM_SHIFT;
1193 if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
1194 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1195 if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
1196 bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
1197 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1198
1199 switch (ci->id) {
1200 case BCMA_CHIP_ID_BCM5357:
1201 case BCMA_CHIP_ID_BCM4749:
1202 case BCMA_CHIP_ID_BCM53572:
1203 case BCMA_CHIP_ID_BCM4716:
1204 case BCMA_CHIP_ID_BCM47162:
1205 fl_ctl = 0x03cb04cb;
1206 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1207 ci->id == BCMA_CHIP_ID_BCM4749 ||
1208 ci->id == BCMA_CHIP_ID_BCM53572)
1209 fl_ctl = 0x2300e1;
1210 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1211 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
1212 break;
1213 }
1214
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001215 if (!bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001216 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1217 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1218 bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) /
1219 1000000;
1220 mdp = (bp_clk * 128 / 1000) - 3;
1221 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1222 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1223 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001224}
1225
1226/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001227static void bgmac_chip_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001228{
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001229 /* 1 interrupt per received frame */
1230 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1231
1232 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1233 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1234
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001235 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001236
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001237 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001238
1239 if (bgmac->loopback)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001240 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001241 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001242 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001243
1244 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1245
Felix Fietkau74b6f292015-04-14 12:08:00 +02001246 bgmac_chip_intrs_on(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001247
1248 bgmac_enable(bgmac);
1249}
1250
1251static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1252{
1253 struct bgmac *bgmac = netdev_priv(dev_id);
1254
1255 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1256 int_status &= bgmac->int_mask;
1257
1258 if (!int_status)
1259 return IRQ_NONE;
1260
Felix Fietkaueb64e292015-04-14 12:07:55 +02001261 int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1262 if (int_status)
1263 bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", int_status);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001264
1265 /* Disable new interrupts until handling existing ones */
1266 bgmac_chip_intrs_off(bgmac);
1267
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001268 napi_schedule(&bgmac->napi);
1269
1270 return IRQ_HANDLED;
1271}
1272
1273static int bgmac_poll(struct napi_struct *napi, int weight)
1274{
1275 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001276 int handled = 0;
1277
Felix Fietkaueb64e292015-04-14 12:07:55 +02001278 /* Ack */
1279 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001280
Felix Fietkaueb64e292015-04-14 12:07:55 +02001281 bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1282 handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001283
Felix Fietkaueb64e292015-04-14 12:07:55 +02001284 /* Poll again if more events arrived in the meantime */
1285 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
Rafał Miłeckie5802672015-04-23 20:56:29 +02001286 return weight;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001287
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001288 if (handled < weight) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001289 napi_complete(napi);
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001290 bgmac_chip_intrs_on(bgmac);
1291 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001292
1293 return handled;
1294}
1295
1296/**************************************************
1297 * net_device_ops
1298 **************************************************/
1299
1300static int bgmac_open(struct net_device *net_dev)
1301{
1302 struct bgmac *bgmac = netdev_priv(net_dev);
1303 int err = 0;
1304
1305 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001306
1307 err = bgmac_dma_init(bgmac);
1308 if (err)
1309 return err;
1310
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001311 /* Specs say about reclaiming rings here, but we do that in DMA init */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001312 bgmac_chip_init(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001313
1314 err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
1315 KBUILD_MODNAME, net_dev);
1316 if (err < 0) {
1317 bgmac_err(bgmac, "IRQ request error: %d!\n", err);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001318 bgmac_dma_cleanup(bgmac);
1319 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001320 }
1321 napi_enable(&bgmac->napi);
1322
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001323 phy_start(net_dev->phydev);
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001324
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001325 netif_carrier_on(net_dev);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001326 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001327}
1328
1329static int bgmac_stop(struct net_device *net_dev)
1330{
1331 struct bgmac *bgmac = netdev_priv(net_dev);
1332
1333 netif_carrier_off(net_dev);
1334
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001335 phy_stop(net_dev->phydev);
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001336
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001337 napi_disable(&bgmac->napi);
1338 bgmac_chip_intrs_off(bgmac);
1339 free_irq(bgmac->core->irq, net_dev);
1340
1341 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001342 bgmac_dma_cleanup(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001343
1344 return 0;
1345}
1346
1347static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1348 struct net_device *net_dev)
1349{
1350 struct bgmac *bgmac = netdev_priv(net_dev);
1351 struct bgmac_dma_ring *ring;
1352
1353 /* No QOS support yet */
1354 ring = &bgmac->tx_ring[0];
1355 return bgmac_dma_tx_add(bgmac, ring, skb);
1356}
1357
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001358static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1359{
1360 struct bgmac *bgmac = netdev_priv(net_dev);
1361 int ret;
1362
1363 ret = eth_prepare_mac_addr_change(net_dev, addr);
1364 if (ret < 0)
1365 return ret;
1366 bgmac_write_mac_address(bgmac, (u8 *)addr);
1367 eth_commit_mac_addr_change(net_dev, addr);
1368 return 0;
1369}
1370
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001371static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1372{
Hauke Mehrtens69c58852013-12-20 15:34:45 +01001373 if (!netif_running(net_dev))
1374 return -EINVAL;
1375
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001376 return phy_mii_ioctl(net_dev->phydev, ifr, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001377}
1378
1379static const struct net_device_ops bgmac_netdev_ops = {
1380 .ndo_open = bgmac_open,
1381 .ndo_stop = bgmac_stop,
1382 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001383 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001384 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001385 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001386 .ndo_do_ioctl = bgmac_ioctl,
1387};
1388
1389/**************************************************
1390 * ethtool_ops
1391 **************************************************/
1392
Florian Fainellif6613d42016-06-07 15:06:14 -07001393struct bgmac_stat {
1394 u8 size;
1395 u32 offset;
1396 const char *name;
1397};
1398
1399static struct bgmac_stat bgmac_get_strings_stats[] = {
1400 { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
1401 { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
1402 { 8, BGMAC_TX_OCTETS, "tx_octets" },
1403 { 4, BGMAC_TX_PKTS, "tx_pkts" },
1404 { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
1405 { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
1406 { 4, BGMAC_TX_LEN_64, "tx_64" },
1407 { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
1408 { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
1409 { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
1410 { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
1411 { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
1412 { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
1413 { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
1414 { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
1415 { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
1416 { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
1417 { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
1418 { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
1419 { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
1420 { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
1421 { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
1422 { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
1423 { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
1424 { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
1425 { 4, BGMAC_TX_DEFERED, "tx_defered" },
1426 { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
1427 { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
1428 { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
1429 { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
1430 { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
1431 { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
1432 { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
1433 { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
1434 { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
1435 { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
1436 { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
1437 { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
1438 { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
1439 { 8, BGMAC_RX_OCTETS, "rx_octets" },
1440 { 4, BGMAC_RX_PKTS, "rx_pkts" },
1441 { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
1442 { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
1443 { 4, BGMAC_RX_LEN_64, "rx_64" },
1444 { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
1445 { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
1446 { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
1447 { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
1448 { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
1449 { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
1450 { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
1451 { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
1452 { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
1453 { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
1454 { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
1455 { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
1456 { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
1457 { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
1458 { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
1459 { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
1460 { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
1461 { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
1462 { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
1463 { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
1464 { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
1465 { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
1466};
1467
1468#define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
1469
1470static int bgmac_get_sset_count(struct net_device *dev, int string_set)
1471{
1472 switch (string_set) {
1473 case ETH_SS_STATS:
1474 return BGMAC_STATS_LEN;
1475 }
1476
1477 return -EOPNOTSUPP;
1478}
1479
1480static void bgmac_get_strings(struct net_device *dev, u32 stringset,
1481 u8 *data)
1482{
1483 int i;
1484
1485 if (stringset != ETH_SS_STATS)
1486 return;
1487
1488 for (i = 0; i < BGMAC_STATS_LEN; i++)
1489 strlcpy(data + i * ETH_GSTRING_LEN,
1490 bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
1491}
1492
1493static void bgmac_get_ethtool_stats(struct net_device *dev,
1494 struct ethtool_stats *ss, uint64_t *data)
1495{
1496 struct bgmac *bgmac = netdev_priv(dev);
1497 const struct bgmac_stat *s;
1498 unsigned int i;
1499 u64 val;
1500
1501 if (!netif_running(dev))
1502 return;
1503
1504 for (i = 0; i < BGMAC_STATS_LEN; i++) {
1505 s = &bgmac_get_strings_stats[i];
1506 val = 0;
1507 if (s->size == 8)
1508 val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
1509 val |= bgmac_read(bgmac, s->offset);
1510 data[i] = val;
1511 }
1512}
1513
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001514static void bgmac_get_drvinfo(struct net_device *net_dev,
1515 struct ethtool_drvinfo *info)
1516{
1517 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1518 strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
1519}
1520
1521static const struct ethtool_ops bgmac_ethtool_ops = {
Florian Fainellif6613d42016-06-07 15:06:14 -07001522 .get_strings = bgmac_get_strings,
1523 .get_sset_count = bgmac_get_sset_count,
1524 .get_ethtool_stats = bgmac_get_ethtool_stats,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001525 .get_drvinfo = bgmac_get_drvinfo,
Philippe Reynes904632a2016-06-19 22:37:06 +02001526 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1527 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001528};
1529
1530/**************************************************
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001531 * MII
1532 **************************************************/
1533
1534static int bgmac_mii_read(struct mii_bus *bus, int mii_id, int regnum)
1535{
1536 return bgmac_phy_read(bus->priv, mii_id, regnum);
1537}
1538
1539static int bgmac_mii_write(struct mii_bus *bus, int mii_id, int regnum,
1540 u16 value)
1541{
1542 return bgmac_phy_write(bus->priv, mii_id, regnum, value);
1543}
1544
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001545static void bgmac_adjust_link(struct net_device *net_dev)
1546{
1547 struct bgmac *bgmac = netdev_priv(net_dev);
Philippe Reynesb21fcb22016-06-19 22:37:05 +02001548 struct phy_device *phy_dev = net_dev->phydev;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001549 bool update = false;
1550
1551 if (phy_dev->link) {
1552 if (phy_dev->speed != bgmac->mac_speed) {
1553 bgmac->mac_speed = phy_dev->speed;
1554 update = true;
1555 }
1556
1557 if (phy_dev->duplex != bgmac->mac_duplex) {
1558 bgmac->mac_duplex = phy_dev->duplex;
1559 update = true;
1560 }
1561 }
1562
1563 if (update) {
1564 bgmac_mac_speed(bgmac);
1565 phy_print_status(phy_dev);
1566 }
1567}
1568
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001569static int bgmac_fixed_phy_register(struct bgmac *bgmac)
1570{
1571 struct fixed_phy_status fphy_status = {
1572 .link = 1,
1573 .speed = SPEED_1000,
1574 .duplex = DUPLEX_FULL,
1575 };
1576 struct phy_device *phy_dev;
1577 int err;
1578
Fabio Estevam4db78d32015-09-02 13:25:59 -03001579 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001580 if (!phy_dev || IS_ERR(phy_dev)) {
1581 bgmac_err(bgmac, "Failed to register fixed PHY device\n");
1582 return -ENODEV;
1583 }
1584
1585 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1586 PHY_INTERFACE_MODE_MII);
1587 if (err) {
1588 bgmac_err(bgmac, "Connecting PHY failed\n");
1589 return err;
1590 }
1591
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001592 return err;
1593}
1594
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001595static int bgmac_mii_register(struct bgmac *bgmac)
1596{
1597 struct mii_bus *mii_bus;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001598 struct phy_device *phy_dev;
1599 char bus_id[MII_BUS_ID_SIZE + 3];
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001600 int err = 0;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001601
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001602 if (bgmac_is_bcm4707_family(bgmac))
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001603 return bgmac_fixed_phy_register(bgmac);
1604
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001605 mii_bus = mdiobus_alloc();
1606 if (!mii_bus)
1607 return -ENOMEM;
1608
1609 mii_bus->name = "bgmac mii bus";
1610 sprintf(mii_bus->id, "%s-%d-%d", "bgmac", bgmac->core->bus->num,
1611 bgmac->core->core_unit);
1612 mii_bus->priv = bgmac;
1613 mii_bus->read = bgmac_mii_read;
1614 mii_bus->write = bgmac_mii_write;
1615 mii_bus->parent = &bgmac->core->dev;
1616 mii_bus->phy_mask = ~(1 << bgmac->phyaddr);
1617
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001618 err = mdiobus_register(mii_bus);
1619 if (err) {
1620 bgmac_err(bgmac, "Registration of mii bus failed\n");
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001621 goto err_free_bus;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001622 }
1623
1624 bgmac->mii_bus = mii_bus;
1625
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001626 /* Connect to the PHY */
1627 snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
1628 bgmac->phyaddr);
1629 phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
1630 PHY_INTERFACE_MODE_MII);
1631 if (IS_ERR(phy_dev)) {
Masanari Iidac01e0152016-04-20 00:27:33 +09001632 bgmac_err(bgmac, "PHY connection failed\n");
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001633 err = PTR_ERR(phy_dev);
1634 goto err_unregister_bus;
1635 }
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001636
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001637 return err;
1638
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001639err_unregister_bus:
1640 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001641err_free_bus:
1642 mdiobus_free(mii_bus);
1643 return err;
1644}
1645
1646static void bgmac_mii_unregister(struct bgmac *bgmac)
1647{
1648 struct mii_bus *mii_bus = bgmac->mii_bus;
1649
1650 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001651 mdiobus_free(mii_bus);
1652}
1653
1654/**************************************************
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001655 * BCMA bus ops
1656 **************************************************/
1657
1658/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
1659static int bgmac_probe(struct bcma_device *core)
1660{
1661 struct net_device *net_dev;
1662 struct bgmac *bgmac;
1663 struct ssb_sprom *sprom = &core->bus->sprom;
Rafał Miłecki538e4562015-08-26 17:53:45 +02001664 u8 *mac;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001665 int err;
1666
Rafał Miłecki538e4562015-08-26 17:53:45 +02001667 switch (core->core_unit) {
1668 case 0:
1669 mac = sprom->et0mac;
1670 break;
1671 case 1:
1672 mac = sprom->et1mac;
1673 break;
1674 case 2:
1675 mac = sprom->et2mac;
1676 break;
1677 default:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001678 pr_err("Unsupported core_unit %d\n", core->core_unit);
1679 return -ENOTSUPP;
1680 }
1681
Rafał Miłeckid166f212013-02-07 00:27:17 +00001682 if (!is_valid_ether_addr(mac)) {
1683 dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
1684 eth_random_addr(mac);
1685 dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
1686 }
1687
Rafał Miłeckib4dfd8e2016-04-12 13:30:45 +02001688 /* This (reset &) enable is not preset in specs or reference driver but
1689 * Broadcom does it in arch PCI code when enabling fake PCI device.
1690 */
1691 bcma_core_enable(core, 0);
1692
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001693 /* Allocation and references */
1694 net_dev = alloc_etherdev(sizeof(*bgmac));
1695 if (!net_dev)
1696 return -ENOMEM;
1697 net_dev->netdev_ops = &bgmac_netdev_ops;
1698 net_dev->irq = core->irq;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001699 net_dev->ethtool_ops = &bgmac_ethtool_ops;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001700 bgmac = netdev_priv(net_dev);
1701 bgmac->net_dev = net_dev;
1702 bgmac->core = core;
1703 bcma_set_drvdata(core, bgmac);
Florian Fainelli2022e9d2016-06-07 15:06:13 -07001704 SET_NETDEV_DEV(net_dev, &core->dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001705
1706 /* Defaults */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001707 memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
1708
1709 /* On BCM4706 we need common core to access PHY */
1710 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
1711 !core->bus->drv_gmac_cmn.core) {
1712 bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
1713 err = -ENODEV;
1714 goto err_netdev_free;
1715 }
1716 bgmac->cmn = core->bus->drv_gmac_cmn.core;
1717
Rafał Miłecki538e4562015-08-26 17:53:45 +02001718 switch (core->core_unit) {
1719 case 0:
1720 bgmac->phyaddr = sprom->et0phyaddr;
1721 break;
1722 case 1:
1723 bgmac->phyaddr = sprom->et1phyaddr;
1724 break;
1725 case 2:
1726 bgmac->phyaddr = sprom->et2phyaddr;
1727 break;
1728 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001729 bgmac->phyaddr &= BGMAC_PHY_MASK;
1730 if (bgmac->phyaddr == BGMAC_PHY_MASK) {
1731 bgmac_err(bgmac, "No PHY found\n");
1732 err = -ENODEV;
1733 goto err_netdev_free;
1734 }
1735 bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
1736 bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
1737
1738 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
1739 bgmac_err(bgmac, "PCI setup not implemented\n");
1740 err = -ENOTSUPP;
1741 goto err_netdev_free;
1742 }
1743
1744 bgmac_chip_reset(bgmac);
1745
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001746 /* For Northstar, we have to take all GMAC core out of reset */
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001747 if (bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001748 struct bcma_device *ns_core;
1749 int ns_gmac;
1750
1751 /* Northstar has 4 GMAC cores */
1752 for (ns_gmac = 0; ns_gmac < 4; ns_gmac++) {
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001753 /* As Northstar requirement, we have to reset all GMACs
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001754 * before accessing one. bgmac_chip_reset() call
1755 * bcma_core_enable() for this core. Then the other
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001756 * three GMACs didn't reset. We do it here.
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001757 */
1758 ns_core = bcma_find_core_unit(core->bus,
1759 BCMA_CORE_MAC_GBIT,
1760 ns_gmac);
1761 if (ns_core && !bcma_core_is_enabled(ns_core))
1762 bcma_core_enable(ns_core, 0);
1763 }
1764 }
1765
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001766 err = bgmac_dma_alloc(bgmac);
1767 if (err) {
1768 bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
1769 goto err_netdev_free;
1770 }
1771
1772 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
Ralf Baechleedb15d82013-02-21 16:16:55 +01001773 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001774 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1775
1776 /* TODO: reset the external phy. Specs are needed */
1777 bgmac_phy_reset(bgmac);
1778
1779 bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
1780 BGMAC_BFL_ENETROBO);
1781 if (bgmac->has_robosw)
1782 bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
1783
1784 if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
1785 bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
1786
Hauke Mehrtens62166422015-01-18 19:49:58 +01001787 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1788
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001789 err = bgmac_mii_register(bgmac);
1790 if (err) {
1791 bgmac_err(bgmac, "Cannot register MDIO\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001792 goto err_dma_free;
1793 }
1794
Felix Fietkau9cde9452015-03-23 12:35:37 +01001795 net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1796 net_dev->hw_features = net_dev->features;
1797 net_dev->vlan_features = net_dev->features;
1798
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001799 err = register_netdev(bgmac->net_dev);
1800 if (err) {
1801 bgmac_err(bgmac, "Cannot register net device\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001802 goto err_mii_unregister;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001803 }
1804
1805 netif_carrier_off(net_dev);
1806
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001807 return 0;
1808
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001809err_mii_unregister:
1810 bgmac_mii_unregister(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001811err_dma_free:
1812 bgmac_dma_free(bgmac);
1813
1814err_netdev_free:
1815 bcma_set_drvdata(core, NULL);
1816 free_netdev(net_dev);
1817
1818 return err;
1819}
1820
1821static void bgmac_remove(struct bcma_device *core)
1822{
1823 struct bgmac *bgmac = bcma_get_drvdata(core);
1824
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001825 unregister_netdev(bgmac->net_dev);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001826 bgmac_mii_unregister(bgmac);
Hauke Mehrtens62166422015-01-18 19:49:58 +01001827 netif_napi_del(&bgmac->napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001828 bgmac_dma_free(bgmac);
1829 bcma_set_drvdata(core, NULL);
1830 free_netdev(bgmac->net_dev);
1831}
1832
1833static struct bcma_driver bgmac_bcma_driver = {
1834 .name = KBUILD_MODNAME,
1835 .id_table = bgmac_bcma_tbl,
1836 .probe = bgmac_probe,
1837 .remove = bgmac_remove,
1838};
1839
1840static int __init bgmac_init(void)
1841{
1842 int err;
1843
1844 err = bcma_driver_register(&bgmac_bcma_driver);
1845 if (err)
1846 return err;
1847 pr_info("Broadcom 47xx GBit MAC driver loaded\n");
1848
1849 return 0;
1850}
1851
1852static void __exit bgmac_exit(void)
1853{
1854 bcma_driver_unregister(&bgmac_bcma_driver);
1855}
1856
1857module_init(bgmac_init)
1858module_exit(bgmac_exit)
1859
1860MODULE_AUTHOR("Rafał Miłecki");
1861MODULE_LICENSE("GPL");