blob: 133bf64c7a8bb4e5d84d4bfbc70c5912c78d425d [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);
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];
270 u32 ctl1;
271 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
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200276 ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
277 len = ctl1 & BGMAC_DESC_CTL1_LEN;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100278 if (ctl1 & BGMAC_DESC_CTL0_SOF)
279 /* Unmap no longer used buffer */
280 dma_unmap_single(dma_dev, slot->dma_addr, len,
281 DMA_TO_DEVICE);
282 else
283 dma_unmap_page(dma_dev, slot->dma_addr, len,
284 DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000285
286 if (slot->skb) {
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200287 bytes_compl += slot->skb->len;
288 pkts_compl++;
289
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000290 /* Free memory! :) */
291 dev_kfree_skb(slot->skb);
292 slot->skb = NULL;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000293 }
294
Felix Fietkau9cde9452015-03-23 12:35:37 +0100295 slot->dma_addr = 0;
Felix Fietkaub38c83d2015-04-14 12:07:54 +0200296 ring->start++;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000297 freed = true;
298 }
299
Felix Fietkau9cde9452015-03-23 12:35:37 +0100300 if (!pkts_compl)
301 return;
302
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200303 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
304
Felix Fietkau9cde9452015-03-23 12:35:37 +0100305 if (netif_queue_stopped(bgmac->net_dev))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000306 netif_wake_queue(bgmac->net_dev);
307}
308
309static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
310{
311 if (!ring->mmio_base)
312 return;
313
314 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
315 if (!bgmac_wait_value(bgmac->core,
316 ring->mmio_base + BGMAC_DMA_RX_STATUS,
317 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
318 10000))
319 bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
320 ring->mmio_base);
321}
322
323static void bgmac_dma_rx_enable(struct bgmac *bgmac,
324 struct bgmac_dma_ring *ring)
325{
326 u32 ctl;
327
328 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100329 if (bgmac->core->id.rev >= 4) {
330 ctl &= ~BGMAC_DMA_RX_BL_MASK;
331 ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
332
333 ctl &= ~BGMAC_DMA_RX_PC_MASK;
334 ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
335
336 ctl &= ~BGMAC_DMA_RX_PT_MASK;
337 ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
338 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000339 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
340 ctl |= BGMAC_DMA_RX_ENABLE;
341 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
342 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
343 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
344 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
345}
346
347static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
348 struct bgmac_slot_info *slot)
349{
350 struct device *dma_dev = bgmac->core->dma_dev;
Nathan Hintzb757a622013-10-29 19:32:01 -0700351 dma_addr_t dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000352 struct bgmac_rx_header *rx;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100353 void *buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000354
355 /* Alloc skb */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100356 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
357 if (!buf)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000358 return -ENOMEM;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000359
360 /* Poison - if everything goes fine, hardware will overwrite it */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200361 rx = buf + BGMAC_RX_BUF_OFFSET;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000362 rx->len = cpu_to_le16(0xdead);
363 rx->flags = cpu_to_le16(0xbeef);
364
365 /* Map skb for the DMA */
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200366 dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
367 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
Nathan Hintzb757a622013-10-29 19:32:01 -0700368 if (dma_mapping_error(dma_dev, dma_addr)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000369 bgmac_err(bgmac, "DMA mapping error\n");
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100370 put_page(virt_to_head_page(buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000371 return -ENOMEM;
372 }
Nathan Hintzb757a622013-10-29 19:32:01 -0700373
374 /* Update the slot */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100375 slot->buf = buf;
Nathan Hintzb757a622013-10-29 19:32:01 -0700376 slot->dma_addr = dma_addr;
377
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000378 return 0;
379}
380
Felix Fietkau4668ae12015-04-14 12:08:01 +0200381static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
382 struct bgmac_dma_ring *ring)
383{
384 dma_wmb();
385
386 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
387 ring->index_base +
388 ring->end * sizeof(struct bgmac_dma_desc));
389}
390
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100391static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
392 struct bgmac_dma_ring *ring, int desc_idx)
393{
394 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
395 u32 ctl0 = 0, ctl1 = 0;
396
Felix Fietkau29ba8772015-04-14 12:08:02 +0200397 if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100398 ctl0 |= BGMAC_DESC_CTL0_EOT;
399 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
400 /* Is there any BGMAC device that requires extension? */
401 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
402 * B43_DMA64_DCTL1_ADDREXT_MASK;
403 */
404
405 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
406 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
407 dma_desc->ctl0 = cpu_to_le32(ctl0);
408 dma_desc->ctl1 = cpu_to_le32(ctl1);
Felix Fietkau4668ae12015-04-14 12:08:01 +0200409
410 ring->end = desc_idx;
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100411}
412
Felix Fietkau56faacd2015-04-14 12:07:57 +0200413static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
414 struct bgmac_slot_info *slot)
415{
416 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
417
418 dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
419 DMA_FROM_DEVICE);
420 rx->len = cpu_to_le16(0xdead);
421 rx->flags = cpu_to_le16(0xbeef);
422 dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
423 DMA_FROM_DEVICE);
424}
425
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000426static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
427 int weight)
428{
429 u32 end_slot;
430 int handled = 0;
431
432 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
433 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200434 end_slot -= ring->index_base;
435 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000436 end_slot /= sizeof(struct bgmac_dma_desc);
437
Felix Fietkau4668ae12015-04-14 12:08:01 +0200438 while (ring->start != end_slot) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000439 struct device *dma_dev = bgmac->core->dma_dev;
440 struct bgmac_slot_info *slot = &ring->slots[ring->start];
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200441 struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100442 struct sk_buff *skb;
443 void *buf = slot->buf;
Felix Fietkau56faacd2015-04-14 12:07:57 +0200444 dma_addr_t dma_addr = slot->dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000445 u16 len, flags;
446
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100447 do {
Felix Fietkau56faacd2015-04-14 12:07:57 +0200448 /* Prepare new skb as replacement */
449 if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
450 bgmac_dma_rx_poison_buf(dma_dev, slot);
451 break;
452 }
453
454 /* Unmap buffer to make it accessible to the CPU */
455 dma_unmap_single(dma_dev, dma_addr,
456 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
457
458 /* Get info from the header */
459 len = le16_to_cpu(rx->len);
460 flags = le16_to_cpu(rx->flags);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100461
462 /* Check for poison and drop or pass the packet */
463 if (len == 0xdead && flags == 0xbeef) {
464 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
465 ring->start);
Felix Fietkau56faacd2015-04-14 12:07:57 +0200466 put_page(virt_to_head_page(buf));
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100467 break;
468 }
469
Felix Fietkau6a6c7082015-04-14 12:07:58 +0200470 if (len > BGMAC_RX_ALLOC_SIZE) {
471 bgmac_err(bgmac, "Found oversized packet at slot %d, DMA issue!\n",
472 ring->start);
473 put_page(virt_to_head_page(buf));
474 break;
475 }
476
Hauke Mehrtens02e71122013-02-28 07:16:54 +0000477 /* Omit CRC. */
478 len -= ETH_FCS_LEN;
479
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100480 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
David S. Miller750afbf2016-01-15 16:07:13 -0500481 if (unlikely(!skb)) {
wangweidongf1640c32016-01-13 11:06:41 +0800482 bgmac_err(bgmac, "build_skb failed\n");
483 put_page(virt_to_head_page(buf));
484 break;
485 }
Felix Fietkau4b62dce2015-04-14 12:07:56 +0200486 skb_put(skb, BGMAC_RX_FRAME_OFFSET +
487 BGMAC_RX_BUF_OFFSET + len);
488 skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
489 BGMAC_RX_BUF_OFFSET);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100490
491 skb_checksum_none_assert(skb);
492 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100493 napi_gro_receive(&bgmac->napi, skb);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100494 handled++;
495 } while (0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000496
Felix Fietkau56faacd2015-04-14 12:07:57 +0200497 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
498
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000499 if (++ring->start >= BGMAC_RX_RING_SLOTS)
500 ring->start = 0;
501
502 if (handled >= weight) /* Should never be greater */
503 break;
504 }
505
Felix Fietkau4668ae12015-04-14 12:08:01 +0200506 bgmac_dma_rx_update_index(bgmac, ring);
507
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000508 return handled;
509}
510
511/* Does ring support unaligned addressing? */
512static bool bgmac_dma_unaligned(struct bgmac *bgmac,
513 struct bgmac_dma_ring *ring,
514 enum bgmac_dma_ring_type ring_type)
515{
516 switch (ring_type) {
517 case BGMAC_DMA_RING_TX:
518 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
519 0xff0);
520 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
521 return true;
522 break;
523 case BGMAC_DMA_RING_RX:
524 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
525 0xff0);
526 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
527 return true;
528 break;
529 }
530 return false;
531}
532
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100533static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
534 struct bgmac_dma_ring *ring)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000535{
536 struct device *dma_dev = bgmac->core->dma_dev;
Felix Fietkau9cde9452015-03-23 12:35:37 +0100537 struct bgmac_dma_desc *dma_desc = ring->cpu_base;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000538 struct bgmac_slot_info *slot;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000539 int i;
540
Felix Fietkau29ba8772015-04-14 12:08:02 +0200541 for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
Felix Fietkau9cde9452015-03-23 12:35:37 +0100542 int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN;
543
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000544 slot = &ring->slots[i];
Felix Fietkau9cde9452015-03-23 12:35:37 +0100545 dev_kfree_skb(slot->skb);
546
547 if (!slot->dma_addr)
548 continue;
549
550 if (slot->skb)
551 dma_unmap_single(dma_dev, slot->dma_addr,
552 len, DMA_TO_DEVICE);
553 else
554 dma_unmap_page(dma_dev, slot->dma_addr,
555 len, DMA_TO_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000556 }
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100557}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000558
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100559static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
560 struct bgmac_dma_ring *ring)
561{
562 struct device *dma_dev = bgmac->core->dma_dev;
563 struct bgmac_slot_info *slot;
564 int i;
565
Felix Fietkau29ba8772015-04-14 12:08:02 +0200566 for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100567 slot = &ring->slots[i];
Felix Fietkau56faacd2015-04-14 12:07:57 +0200568 if (!slot->dma_addr)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100569 continue;
570
Felix Fietkau56faacd2015-04-14 12:07:57 +0200571 dma_unmap_single(dma_dev, slot->dma_addr,
572 BGMAC_RX_BUF_SIZE,
573 DMA_FROM_DEVICE);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100574 put_page(virt_to_head_page(slot->buf));
Felix Fietkau56faacd2015-04-14 12:07:57 +0200575 slot->dma_addr = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000576 }
577}
578
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100579static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
Felix Fietkau29ba8772015-04-14 12:08:02 +0200580 struct bgmac_dma_ring *ring,
581 int num_slots)
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100582{
583 struct device *dma_dev = bgmac->core->dma_dev;
584 int size;
585
586 if (!ring->cpu_base)
587 return;
588
589 /* Free ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200590 size = num_slots * sizeof(struct bgmac_dma_desc);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100591 dma_free_coherent(dma_dev, size, ring->cpu_base,
592 ring->dma_base);
593}
594
Felix Fietkau74b6f292015-04-14 12:08:00 +0200595static void bgmac_dma_cleanup(struct bgmac *bgmac)
596{
597 int i;
598
599 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
600 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
601
602 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
603 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
604}
605
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000606static void bgmac_dma_free(struct bgmac *bgmac)
607{
608 int i;
609
Felix Fietkau74b6f292015-04-14 12:08:00 +0200610 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200611 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
612 BGMAC_TX_RING_SLOTS);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200613
614 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
Felix Fietkau29ba8772015-04-14 12:08:02 +0200615 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
616 BGMAC_RX_RING_SLOTS);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000617}
618
619static int bgmac_dma_alloc(struct bgmac *bgmac)
620{
621 struct device *dma_dev = bgmac->core->dma_dev;
622 struct bgmac_dma_ring *ring;
623 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
624 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
625 int size; /* ring size: different for Tx and Rx */
626 int err;
627 int i;
628
629 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
630 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
631
632 if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
633 bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
634 return -ENOTSUPP;
635 }
636
637 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
638 ring = &bgmac->tx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000639 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000640
641 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200642 size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000643 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
644 &ring->dma_base,
645 GFP_KERNEL);
646 if (!ring->cpu_base) {
647 bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
648 ring->mmio_base);
649 goto err_dma_free;
650 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000651
Rafał Miłecki99003032013-09-15 23:13:18 +0200652 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
653 BGMAC_DMA_RING_TX);
654 if (ring->unaligned)
655 ring->index_base = lower_32_bits(ring->dma_base);
656 else
657 ring->index_base = 0;
658
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000659 /* No need to alloc TX slots yet */
660 }
661
662 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
663 ring = &bgmac->rx_ring[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000664 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000665
666 /* Alloc ring of descriptors */
Felix Fietkau29ba8772015-04-14 12:08:02 +0200667 size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000668 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
669 &ring->dma_base,
670 GFP_KERNEL);
671 if (!ring->cpu_base) {
672 bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
673 ring->mmio_base);
674 err = -ENOMEM;
675 goto err_dma_free;
676 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000677
Rafał Miłecki99003032013-09-15 23:13:18 +0200678 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
679 BGMAC_DMA_RING_RX);
680 if (ring->unaligned)
681 ring->index_base = lower_32_bits(ring->dma_base);
682 else
683 ring->index_base = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000684 }
685
686 return 0;
687
688err_dma_free:
689 bgmac_dma_free(bgmac);
690 return -ENOMEM;
691}
692
Felix Fietkau74b6f292015-04-14 12:08:00 +0200693static int bgmac_dma_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000694{
695 struct bgmac_dma_ring *ring;
Felix Fietkau74b6f292015-04-14 12:08:00 +0200696 int i, err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000697
698 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
699 ring = &bgmac->tx_ring[i];
700
Rafał Miłecki99003032013-09-15 23:13:18 +0200701 if (!ring->unaligned)
702 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000703 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
704 lower_32_bits(ring->dma_base));
705 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
706 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200707 if (ring->unaligned)
708 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000709
710 ring->start = 0;
711 ring->end = 0; /* Points the slot that should *not* be read */
712 }
713
714 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000715 int j;
716
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000717 ring = &bgmac->rx_ring[i];
718
Rafał Miłecki99003032013-09-15 23:13:18 +0200719 if (!ring->unaligned)
720 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000721 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
722 lower_32_bits(ring->dma_base));
723 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
724 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200725 if (ring->unaligned)
726 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000727
Felix Fietkau4668ae12015-04-14 12:08:01 +0200728 ring->start = 0;
729 ring->end = 0;
Felix Fietkau29ba8772015-04-14 12:08:02 +0200730 for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
Felix Fietkau74b6f292015-04-14 12:08:00 +0200731 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
732 if (err)
733 goto error;
734
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100735 bgmac_dma_rx_setup_desc(bgmac, ring, j);
Felix Fietkau74b6f292015-04-14 12:08:00 +0200736 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000737
Felix Fietkau4668ae12015-04-14 12:08:01 +0200738 bgmac_dma_rx_update_index(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000739 }
Felix Fietkau74b6f292015-04-14 12:08:00 +0200740
741 return 0;
742
743error:
744 bgmac_dma_cleanup(bgmac);
745 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000746}
747
748/**************************************************
749 * PHY ops
750 **************************************************/
751
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000752static u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000753{
754 struct bcma_device *core;
755 u16 phy_access_addr;
756 u16 phy_ctl_addr;
757 u32 tmp;
758
759 BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
760 BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
761 BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
762 BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
763 BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
764 BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
765 BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
766 BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
767 BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
768 BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
769 BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
770
771 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
772 core = bgmac->core->bus->drv_gmac_cmn.core;
773 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
774 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
775 } else {
776 core = bgmac->core;
777 phy_access_addr = BGMAC_PHY_ACCESS;
778 phy_ctl_addr = BGMAC_PHY_CNTL;
779 }
780
781 tmp = bcma_read32(core, phy_ctl_addr);
782 tmp &= ~BGMAC_PC_EPA_MASK;
783 tmp |= phyaddr;
784 bcma_write32(core, phy_ctl_addr, tmp);
785
786 tmp = BGMAC_PA_START;
787 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
788 tmp |= reg << BGMAC_PA_REG_SHIFT;
789 bcma_write32(core, phy_access_addr, tmp);
790
791 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
792 bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
793 phyaddr, reg);
794 return 0xffff;
795 }
796
797 return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
798}
799
800/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000801static int bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000802{
803 struct bcma_device *core;
804 u16 phy_access_addr;
805 u16 phy_ctl_addr;
806 u32 tmp;
807
808 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
809 core = bgmac->core->bus->drv_gmac_cmn.core;
810 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
811 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
812 } else {
813 core = bgmac->core;
814 phy_access_addr = BGMAC_PHY_ACCESS;
815 phy_ctl_addr = BGMAC_PHY_CNTL;
816 }
817
818 tmp = bcma_read32(core, phy_ctl_addr);
819 tmp &= ~BGMAC_PC_EPA_MASK;
820 tmp |= phyaddr;
821 bcma_write32(core, phy_ctl_addr, tmp);
822
823 bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
824 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
825 bgmac_warn(bgmac, "Error setting MDIO int\n");
826
827 tmp = BGMAC_PA_START;
828 tmp |= BGMAC_PA_WRITE;
829 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
830 tmp |= reg << BGMAC_PA_REG_SHIFT;
831 tmp |= value;
832 bcma_write32(core, phy_access_addr, tmp);
833
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000834 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000835 bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
836 phyaddr, reg);
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000837 return -ETIMEDOUT;
838 }
839
840 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000841}
842
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000843/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
844static void bgmac_phy_init(struct bgmac *bgmac)
845{
846 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
847 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
848 u8 i;
849
850 if (ci->id == BCMA_CHIP_ID_BCM5356) {
851 for (i = 0; i < 5; i++) {
852 bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
853 bgmac_phy_write(bgmac, i, 0x15, 0x0100);
854 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
855 bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
856 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
857 }
858 }
859 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
860 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
861 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
862 bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
863 bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
864 for (i = 0; i < 5; i++) {
865 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
866 bgmac_phy_write(bgmac, i, 0x16, 0x5284);
867 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
868 bgmac_phy_write(bgmac, i, 0x17, 0x0010);
869 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
870 bgmac_phy_write(bgmac, i, 0x16, 0x5296);
871 bgmac_phy_write(bgmac, i, 0x17, 0x1073);
872 bgmac_phy_write(bgmac, i, 0x17, 0x9073);
873 bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
874 bgmac_phy_write(bgmac, i, 0x17, 0x9273);
875 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
876 }
877 }
878}
879
880/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
881static void bgmac_phy_reset(struct bgmac *bgmac)
882{
883 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
884 return;
885
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100886 bgmac_phy_write(bgmac, bgmac->phyaddr, MII_BMCR, BMCR_RESET);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000887 udelay(100);
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100888 if (bgmac_phy_read(bgmac, bgmac->phyaddr, MII_BMCR) & BMCR_RESET)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000889 bgmac_err(bgmac, "PHY reset failed\n");
890 bgmac_phy_init(bgmac);
891}
892
893/**************************************************
894 * Chip ops
895 **************************************************/
896
897/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
898 * nothing to change? Try if after stabilizng driver.
899 */
900static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
901 bool force)
902{
903 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
904 u32 new_val = (cmdcfg & mask) | set;
905
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100906 bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000907 udelay(2);
908
909 if (new_val != cmdcfg || force)
910 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
911
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100912 bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000913 udelay(2);
914}
915
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000916static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
917{
918 u32 tmp;
919
920 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
921 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
922 tmp = (addr[4] << 8) | addr[5];
923 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
924}
925
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000926static void bgmac_set_rx_mode(struct net_device *net_dev)
927{
928 struct bgmac *bgmac = netdev_priv(net_dev);
929
930 if (net_dev->flags & IFF_PROMISC)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000931 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000932 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000933 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000934}
935
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000936#if 0 /* We don't use that regs yet */
937static void bgmac_chip_stats_update(struct bgmac *bgmac)
938{
939 int i;
940
941 if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
942 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
943 bgmac->mib_tx_regs[i] =
944 bgmac_read(bgmac,
945 BGMAC_TX_GOOD_OCTETS + (i * 4));
946 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
947 bgmac->mib_rx_regs[i] =
948 bgmac_read(bgmac,
949 BGMAC_RX_GOOD_OCTETS + (i * 4));
950 }
951
952 /* TODO: what else? how to handle BCM4706? Specs are needed */
953}
954#endif
955
956static void bgmac_clear_mib(struct bgmac *bgmac)
957{
958 int i;
959
960 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
961 return;
962
963 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
964 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
965 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
966 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
967 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
968}
969
970/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100971static void bgmac_mac_speed(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000972{
973 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
974 u32 set = 0;
975
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100976 switch (bgmac->mac_speed) {
977 case SPEED_10:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000978 set |= BGMAC_CMDCFG_ES_10;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100979 break;
980 case SPEED_100:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000981 set |= BGMAC_CMDCFG_ES_100;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100982 break;
983 case SPEED_1000:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000984 set |= BGMAC_CMDCFG_ES_1000;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100985 break;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100986 case SPEED_2500:
987 set |= BGMAC_CMDCFG_ES_2500;
988 break;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100989 default:
990 bgmac_err(bgmac, "Unsupported speed: %d\n", bgmac->mac_speed);
991 }
992
993 if (bgmac->mac_duplex == DUPLEX_HALF)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000994 set |= BGMAC_CMDCFG_HD;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100995
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000996 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
997}
998
999static void bgmac_miiconfig(struct bgmac *bgmac)
1000{
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001001 struct bcma_device *core = bgmac->core;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001002 u8 imode;
1003
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001004 if (bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001005 bcma_awrite32(core, BCMA_IOCTL,
1006 bcma_aread32(core, BCMA_IOCTL) | 0x40 |
1007 BGMAC_BCMA_IOCTL_SW_CLKEN);
1008 bgmac->mac_speed = SPEED_2500;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001009 bgmac->mac_duplex = DUPLEX_FULL;
1010 bgmac_mac_speed(bgmac);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001011 } else {
1012 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
1013 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
1014 if (imode == 0 || imode == 1) {
1015 bgmac->mac_speed = SPEED_100;
1016 bgmac->mac_duplex = DUPLEX_FULL;
1017 bgmac_mac_speed(bgmac);
1018 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001019 }
1020}
1021
1022/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
1023static void bgmac_chip_reset(struct bgmac *bgmac)
1024{
1025 struct bcma_device *core = bgmac->core;
1026 struct bcma_bus *bus = core->bus;
1027 struct bcma_chipinfo *ci = &bus->chipinfo;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001028 u32 flags;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001029 u32 iost;
1030 int i;
1031
1032 if (bcma_core_is_enabled(core)) {
1033 if (!bgmac->stats_grabbed) {
1034 /* bgmac_chip_stats_update(bgmac); */
1035 bgmac->stats_grabbed = true;
1036 }
1037
1038 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
1039 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
1040
1041 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
1042 udelay(1);
1043
1044 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
1045 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
1046
1047 /* TODO: Clear software multicast filter list */
1048 }
1049
1050 iost = bcma_aread32(core, BCMA_IOST);
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001051 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001052 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001053 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001054 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
1055
Rafał Miłecki9e4e6202016-02-22 22:51:13 +01001056 /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
1057 if (ci->id != BCMA_CHIP_ID_BCM4707 &&
1058 ci->id != BCMA_CHIP_ID_BCM47094) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001059 flags = 0;
1060 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
1061 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
1062 if (!bgmac->has_robosw)
1063 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
1064 }
1065 bcma_core_enable(core, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001066 }
1067
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001068 /* Request Misc PLL for corerev > 2 */
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001069 if (core->id.rev > 2 && !bgmac_is_bcm4707_family(bgmac)) {
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001070 bgmac_set(bgmac, BCMA_CLKCTLST,
1071 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
1072 bgmac_wait_value(bgmac->core, BCMA_CLKCTLST,
1073 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
1074 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001075 1000);
1076 }
1077
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001078 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1079 ci->id == BCMA_CHIP_ID_BCM4749 ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001080 ci->id == BCMA_CHIP_ID_BCM53572) {
1081 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
1082 u8 et_swtype = 0;
1083 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
Rafał Miłecki6a391e72013-09-15 00:22:47 +02001084 BGMAC_CHIPCTL_1_IF_TYPE_MII;
Hauke Mehrtens36472682013-09-15 22:49:08 +02001085 char buf[4];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001086
Hauke Mehrtens36472682013-09-15 22:49:08 +02001087 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001088 if (kstrtou8(buf, 0, &et_swtype))
1089 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
1090 buf);
1091 et_swtype &= 0x0f;
1092 et_swtype <<= 4;
1093 sw_type = et_swtype;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001094 } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM5358) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001095 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +01001096 } else if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
1097 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
1098 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188)) {
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +00001099 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
1100 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001101 }
1102 bcma_chipco_chipctl_maskset(cc, 1,
1103 ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
1104 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
1105 sw_type);
1106 }
1107
1108 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
1109 bcma_awrite32(core, BCMA_IOCTL,
1110 bcma_aread32(core, BCMA_IOCTL) &
1111 ~BGMAC_BCMA_IOCTL_SW_RESET);
1112
1113 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
1114 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
1115 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
1116 * be keps until taking MAC out of the reset.
1117 */
1118 bgmac_cmdcfg_maskset(bgmac,
1119 ~(BGMAC_CMDCFG_TE |
1120 BGMAC_CMDCFG_RE |
1121 BGMAC_CMDCFG_RPI |
1122 BGMAC_CMDCFG_TAI |
1123 BGMAC_CMDCFG_HD |
1124 BGMAC_CMDCFG_ML |
1125 BGMAC_CMDCFG_CFE |
1126 BGMAC_CMDCFG_RL |
1127 BGMAC_CMDCFG_RED |
1128 BGMAC_CMDCFG_PE |
1129 BGMAC_CMDCFG_TPI |
1130 BGMAC_CMDCFG_PAD_EN |
1131 BGMAC_CMDCFG_PF),
1132 BGMAC_CMDCFG_PROM |
1133 BGMAC_CMDCFG_NLC |
1134 BGMAC_CMDCFG_CFE |
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001135 BGMAC_CMDCFG_SR(core->id.rev),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001136 false);
Rafał Miłeckid4699622013-12-11 07:44:14 +01001137 bgmac->mac_speed = SPEED_UNKNOWN;
1138 bgmac->mac_duplex = DUPLEX_UNKNOWN;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001139
1140 bgmac_clear_mib(bgmac);
1141 if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
1142 bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
1143 BCMA_GMAC_CMN_PC_MTE);
1144 else
1145 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1146 bgmac_miiconfig(bgmac);
1147 bgmac_phy_init(bgmac);
1148
Hauke Mehrtens49a467b2013-09-29 13:54:58 +02001149 netdev_reset_queue(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001150}
1151
1152static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1153{
1154 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1155}
1156
1157static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1158{
1159 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
Nathan Hintz41608152013-02-13 19:14:10 +00001160 bgmac_read(bgmac, BGMAC_INT_MASK);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001161}
1162
1163/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1164static void bgmac_enable(struct bgmac *bgmac)
1165{
1166 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
1167 u32 cmdcfg;
1168 u32 mode;
1169 u32 rxq_ctl;
1170 u32 fl_ctl;
1171 u16 bp_clk;
1172 u8 mdp;
1173
1174 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
1175 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001176 BGMAC_CMDCFG_SR(bgmac->core->id.rev), true);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001177 udelay(2);
1178 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1179 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1180
1181 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1182 BGMAC_DS_MM_SHIFT;
1183 if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
1184 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1185 if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
1186 bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
1187 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1188
1189 switch (ci->id) {
1190 case BCMA_CHIP_ID_BCM5357:
1191 case BCMA_CHIP_ID_BCM4749:
1192 case BCMA_CHIP_ID_BCM53572:
1193 case BCMA_CHIP_ID_BCM4716:
1194 case BCMA_CHIP_ID_BCM47162:
1195 fl_ctl = 0x03cb04cb;
1196 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1197 ci->id == BCMA_CHIP_ID_BCM4749 ||
1198 ci->id == BCMA_CHIP_ID_BCM53572)
1199 fl_ctl = 0x2300e1;
1200 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1201 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
1202 break;
1203 }
1204
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001205 if (!bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001206 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1207 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1208 bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) /
1209 1000000;
1210 mdp = (bp_clk * 128 / 1000) - 3;
1211 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1212 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1213 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001214}
1215
1216/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001217static void bgmac_chip_init(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001218{
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001219 /* 1 interrupt per received frame */
1220 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1221
1222 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1223 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1224
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001225 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001226
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001227 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001228
1229 if (bgmac->loopback)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001230 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001231 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001232 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001233
1234 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1235
Felix Fietkau74b6f292015-04-14 12:08:00 +02001236 bgmac_chip_intrs_on(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001237
1238 bgmac_enable(bgmac);
1239}
1240
1241static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1242{
1243 struct bgmac *bgmac = netdev_priv(dev_id);
1244
1245 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1246 int_status &= bgmac->int_mask;
1247
1248 if (!int_status)
1249 return IRQ_NONE;
1250
Felix Fietkaueb64e292015-04-14 12:07:55 +02001251 int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
1252 if (int_status)
1253 bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", int_status);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001254
1255 /* Disable new interrupts until handling existing ones */
1256 bgmac_chip_intrs_off(bgmac);
1257
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001258 napi_schedule(&bgmac->napi);
1259
1260 return IRQ_HANDLED;
1261}
1262
1263static int bgmac_poll(struct napi_struct *napi, int weight)
1264{
1265 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001266 int handled = 0;
1267
Felix Fietkaueb64e292015-04-14 12:07:55 +02001268 /* Ack */
1269 bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001270
Felix Fietkaueb64e292015-04-14 12:07:55 +02001271 bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
1272 handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001273
Felix Fietkaueb64e292015-04-14 12:07:55 +02001274 /* Poll again if more events arrived in the meantime */
1275 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
Rafał Miłeckie5802672015-04-23 20:56:29 +02001276 return weight;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001277
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001278 if (handled < weight) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001279 napi_complete(napi);
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001280 bgmac_chip_intrs_on(bgmac);
1281 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001282
1283 return handled;
1284}
1285
1286/**************************************************
1287 * net_device_ops
1288 **************************************************/
1289
1290static int bgmac_open(struct net_device *net_dev)
1291{
1292 struct bgmac *bgmac = netdev_priv(net_dev);
1293 int err = 0;
1294
1295 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001296
1297 err = bgmac_dma_init(bgmac);
1298 if (err)
1299 return err;
1300
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001301 /* Specs say about reclaiming rings here, but we do that in DMA init */
Felix Fietkau74b6f292015-04-14 12:08:00 +02001302 bgmac_chip_init(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001303
1304 err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
1305 KBUILD_MODNAME, net_dev);
1306 if (err < 0) {
1307 bgmac_err(bgmac, "IRQ request error: %d!\n", err);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001308 bgmac_dma_cleanup(bgmac);
1309 return err;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001310 }
1311 napi_enable(&bgmac->napi);
1312
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001313 phy_start(bgmac->phy_dev);
1314
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001315 netif_carrier_on(net_dev);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001316 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001317}
1318
1319static int bgmac_stop(struct net_device *net_dev)
1320{
1321 struct bgmac *bgmac = netdev_priv(net_dev);
1322
1323 netif_carrier_off(net_dev);
1324
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001325 phy_stop(bgmac->phy_dev);
1326
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001327 napi_disable(&bgmac->napi);
1328 bgmac_chip_intrs_off(bgmac);
1329 free_irq(bgmac->core->irq, net_dev);
1330
1331 bgmac_chip_reset(bgmac);
Felix Fietkau74b6f292015-04-14 12:08:00 +02001332 bgmac_dma_cleanup(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001333
1334 return 0;
1335}
1336
1337static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1338 struct net_device *net_dev)
1339{
1340 struct bgmac *bgmac = netdev_priv(net_dev);
1341 struct bgmac_dma_ring *ring;
1342
1343 /* No QOS support yet */
1344 ring = &bgmac->tx_ring[0];
1345 return bgmac_dma_tx_add(bgmac, ring, skb);
1346}
1347
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001348static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1349{
1350 struct bgmac *bgmac = netdev_priv(net_dev);
1351 int ret;
1352
1353 ret = eth_prepare_mac_addr_change(net_dev, addr);
1354 if (ret < 0)
1355 return ret;
1356 bgmac_write_mac_address(bgmac, (u8 *)addr);
1357 eth_commit_mac_addr_change(net_dev, addr);
1358 return 0;
1359}
1360
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001361static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1362{
1363 struct bgmac *bgmac = netdev_priv(net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001364
Hauke Mehrtens69c58852013-12-20 15:34:45 +01001365 if (!netif_running(net_dev))
1366 return -EINVAL;
1367
1368 return phy_mii_ioctl(bgmac->phy_dev, ifr, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001369}
1370
1371static const struct net_device_ops bgmac_netdev_ops = {
1372 .ndo_open = bgmac_open,
1373 .ndo_stop = bgmac_stop,
1374 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001375 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001376 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001377 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001378 .ndo_do_ioctl = bgmac_ioctl,
1379};
1380
1381/**************************************************
1382 * ethtool_ops
1383 **************************************************/
1384
Florian Fainellif6613d42016-06-07 15:06:14 -07001385struct bgmac_stat {
1386 u8 size;
1387 u32 offset;
1388 const char *name;
1389};
1390
1391static struct bgmac_stat bgmac_get_strings_stats[] = {
1392 { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
1393 { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
1394 { 8, BGMAC_TX_OCTETS, "tx_octets" },
1395 { 4, BGMAC_TX_PKTS, "tx_pkts" },
1396 { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
1397 { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
1398 { 4, BGMAC_TX_LEN_64, "tx_64" },
1399 { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
1400 { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
1401 { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
1402 { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
1403 { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
1404 { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
1405 { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
1406 { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
1407 { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
1408 { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
1409 { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
1410 { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
1411 { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
1412 { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
1413 { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
1414 { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
1415 { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
1416 { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
1417 { 4, BGMAC_TX_DEFERED, "tx_defered" },
1418 { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
1419 { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
1420 { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
1421 { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
1422 { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
1423 { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
1424 { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
1425 { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
1426 { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
1427 { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
1428 { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
1429 { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
1430 { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
1431 { 8, BGMAC_RX_OCTETS, "rx_octets" },
1432 { 4, BGMAC_RX_PKTS, "rx_pkts" },
1433 { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
1434 { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
1435 { 4, BGMAC_RX_LEN_64, "rx_64" },
1436 { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
1437 { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
1438 { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
1439 { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
1440 { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
1441 { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
1442 { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
1443 { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
1444 { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
1445 { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
1446 { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
1447 { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
1448 { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
1449 { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
1450 { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
1451 { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
1452 { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
1453 { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
1454 { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
1455 { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
1456 { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
1457 { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
1458};
1459
1460#define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
1461
1462static int bgmac_get_sset_count(struct net_device *dev, int string_set)
1463{
1464 switch (string_set) {
1465 case ETH_SS_STATS:
1466 return BGMAC_STATS_LEN;
1467 }
1468
1469 return -EOPNOTSUPP;
1470}
1471
1472static void bgmac_get_strings(struct net_device *dev, u32 stringset,
1473 u8 *data)
1474{
1475 int i;
1476
1477 if (stringset != ETH_SS_STATS)
1478 return;
1479
1480 for (i = 0; i < BGMAC_STATS_LEN; i++)
1481 strlcpy(data + i * ETH_GSTRING_LEN,
1482 bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
1483}
1484
1485static void bgmac_get_ethtool_stats(struct net_device *dev,
1486 struct ethtool_stats *ss, uint64_t *data)
1487{
1488 struct bgmac *bgmac = netdev_priv(dev);
1489 const struct bgmac_stat *s;
1490 unsigned int i;
1491 u64 val;
1492
1493 if (!netif_running(dev))
1494 return;
1495
1496 for (i = 0; i < BGMAC_STATS_LEN; i++) {
1497 s = &bgmac_get_strings_stats[i];
1498 val = 0;
1499 if (s->size == 8)
1500 val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
1501 val |= bgmac_read(bgmac, s->offset);
1502 data[i] = val;
1503 }
1504}
1505
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001506static int bgmac_get_settings(struct net_device *net_dev,
1507 struct ethtool_cmd *cmd)
1508{
1509 struct bgmac *bgmac = netdev_priv(net_dev);
1510
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001511 return phy_ethtool_gset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001512}
1513
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001514static int bgmac_set_settings(struct net_device *net_dev,
1515 struct ethtool_cmd *cmd)
1516{
1517 struct bgmac *bgmac = netdev_priv(net_dev);
1518
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001519 return phy_ethtool_sset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001520}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001521
1522static void bgmac_get_drvinfo(struct net_device *net_dev,
1523 struct ethtool_drvinfo *info)
1524{
1525 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1526 strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
1527}
1528
1529static const struct ethtool_ops bgmac_ethtool_ops = {
Florian Fainellif6613d42016-06-07 15:06:14 -07001530 .get_strings = bgmac_get_strings,
1531 .get_sset_count = bgmac_get_sset_count,
1532 .get_ethtool_stats = bgmac_get_ethtool_stats,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001533 .get_settings = bgmac_get_settings,
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001534 .set_settings = bgmac_set_settings,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001535 .get_drvinfo = bgmac_get_drvinfo,
1536};
1537
1538/**************************************************
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001539 * MII
1540 **************************************************/
1541
1542static int bgmac_mii_read(struct mii_bus *bus, int mii_id, int regnum)
1543{
1544 return bgmac_phy_read(bus->priv, mii_id, regnum);
1545}
1546
1547static int bgmac_mii_write(struct mii_bus *bus, int mii_id, int regnum,
1548 u16 value)
1549{
1550 return bgmac_phy_write(bus->priv, mii_id, regnum, value);
1551}
1552
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001553static void bgmac_adjust_link(struct net_device *net_dev)
1554{
1555 struct bgmac *bgmac = netdev_priv(net_dev);
1556 struct phy_device *phy_dev = bgmac->phy_dev;
1557 bool update = false;
1558
1559 if (phy_dev->link) {
1560 if (phy_dev->speed != bgmac->mac_speed) {
1561 bgmac->mac_speed = phy_dev->speed;
1562 update = true;
1563 }
1564
1565 if (phy_dev->duplex != bgmac->mac_duplex) {
1566 bgmac->mac_duplex = phy_dev->duplex;
1567 update = true;
1568 }
1569 }
1570
1571 if (update) {
1572 bgmac_mac_speed(bgmac);
1573 phy_print_status(phy_dev);
1574 }
1575}
1576
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001577static int bgmac_fixed_phy_register(struct bgmac *bgmac)
1578{
1579 struct fixed_phy_status fphy_status = {
1580 .link = 1,
1581 .speed = SPEED_1000,
1582 .duplex = DUPLEX_FULL,
1583 };
1584 struct phy_device *phy_dev;
1585 int err;
1586
Fabio Estevam4db78d32015-09-02 13:25:59 -03001587 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001588 if (!phy_dev || IS_ERR(phy_dev)) {
1589 bgmac_err(bgmac, "Failed to register fixed PHY device\n");
1590 return -ENODEV;
1591 }
1592
1593 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1594 PHY_INTERFACE_MODE_MII);
1595 if (err) {
1596 bgmac_err(bgmac, "Connecting PHY failed\n");
1597 return err;
1598 }
1599
1600 bgmac->phy_dev = phy_dev;
1601
1602 return err;
1603}
1604
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001605static int bgmac_mii_register(struct bgmac *bgmac)
1606{
1607 struct mii_bus *mii_bus;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001608 struct phy_device *phy_dev;
1609 char bus_id[MII_BUS_ID_SIZE + 3];
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001610 int err = 0;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001611
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001612 if (bgmac_is_bcm4707_family(bgmac))
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001613 return bgmac_fixed_phy_register(bgmac);
1614
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001615 mii_bus = mdiobus_alloc();
1616 if (!mii_bus)
1617 return -ENOMEM;
1618
1619 mii_bus->name = "bgmac mii bus";
1620 sprintf(mii_bus->id, "%s-%d-%d", "bgmac", bgmac->core->bus->num,
1621 bgmac->core->core_unit);
1622 mii_bus->priv = bgmac;
1623 mii_bus->read = bgmac_mii_read;
1624 mii_bus->write = bgmac_mii_write;
1625 mii_bus->parent = &bgmac->core->dev;
1626 mii_bus->phy_mask = ~(1 << bgmac->phyaddr);
1627
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001628 err = mdiobus_register(mii_bus);
1629 if (err) {
1630 bgmac_err(bgmac, "Registration of mii bus failed\n");
Andrew Lunne7f4dc32016-01-06 20:11:15 +01001631 goto err_free_bus;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001632 }
1633
1634 bgmac->mii_bus = mii_bus;
1635
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001636 /* Connect to the PHY */
1637 snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
1638 bgmac->phyaddr);
1639 phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
1640 PHY_INTERFACE_MODE_MII);
1641 if (IS_ERR(phy_dev)) {
Masanari Iidac01e0152016-04-20 00:27:33 +09001642 bgmac_err(bgmac, "PHY connection failed\n");
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001643 err = PTR_ERR(phy_dev);
1644 goto err_unregister_bus;
1645 }
1646 bgmac->phy_dev = phy_dev;
1647
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001648 return err;
1649
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001650err_unregister_bus:
1651 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001652err_free_bus:
1653 mdiobus_free(mii_bus);
1654 return err;
1655}
1656
1657static void bgmac_mii_unregister(struct bgmac *bgmac)
1658{
1659 struct mii_bus *mii_bus = bgmac->mii_bus;
1660
1661 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001662 mdiobus_free(mii_bus);
1663}
1664
1665/**************************************************
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001666 * BCMA bus ops
1667 **************************************************/
1668
1669/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
1670static int bgmac_probe(struct bcma_device *core)
1671{
1672 struct net_device *net_dev;
1673 struct bgmac *bgmac;
1674 struct ssb_sprom *sprom = &core->bus->sprom;
Rafał Miłecki538e4562015-08-26 17:53:45 +02001675 u8 *mac;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001676 int err;
1677
Rafał Miłecki538e4562015-08-26 17:53:45 +02001678 switch (core->core_unit) {
1679 case 0:
1680 mac = sprom->et0mac;
1681 break;
1682 case 1:
1683 mac = sprom->et1mac;
1684 break;
1685 case 2:
1686 mac = sprom->et2mac;
1687 break;
1688 default:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001689 pr_err("Unsupported core_unit %d\n", core->core_unit);
1690 return -ENOTSUPP;
1691 }
1692
Rafał Miłeckid166f212013-02-07 00:27:17 +00001693 if (!is_valid_ether_addr(mac)) {
1694 dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
1695 eth_random_addr(mac);
1696 dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
1697 }
1698
Rafał Miłeckib4dfd8e2016-04-12 13:30:45 +02001699 /* This (reset &) enable is not preset in specs or reference driver but
1700 * Broadcom does it in arch PCI code when enabling fake PCI device.
1701 */
1702 bcma_core_enable(core, 0);
1703
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001704 /* Allocation and references */
1705 net_dev = alloc_etherdev(sizeof(*bgmac));
1706 if (!net_dev)
1707 return -ENOMEM;
1708 net_dev->netdev_ops = &bgmac_netdev_ops;
1709 net_dev->irq = core->irq;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001710 net_dev->ethtool_ops = &bgmac_ethtool_ops;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001711 bgmac = netdev_priv(net_dev);
1712 bgmac->net_dev = net_dev;
1713 bgmac->core = core;
1714 bcma_set_drvdata(core, bgmac);
Florian Fainelli2022e9d2016-06-07 15:06:13 -07001715 SET_NETDEV_DEV(net_dev, &core->dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001716
1717 /* Defaults */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001718 memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
1719
1720 /* On BCM4706 we need common core to access PHY */
1721 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
1722 !core->bus->drv_gmac_cmn.core) {
1723 bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
1724 err = -ENODEV;
1725 goto err_netdev_free;
1726 }
1727 bgmac->cmn = core->bus->drv_gmac_cmn.core;
1728
Rafał Miłecki538e4562015-08-26 17:53:45 +02001729 switch (core->core_unit) {
1730 case 0:
1731 bgmac->phyaddr = sprom->et0phyaddr;
1732 break;
1733 case 1:
1734 bgmac->phyaddr = sprom->et1phyaddr;
1735 break;
1736 case 2:
1737 bgmac->phyaddr = sprom->et2phyaddr;
1738 break;
1739 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001740 bgmac->phyaddr &= BGMAC_PHY_MASK;
1741 if (bgmac->phyaddr == BGMAC_PHY_MASK) {
1742 bgmac_err(bgmac, "No PHY found\n");
1743 err = -ENODEV;
1744 goto err_netdev_free;
1745 }
1746 bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
1747 bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
1748
1749 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
1750 bgmac_err(bgmac, "PCI setup not implemented\n");
1751 err = -ENOTSUPP;
1752 goto err_netdev_free;
1753 }
1754
1755 bgmac_chip_reset(bgmac);
1756
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001757 /* For Northstar, we have to take all GMAC core out of reset */
Rafał Miłecki387b75f2016-02-02 07:47:14 +01001758 if (bgmac_is_bcm4707_family(bgmac)) {
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001759 struct bcma_device *ns_core;
1760 int ns_gmac;
1761
1762 /* Northstar has 4 GMAC cores */
1763 for (ns_gmac = 0; ns_gmac < 4; ns_gmac++) {
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001764 /* As Northstar requirement, we have to reset all GMACs
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001765 * before accessing one. bgmac_chip_reset() call
1766 * bcma_core_enable() for this core. Then the other
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001767 * three GMACs didn't reset. We do it here.
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001768 */
1769 ns_core = bcma_find_core_unit(core->bus,
1770 BCMA_CORE_MAC_GBIT,
1771 ns_gmac);
1772 if (ns_core && !bcma_core_is_enabled(ns_core))
1773 bcma_core_enable(ns_core, 0);
1774 }
1775 }
1776
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001777 err = bgmac_dma_alloc(bgmac);
1778 if (err) {
1779 bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
1780 goto err_netdev_free;
1781 }
1782
1783 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
Ralf Baechleedb15d82013-02-21 16:16:55 +01001784 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001785 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1786
1787 /* TODO: reset the external phy. Specs are needed */
1788 bgmac_phy_reset(bgmac);
1789
1790 bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
1791 BGMAC_BFL_ENETROBO);
1792 if (bgmac->has_robosw)
1793 bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
1794
1795 if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
1796 bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
1797
Hauke Mehrtens62166422015-01-18 19:49:58 +01001798 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1799
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001800 err = bgmac_mii_register(bgmac);
1801 if (err) {
1802 bgmac_err(bgmac, "Cannot register MDIO\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001803 goto err_dma_free;
1804 }
1805
Felix Fietkau9cde9452015-03-23 12:35:37 +01001806 net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1807 net_dev->hw_features = net_dev->features;
1808 net_dev->vlan_features = net_dev->features;
1809
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001810 err = register_netdev(bgmac->net_dev);
1811 if (err) {
1812 bgmac_err(bgmac, "Cannot register net device\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001813 goto err_mii_unregister;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001814 }
1815
1816 netif_carrier_off(net_dev);
1817
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001818 return 0;
1819
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001820err_mii_unregister:
1821 bgmac_mii_unregister(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001822err_dma_free:
1823 bgmac_dma_free(bgmac);
1824
1825err_netdev_free:
1826 bcma_set_drvdata(core, NULL);
1827 free_netdev(net_dev);
1828
1829 return err;
1830}
1831
1832static void bgmac_remove(struct bcma_device *core)
1833{
1834 struct bgmac *bgmac = bcma_get_drvdata(core);
1835
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001836 unregister_netdev(bgmac->net_dev);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001837 bgmac_mii_unregister(bgmac);
Hauke Mehrtens62166422015-01-18 19:49:58 +01001838 netif_napi_del(&bgmac->napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001839 bgmac_dma_free(bgmac);
1840 bcma_set_drvdata(core, NULL);
1841 free_netdev(bgmac->net_dev);
1842}
1843
1844static struct bcma_driver bgmac_bcma_driver = {
1845 .name = KBUILD_MODNAME,
1846 .id_table = bgmac_bcma_tbl,
1847 .probe = bgmac_probe,
1848 .remove = bgmac_remove,
1849};
1850
1851static int __init bgmac_init(void)
1852{
1853 int err;
1854
1855 err = bcma_driver_register(&bgmac_bcma_driver);
1856 if (err)
1857 return err;
1858 pr_info("Broadcom 47xx GBit MAC driver loaded\n");
1859
1860 return 0;
1861}
1862
1863static void __exit bgmac_exit(void)
1864{
1865 bcma_driver_unregister(&bgmac_bcma_driver);
1866}
1867
1868module_init(bgmac_init)
1869module_exit(bgmac_exit)
1870
1871MODULE_AUTHOR("Rafał Miłecki");
1872MODULE_LICENSE("GPL");