blob: c7da37aef03517abea0cd3caaaee59b015196df7 [file] [log] [blame]
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001/*
2 * Driver for (BCM4706)? GBit MAC core on BCMA bus.
3 *
4 * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
5 *
6 * Licensed under the GNU/GPL. See COPYING for details.
7 */
8
9#include "bgmac.h"
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/etherdevice.h>
15#include <linux/mii.h>
Rafał Miłecki11e5e762013-03-07 01:53:28 +000016#include <linux/phy.h>
Rafał Miłeckic25b23b2015-03-20 23:14:31 +010017#include <linux/phy_fixed.h>
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000018#include <linux/interrupt.h>
19#include <linux/dma-mapping.h>
Ralf Baechleedb15d82013-02-21 16:16:55 +010020#include <bcm47xx_nvram.h>
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000021
22static const struct bcma_device_id bgmac_bcma_tbl[] = {
23 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_4706_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
24 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
Joe Perchesf7219b52015-02-10 12:55:03 -080025 {},
Rafał Miłeckidd4544f2013-01-08 20:06:23 +000026};
27MODULE_DEVICE_TABLE(bcma, bgmac_bcma_tbl);
28
29static bool bgmac_wait_value(struct bcma_device *core, u16 reg, u32 mask,
30 u32 value, int timeout)
31{
32 u32 val;
33 int i;
34
35 for (i = 0; i < timeout / 10; i++) {
36 val = bcma_read32(core, reg);
37 if ((val & mask) == value)
38 return true;
39 udelay(10);
40 }
41 pr_err("Timeout waiting for reg 0x%X\n", reg);
42 return false;
43}
44
45/**************************************************
46 * DMA
47 **************************************************/
48
49static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
50{
51 u32 val;
52 int i;
53
54 if (!ring->mmio_base)
55 return;
56
57 /* Suspend DMA TX ring first.
58 * bgmac_wait_value doesn't support waiting for any of few values, so
59 * implement whole loop here.
60 */
61 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
62 BGMAC_DMA_TX_SUSPEND);
63 for (i = 0; i < 10000 / 10; i++) {
64 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
65 val &= BGMAC_DMA_TX_STAT;
66 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
67 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
68 val == BGMAC_DMA_TX_STAT_STOPPED) {
69 i = 0;
70 break;
71 }
72 udelay(10);
73 }
74 if (i)
75 bgmac_err(bgmac, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
76 ring->mmio_base, val);
77
78 /* Remove SUSPEND bit */
79 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
80 if (!bgmac_wait_value(bgmac->core,
81 ring->mmio_base + BGMAC_DMA_TX_STATUS,
82 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
83 10000)) {
84 bgmac_warn(bgmac, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
85 ring->mmio_base);
86 udelay(300);
87 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
88 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
89 bgmac_err(bgmac, "Reset of DMA TX ring 0x%X failed\n",
90 ring->mmio_base);
91 }
92}
93
94static void bgmac_dma_tx_enable(struct bgmac *bgmac,
95 struct bgmac_dma_ring *ring)
96{
97 u32 ctl;
98
99 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100100 if (bgmac->core->id.rev >= 4) {
101 ctl &= ~BGMAC_DMA_TX_BL_MASK;
102 ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
103
104 ctl &= ~BGMAC_DMA_TX_MR_MASK;
105 ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
106
107 ctl &= ~BGMAC_DMA_TX_PC_MASK;
108 ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
109
110 ctl &= ~BGMAC_DMA_TX_PT_MASK;
111 ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
112 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000113 ctl |= BGMAC_DMA_TX_ENABLE;
114 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
115 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
116}
117
118static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
119 struct bgmac_dma_ring *ring,
120 struct sk_buff *skb)
121{
122 struct device *dma_dev = bgmac->core->dma_dev;
123 struct net_device *net_dev = bgmac->net_dev;
124 struct bgmac_dma_desc *dma_desc;
125 struct bgmac_slot_info *slot;
126 u32 ctl0, ctl1;
127 int free_slots;
128
129 if (skb->len > BGMAC_DESC_CTL1_LEN) {
130 bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
131 goto err_stop_drop;
132 }
133
134 if (ring->start <= ring->end)
135 free_slots = ring->start - ring->end + BGMAC_TX_RING_SLOTS;
136 else
137 free_slots = ring->start - ring->end;
138 if (free_slots == 1) {
139 bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
140 netif_stop_queue(net_dev);
141 return NETDEV_TX_BUSY;
142 }
143
144 slot = &ring->slots[ring->end];
145 slot->skb = skb;
146 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb->len,
147 DMA_TO_DEVICE);
148 if (dma_mapping_error(dma_dev, slot->dma_addr)) {
149 bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n",
150 ring->mmio_base);
151 goto err_stop_drop;
152 }
153
154 ctl0 = BGMAC_DESC_CTL0_IOC | BGMAC_DESC_CTL0_SOF | BGMAC_DESC_CTL0_EOF;
155 if (ring->end == ring->num_slots - 1)
156 ctl0 |= BGMAC_DESC_CTL0_EOT;
157 ctl1 = skb->len & BGMAC_DESC_CTL1_LEN;
158
159 dma_desc = ring->cpu_base;
160 dma_desc += ring->end;
161 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
162 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
163 dma_desc->ctl0 = cpu_to_le32(ctl0);
164 dma_desc->ctl1 = cpu_to_le32(ctl1);
165
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200166 netdev_sent_queue(net_dev, skb->len);
167
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000168 wmb();
169
170 /* Increase ring->end to point empty slot. We tell hardware the first
171 * slot it should *not* read.
172 */
173 if (++ring->end >= BGMAC_TX_RING_SLOTS)
174 ring->end = 0;
175 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
Rafał Miłecki99003032013-09-15 23:13:18 +0200176 ring->index_base +
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000177 ring->end * sizeof(struct bgmac_dma_desc));
178
179 /* Always keep one slot free to allow detecting bugged calls. */
180 if (--free_slots == 1)
181 netif_stop_queue(net_dev);
182
183 return NETDEV_TX_OK;
184
185err_stop_drop:
186 netif_stop_queue(net_dev);
187 dev_kfree_skb(skb);
188 return NETDEV_TX_OK;
189}
190
191/* Free transmitted packets */
192static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
193{
194 struct device *dma_dev = bgmac->core->dma_dev;
195 int empty_slot;
196 bool freed = false;
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200197 unsigned bytes_compl = 0, pkts_compl = 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000198
199 /* The last slot that hardware didn't consume yet */
200 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
201 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200202 empty_slot -= ring->index_base;
203 empty_slot &= BGMAC_DMA_TX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000204 empty_slot /= sizeof(struct bgmac_dma_desc);
205
206 while (ring->start != empty_slot) {
207 struct bgmac_slot_info *slot = &ring->slots[ring->start];
208
209 if (slot->skb) {
210 /* Unmap no longer used buffer */
211 dma_unmap_single(dma_dev, slot->dma_addr,
212 slot->skb->len, DMA_TO_DEVICE);
213 slot->dma_addr = 0;
214
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200215 bytes_compl += slot->skb->len;
216 pkts_compl++;
217
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000218 /* Free memory! :) */
219 dev_kfree_skb(slot->skb);
220 slot->skb = NULL;
221 } else {
222 bgmac_err(bgmac, "Hardware reported transmission for empty TX ring slot %d! End of ring: %d\n",
223 ring->start, ring->end);
224 }
225
226 if (++ring->start >= BGMAC_TX_RING_SLOTS)
227 ring->start = 0;
228 freed = true;
229 }
230
Hauke Mehrtens49a467b2013-09-29 13:54:58 +0200231 netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
232
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000233 if (freed && netif_queue_stopped(bgmac->net_dev))
234 netif_wake_queue(bgmac->net_dev);
235}
236
237static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
238{
239 if (!ring->mmio_base)
240 return;
241
242 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
243 if (!bgmac_wait_value(bgmac->core,
244 ring->mmio_base + BGMAC_DMA_RX_STATUS,
245 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
246 10000))
247 bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
248 ring->mmio_base);
249}
250
251static void bgmac_dma_rx_enable(struct bgmac *bgmac,
252 struct bgmac_dma_ring *ring)
253{
254 u32 ctl;
255
256 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
Hauke Mehrtens56ceecd2014-01-05 01:10:44 +0100257 if (bgmac->core->id.rev >= 4) {
258 ctl &= ~BGMAC_DMA_RX_BL_MASK;
259 ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
260
261 ctl &= ~BGMAC_DMA_RX_PC_MASK;
262 ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
263
264 ctl &= ~BGMAC_DMA_RX_PT_MASK;
265 ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
266 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000267 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
268 ctl |= BGMAC_DMA_RX_ENABLE;
269 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
270 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
271 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
272 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
273}
274
275static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
276 struct bgmac_slot_info *slot)
277{
278 struct device *dma_dev = bgmac->core->dma_dev;
Nathan Hintzb757a622013-10-29 19:32:01 -0700279 dma_addr_t dma_addr;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000280 struct bgmac_rx_header *rx;
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100281 void *buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000282
283 /* Alloc skb */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100284 buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
285 if (!buf)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000286 return -ENOMEM;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000287
288 /* Poison - if everything goes fine, hardware will overwrite it */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100289 rx = buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000290 rx->len = cpu_to_le16(0xdead);
291 rx->flags = cpu_to_le16(0xbeef);
292
293 /* Map skb for the DMA */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100294 dma_addr = dma_map_single(dma_dev, buf, BGMAC_RX_BUF_SIZE,
295 DMA_FROM_DEVICE);
Nathan Hintzb757a622013-10-29 19:32:01 -0700296 if (dma_mapping_error(dma_dev, dma_addr)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000297 bgmac_err(bgmac, "DMA mapping error\n");
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100298 put_page(virt_to_head_page(buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000299 return -ENOMEM;
300 }
Nathan Hintzb757a622013-10-29 19:32:01 -0700301
302 /* Update the slot */
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100303 slot->buf = buf;
Nathan Hintzb757a622013-10-29 19:32:01 -0700304 slot->dma_addr = dma_addr;
305
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000306 return 0;
307}
308
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100309static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
310 struct bgmac_dma_ring *ring, int desc_idx)
311{
312 struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
313 u32 ctl0 = 0, ctl1 = 0;
314
315 if (desc_idx == ring->num_slots - 1)
316 ctl0 |= BGMAC_DESC_CTL0_EOT;
317 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
318 /* Is there any BGMAC device that requires extension? */
319 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
320 * B43_DMA64_DCTL1_ADDREXT_MASK;
321 */
322
323 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
324 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
325 dma_desc->ctl0 = cpu_to_le32(ctl0);
326 dma_desc->ctl1 = cpu_to_le32(ctl1);
327}
328
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000329static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
330 int weight)
331{
332 u32 end_slot;
333 int handled = 0;
334
335 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
336 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłecki99003032013-09-15 23:13:18 +0200337 end_slot -= ring->index_base;
338 end_slot &= BGMAC_DMA_RX_STATDPTR;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000339 end_slot /= sizeof(struct bgmac_dma_desc);
340
341 ring->end = end_slot;
342
343 while (ring->start != ring->end) {
344 struct device *dma_dev = bgmac->core->dma_dev;
345 struct bgmac_slot_info *slot = &ring->slots[ring->start];
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100346 struct bgmac_rx_header *rx = slot->buf;
347 struct sk_buff *skb;
348 void *buf = slot->buf;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000349 u16 len, flags;
350
351 /* Unmap buffer to make it accessible to the CPU */
352 dma_sync_single_for_cpu(dma_dev, slot->dma_addr,
353 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
354
355 /* Get info from the header */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000356 len = le16_to_cpu(rx->len);
357 flags = le16_to_cpu(rx->flags);
358
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100359 do {
360 dma_addr_t old_dma_addr = slot->dma_addr;
361 int err;
362
363 /* Check for poison and drop or pass the packet */
364 if (len == 0xdead && flags == 0xbeef) {
365 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
366 ring->start);
367 dma_sync_single_for_device(dma_dev,
368 slot->dma_addr,
369 BGMAC_RX_BUF_SIZE,
370 DMA_FROM_DEVICE);
371 break;
372 }
373
Hauke Mehrtens02e71122013-02-28 07:16:54 +0000374 /* Omit CRC. */
375 len -= ETH_FCS_LEN;
376
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100377 /* Prepare new skb as replacement */
378 err = bgmac_dma_rx_skb_for_slot(bgmac, slot);
379 if (err) {
380 /* Poison the old skb */
381 rx->len = cpu_to_le16(0xdead);
382 rx->flags = cpu_to_le16(0xbeef);
383
384 dma_sync_single_for_device(dma_dev,
385 slot->dma_addr,
386 BGMAC_RX_BUF_SIZE,
387 DMA_FROM_DEVICE);
388 break;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000389 }
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100390 bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000391
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100392 /* Unmap old skb, we'll pass it to the netfif */
393 dma_unmap_single(dma_dev, old_dma_addr,
394 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000395
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100396 skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100397 skb_put(skb, BGMAC_RX_FRAME_OFFSET + len);
398 skb_pull(skb, BGMAC_RX_FRAME_OFFSET);
399
400 skb_checksum_none_assert(skb);
401 skb->protocol = eth_type_trans(skb, bgmac->net_dev);
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100402 napi_gro_receive(&bgmac->napi, skb);
Rafał Miłecki92b9ccd32013-10-30 08:00:00 +0100403 handled++;
404 } while (0);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000405
406 if (++ring->start >= BGMAC_RX_RING_SLOTS)
407 ring->start = 0;
408
409 if (handled >= weight) /* Should never be greater */
410 break;
411 }
412
413 return handled;
414}
415
416/* Does ring support unaligned addressing? */
417static bool bgmac_dma_unaligned(struct bgmac *bgmac,
418 struct bgmac_dma_ring *ring,
419 enum bgmac_dma_ring_type ring_type)
420{
421 switch (ring_type) {
422 case BGMAC_DMA_RING_TX:
423 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
424 0xff0);
425 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
426 return true;
427 break;
428 case BGMAC_DMA_RING_RX:
429 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
430 0xff0);
431 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
432 return true;
433 break;
434 }
435 return false;
436}
437
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100438static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
439 struct bgmac_dma_ring *ring)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000440{
441 struct device *dma_dev = bgmac->core->dma_dev;
442 struct bgmac_slot_info *slot;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000443 int i;
444
445 for (i = 0; i < ring->num_slots; i++) {
446 slot = &ring->slots[i];
447 if (slot->skb) {
448 if (slot->dma_addr)
449 dma_unmap_single(dma_dev, slot->dma_addr,
450 slot->skb->len, DMA_TO_DEVICE);
451 dev_kfree_skb(slot->skb);
452 }
453 }
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100454}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000455
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100456static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
457 struct bgmac_dma_ring *ring)
458{
459 struct device *dma_dev = bgmac->core->dma_dev;
460 struct bgmac_slot_info *slot;
461 int i;
462
463 for (i = 0; i < ring->num_slots; i++) {
464 slot = &ring->slots[i];
465 if (!slot->buf)
466 continue;
467
468 if (slot->dma_addr)
469 dma_unmap_single(dma_dev, slot->dma_addr,
470 BGMAC_RX_BUF_SIZE,
471 DMA_FROM_DEVICE);
472 put_page(virt_to_head_page(slot->buf));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000473 }
474}
475
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100476static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
477 struct bgmac_dma_ring *ring)
478{
479 struct device *dma_dev = bgmac->core->dma_dev;
480 int size;
481
482 if (!ring->cpu_base)
483 return;
484
485 /* Free ring of descriptors */
486 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
487 dma_free_coherent(dma_dev, size, ring->cpu_base,
488 ring->dma_base);
489}
490
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000491static void bgmac_dma_free(struct bgmac *bgmac)
492{
493 int i;
494
Felix Fietkau45c9b3c2015-03-23 12:35:36 +0100495 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
496 bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
497 bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i]);
498 }
499 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
500 bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
501 bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i]);
502 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000503}
504
505static int bgmac_dma_alloc(struct bgmac *bgmac)
506{
507 struct device *dma_dev = bgmac->core->dma_dev;
508 struct bgmac_dma_ring *ring;
509 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
510 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
511 int size; /* ring size: different for Tx and Rx */
512 int err;
513 int i;
514
515 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
516 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
517
518 if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
519 bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
520 return -ENOTSUPP;
521 }
522
523 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
524 ring = &bgmac->tx_ring[i];
525 ring->num_slots = BGMAC_TX_RING_SLOTS;
526 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000527
528 /* Alloc ring of descriptors */
529 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
530 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
531 &ring->dma_base,
532 GFP_KERNEL);
533 if (!ring->cpu_base) {
534 bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
535 ring->mmio_base);
536 goto err_dma_free;
537 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000538
Rafał Miłecki99003032013-09-15 23:13:18 +0200539 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
540 BGMAC_DMA_RING_TX);
541 if (ring->unaligned)
542 ring->index_base = lower_32_bits(ring->dma_base);
543 else
544 ring->index_base = 0;
545
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000546 /* No need to alloc TX slots yet */
547 }
548
549 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000550 int j;
551
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000552 ring = &bgmac->rx_ring[i];
553 ring->num_slots = BGMAC_RX_RING_SLOTS;
554 ring->mmio_base = ring_base[i];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000555
556 /* Alloc ring of descriptors */
557 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
558 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
559 &ring->dma_base,
560 GFP_KERNEL);
561 if (!ring->cpu_base) {
562 bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
563 ring->mmio_base);
564 err = -ENOMEM;
565 goto err_dma_free;
566 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000567
Rafał Miłecki99003032013-09-15 23:13:18 +0200568 ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
569 BGMAC_DMA_RING_RX);
570 if (ring->unaligned)
571 ring->index_base = lower_32_bits(ring->dma_base);
572 else
573 ring->index_base = 0;
574
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000575 /* Alloc RX slots */
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000576 for (j = 0; j < ring->num_slots; j++) {
577 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000578 if (err) {
579 bgmac_err(bgmac, "Can't allocate skb for slot in RX ring\n");
580 goto err_dma_free;
581 }
582 }
583 }
584
585 return 0;
586
587err_dma_free:
588 bgmac_dma_free(bgmac);
589 return -ENOMEM;
590}
591
592static void bgmac_dma_init(struct bgmac *bgmac)
593{
594 struct bgmac_dma_ring *ring;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000595 int i;
596
597 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
598 ring = &bgmac->tx_ring[i];
599
Rafał Miłecki99003032013-09-15 23:13:18 +0200600 if (!ring->unaligned)
601 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000602 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
603 lower_32_bits(ring->dma_base));
604 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
605 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200606 if (ring->unaligned)
607 bgmac_dma_tx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000608
609 ring->start = 0;
610 ring->end = 0; /* Points the slot that should *not* be read */
611 }
612
613 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
Rafał Miłecki70a737b2013-02-25 08:22:26 +0000614 int j;
615
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000616 ring = &bgmac->rx_ring[i];
617
Rafał Miłecki99003032013-09-15 23:13:18 +0200618 if (!ring->unaligned)
619 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000620 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
621 lower_32_bits(ring->dma_base));
622 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
623 upper_32_bits(ring->dma_base));
Rafał Miłecki99003032013-09-15 23:13:18 +0200624 if (ring->unaligned)
625 bgmac_dma_rx_enable(bgmac, ring);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000626
Rafał Miłeckid549c76b2013-10-28 14:40:29 +0100627 for (j = 0; j < ring->num_slots; j++)
628 bgmac_dma_rx_setup_desc(bgmac, ring, j);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000629
630 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
Rafał Miłecki99003032013-09-15 23:13:18 +0200631 ring->index_base +
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000632 ring->num_slots * sizeof(struct bgmac_dma_desc));
633
634 ring->start = 0;
635 ring->end = 0;
636 }
637}
638
639/**************************************************
640 * PHY ops
641 **************************************************/
642
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000643static u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000644{
645 struct bcma_device *core;
646 u16 phy_access_addr;
647 u16 phy_ctl_addr;
648 u32 tmp;
649
650 BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
651 BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
652 BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
653 BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
654 BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
655 BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
656 BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
657 BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
658 BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
659 BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
660 BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
661
662 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
663 core = bgmac->core->bus->drv_gmac_cmn.core;
664 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
665 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
666 } else {
667 core = bgmac->core;
668 phy_access_addr = BGMAC_PHY_ACCESS;
669 phy_ctl_addr = BGMAC_PHY_CNTL;
670 }
671
672 tmp = bcma_read32(core, phy_ctl_addr);
673 tmp &= ~BGMAC_PC_EPA_MASK;
674 tmp |= phyaddr;
675 bcma_write32(core, phy_ctl_addr, tmp);
676
677 tmp = BGMAC_PA_START;
678 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
679 tmp |= reg << BGMAC_PA_REG_SHIFT;
680 bcma_write32(core, phy_access_addr, tmp);
681
682 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
683 bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
684 phyaddr, reg);
685 return 0xffff;
686 }
687
688 return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
689}
690
691/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000692static int bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000693{
694 struct bcma_device *core;
695 u16 phy_access_addr;
696 u16 phy_ctl_addr;
697 u32 tmp;
698
699 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
700 core = bgmac->core->bus->drv_gmac_cmn.core;
701 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
702 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
703 } else {
704 core = bgmac->core;
705 phy_access_addr = BGMAC_PHY_ACCESS;
706 phy_ctl_addr = BGMAC_PHY_CNTL;
707 }
708
709 tmp = bcma_read32(core, phy_ctl_addr);
710 tmp &= ~BGMAC_PC_EPA_MASK;
711 tmp |= phyaddr;
712 bcma_write32(core, phy_ctl_addr, tmp);
713
714 bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
715 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
716 bgmac_warn(bgmac, "Error setting MDIO int\n");
717
718 tmp = BGMAC_PA_START;
719 tmp |= BGMAC_PA_WRITE;
720 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
721 tmp |= reg << BGMAC_PA_REG_SHIFT;
722 tmp |= value;
723 bcma_write32(core, phy_access_addr, tmp);
724
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000725 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000726 bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
727 phyaddr, reg);
Rafał Miłecki217a55a2013-02-12 23:14:51 +0000728 return -ETIMEDOUT;
729 }
730
731 return 0;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000732}
733
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000734/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
735static void bgmac_phy_init(struct bgmac *bgmac)
736{
737 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
738 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
739 u8 i;
740
741 if (ci->id == BCMA_CHIP_ID_BCM5356) {
742 for (i = 0; i < 5; i++) {
743 bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
744 bgmac_phy_write(bgmac, i, 0x15, 0x0100);
745 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
746 bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
747 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
748 }
749 }
750 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
751 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
752 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
753 bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
754 bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
755 for (i = 0; i < 5; i++) {
756 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
757 bgmac_phy_write(bgmac, i, 0x16, 0x5284);
758 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
759 bgmac_phy_write(bgmac, i, 0x17, 0x0010);
760 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
761 bgmac_phy_write(bgmac, i, 0x16, 0x5296);
762 bgmac_phy_write(bgmac, i, 0x17, 0x1073);
763 bgmac_phy_write(bgmac, i, 0x17, 0x9073);
764 bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
765 bgmac_phy_write(bgmac, i, 0x17, 0x9273);
766 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
767 }
768 }
769}
770
771/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
772static void bgmac_phy_reset(struct bgmac *bgmac)
773{
774 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
775 return;
776
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100777 bgmac_phy_write(bgmac, bgmac->phyaddr, MII_BMCR, BMCR_RESET);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000778 udelay(100);
Rafał Miłecki5322dbf2013-12-20 15:33:52 +0100779 if (bgmac_phy_read(bgmac, bgmac->phyaddr, MII_BMCR) & BMCR_RESET)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000780 bgmac_err(bgmac, "PHY reset failed\n");
781 bgmac_phy_init(bgmac);
782}
783
784/**************************************************
785 * Chip ops
786 **************************************************/
787
788/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
789 * nothing to change? Try if after stabilizng driver.
790 */
791static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
792 bool force)
793{
794 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
795 u32 new_val = (cmdcfg & mask) | set;
796
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100797 bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000798 udelay(2);
799
800 if (new_val != cmdcfg || force)
801 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
802
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +0100803 bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR(bgmac->core->id.rev));
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000804 udelay(2);
805}
806
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000807static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
808{
809 u32 tmp;
810
811 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
812 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
813 tmp = (addr[4] << 8) | addr[5];
814 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
815}
816
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000817static void bgmac_set_rx_mode(struct net_device *net_dev)
818{
819 struct bgmac *bgmac = netdev_priv(net_dev);
820
821 if (net_dev->flags & IFF_PROMISC)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000822 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000823 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +0000824 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000825}
826
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000827#if 0 /* We don't use that regs yet */
828static void bgmac_chip_stats_update(struct bgmac *bgmac)
829{
830 int i;
831
832 if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
833 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
834 bgmac->mib_tx_regs[i] =
835 bgmac_read(bgmac,
836 BGMAC_TX_GOOD_OCTETS + (i * 4));
837 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
838 bgmac->mib_rx_regs[i] =
839 bgmac_read(bgmac,
840 BGMAC_RX_GOOD_OCTETS + (i * 4));
841 }
842
843 /* TODO: what else? how to handle BCM4706? Specs are needed */
844}
845#endif
846
847static void bgmac_clear_mib(struct bgmac *bgmac)
848{
849 int i;
850
851 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
852 return;
853
854 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
855 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
856 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
857 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
858 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
859}
860
861/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100862static void bgmac_mac_speed(struct bgmac *bgmac)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000863{
864 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
865 u32 set = 0;
866
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100867 switch (bgmac->mac_speed) {
868 case SPEED_10:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000869 set |= BGMAC_CMDCFG_ES_10;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100870 break;
871 case SPEED_100:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000872 set |= BGMAC_CMDCFG_ES_100;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100873 break;
874 case SPEED_1000:
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000875 set |= BGMAC_CMDCFG_ES_1000;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100876 break;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100877 case SPEED_2500:
878 set |= BGMAC_CMDCFG_ES_2500;
879 break;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100880 default:
881 bgmac_err(bgmac, "Unsupported speed: %d\n", bgmac->mac_speed);
882 }
883
884 if (bgmac->mac_duplex == DUPLEX_HALF)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000885 set |= BGMAC_CMDCFG_HD;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100886
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000887 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
888}
889
890static void bgmac_miiconfig(struct bgmac *bgmac)
891{
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100892 struct bcma_device *core = bgmac->core;
893 struct bcma_chipinfo *ci = &core->bus->chipinfo;
894 u8 imode;
895
896 if (ci->id == BCMA_CHIP_ID_BCM4707 ||
897 ci->id == BCMA_CHIP_ID_BCM53018) {
898 bcma_awrite32(core, BCMA_IOCTL,
899 bcma_aread32(core, BCMA_IOCTL) | 0x40 |
900 BGMAC_BCMA_IOCTL_SW_CLKEN);
901 bgmac->mac_speed = SPEED_2500;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +0100902 bgmac->mac_duplex = DUPLEX_FULL;
903 bgmac_mac_speed(bgmac);
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100904 } else {
905 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
906 BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
907 if (imode == 0 || imode == 1) {
908 bgmac->mac_speed = SPEED_100;
909 bgmac->mac_duplex = DUPLEX_FULL;
910 bgmac_mac_speed(bgmac);
911 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000912 }
913}
914
915/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
916static void bgmac_chip_reset(struct bgmac *bgmac)
917{
918 struct bcma_device *core = bgmac->core;
919 struct bcma_bus *bus = core->bus;
920 struct bcma_chipinfo *ci = &bus->chipinfo;
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100921 u32 flags;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000922 u32 iost;
923 int i;
924
925 if (bcma_core_is_enabled(core)) {
926 if (!bgmac->stats_grabbed) {
927 /* bgmac_chip_stats_update(bgmac); */
928 bgmac->stats_grabbed = true;
929 }
930
931 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
932 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
933
934 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
935 udelay(1);
936
937 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
938 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
939
940 /* TODO: Clear software multicast filter list */
941 }
942
943 iost = bcma_aread32(core, BCMA_IOST);
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100944 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000945 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100946 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188))
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000947 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
948
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100949 /* 3GMAC: for BCM4707, only do core reset at bgmac_probe() */
950 if (ci->id != BCMA_CHIP_ID_BCM4707) {
951 flags = 0;
952 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
953 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
954 if (!bgmac->has_robosw)
955 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
956 }
957 bcma_core_enable(core, flags);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000958 }
959
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +0100960 /* Request Misc PLL for corerev > 2 */
961 if (core->id.rev > 2 &&
962 ci->id != BCMA_CHIP_ID_BCM4707 &&
963 ci->id != BCMA_CHIP_ID_BCM53018) {
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100964 bgmac_set(bgmac, BCMA_CLKCTLST,
965 BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
966 bgmac_wait_value(bgmac->core, BCMA_CLKCTLST,
967 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
968 BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000969 1000);
970 }
971
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100972 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
973 ci->id == BCMA_CHIP_ID_BCM4749 ||
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000974 ci->id == BCMA_CHIP_ID_BCM53572) {
975 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
976 u8 et_swtype = 0;
977 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
Rafał Miłecki6a391e72013-09-15 00:22:47 +0200978 BGMAC_CHIPCTL_1_IF_TYPE_MII;
Hauke Mehrtens36472682013-09-15 22:49:08 +0200979 char buf[4];
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000980
Hauke Mehrtens36472682013-09-15 22:49:08 +0200981 if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000982 if (kstrtou8(buf, 0, &et_swtype))
983 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
984 buf);
985 et_swtype &= 0x0f;
986 et_swtype <<= 4;
987 sw_type = et_swtype;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100988 } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM5358) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000989 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Rafał Miłecki1a0ab762013-12-11 08:44:37 +0100990 } else if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
991 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
992 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188)) {
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +0000993 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
994 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000995 }
996 bcma_chipco_chipctl_maskset(cc, 1,
997 ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
998 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
999 sw_type);
1000 }
1001
1002 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
1003 bcma_awrite32(core, BCMA_IOCTL,
1004 bcma_aread32(core, BCMA_IOCTL) &
1005 ~BGMAC_BCMA_IOCTL_SW_RESET);
1006
1007 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
1008 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
1009 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
1010 * be keps until taking MAC out of the reset.
1011 */
1012 bgmac_cmdcfg_maskset(bgmac,
1013 ~(BGMAC_CMDCFG_TE |
1014 BGMAC_CMDCFG_RE |
1015 BGMAC_CMDCFG_RPI |
1016 BGMAC_CMDCFG_TAI |
1017 BGMAC_CMDCFG_HD |
1018 BGMAC_CMDCFG_ML |
1019 BGMAC_CMDCFG_CFE |
1020 BGMAC_CMDCFG_RL |
1021 BGMAC_CMDCFG_RED |
1022 BGMAC_CMDCFG_PE |
1023 BGMAC_CMDCFG_TPI |
1024 BGMAC_CMDCFG_PAD_EN |
1025 BGMAC_CMDCFG_PF),
1026 BGMAC_CMDCFG_PROM |
1027 BGMAC_CMDCFG_NLC |
1028 BGMAC_CMDCFG_CFE |
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001029 BGMAC_CMDCFG_SR(core->id.rev),
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001030 false);
Rafał Miłeckid4699622013-12-11 07:44:14 +01001031 bgmac->mac_speed = SPEED_UNKNOWN;
1032 bgmac->mac_duplex = DUPLEX_UNKNOWN;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001033
1034 bgmac_clear_mib(bgmac);
1035 if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
1036 bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
1037 BCMA_GMAC_CMN_PC_MTE);
1038 else
1039 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
1040 bgmac_miiconfig(bgmac);
1041 bgmac_phy_init(bgmac);
1042
Hauke Mehrtens49a467b2013-09-29 13:54:58 +02001043 netdev_reset_queue(bgmac->net_dev);
1044
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001045 bgmac->int_status = 0;
1046}
1047
1048static void bgmac_chip_intrs_on(struct bgmac *bgmac)
1049{
1050 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
1051}
1052
1053static void bgmac_chip_intrs_off(struct bgmac *bgmac)
1054{
1055 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
Nathan Hintz41608152013-02-13 19:14:10 +00001056 bgmac_read(bgmac, BGMAC_INT_MASK);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001057}
1058
1059/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
1060static void bgmac_enable(struct bgmac *bgmac)
1061{
1062 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
1063 u32 cmdcfg;
1064 u32 mode;
1065 u32 rxq_ctl;
1066 u32 fl_ctl;
1067 u16 bp_clk;
1068 u8 mdp;
1069
1070 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
1071 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
Hauke Mehrtens48e07fb2014-01-05 01:10:45 +01001072 BGMAC_CMDCFG_SR(bgmac->core->id.rev), true);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001073 udelay(2);
1074 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
1075 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
1076
1077 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
1078 BGMAC_DS_MM_SHIFT;
1079 if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
1080 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
1081 if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
1082 bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
1083 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1084
1085 switch (ci->id) {
1086 case BCMA_CHIP_ID_BCM5357:
1087 case BCMA_CHIP_ID_BCM4749:
1088 case BCMA_CHIP_ID_BCM53572:
1089 case BCMA_CHIP_ID_BCM4716:
1090 case BCMA_CHIP_ID_BCM47162:
1091 fl_ctl = 0x03cb04cb;
1092 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1093 ci->id == BCMA_CHIP_ID_BCM4749 ||
1094 ci->id == BCMA_CHIP_ID_BCM53572)
1095 fl_ctl = 0x2300e1;
1096 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1097 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
1098 break;
1099 }
1100
Hauke Mehrtens6df4aff2014-01-05 01:10:47 +01001101 if (ci->id != BCMA_CHIP_ID_BCM4707 &&
1102 ci->id != BCMA_CHIP_ID_BCM53018) {
1103 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1104 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1105 bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) /
1106 1000000;
1107 mdp = (bp_clk * 128 / 1000) - 3;
1108 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1109 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1110 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001111}
1112
1113/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
1114static void bgmac_chip_init(struct bgmac *bgmac, bool full_init)
1115{
1116 struct bgmac_dma_ring *ring;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001117 int i;
1118
1119 /* 1 interrupt per received frame */
1120 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1121
1122 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1123 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1124
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001125 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001126
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001127 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001128
1129 if (bgmac->loopback)
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001130 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001131 else
Rafał Miłeckie9ba1032013-02-07 05:40:38 +00001132 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001133
1134 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1135
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001136 if (full_init) {
1137 bgmac_dma_init(bgmac);
1138 if (1) /* FIXME: is there any case we don't want IRQs? */
1139 bgmac_chip_intrs_on(bgmac);
1140 } else {
1141 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
1142 ring = &bgmac->rx_ring[i];
1143 bgmac_dma_rx_enable(bgmac, ring);
1144 }
1145 }
1146
1147 bgmac_enable(bgmac);
1148}
1149
1150static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1151{
1152 struct bgmac *bgmac = netdev_priv(dev_id);
1153
1154 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1155 int_status &= bgmac->int_mask;
1156
1157 if (!int_status)
1158 return IRQ_NONE;
1159
1160 /* Ack */
1161 bgmac_write(bgmac, BGMAC_INT_STATUS, int_status);
1162
1163 /* Disable new interrupts until handling existing ones */
1164 bgmac_chip_intrs_off(bgmac);
1165
1166 bgmac->int_status = int_status;
1167
1168 napi_schedule(&bgmac->napi);
1169
1170 return IRQ_HANDLED;
1171}
1172
1173static int bgmac_poll(struct napi_struct *napi, int weight)
1174{
1175 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
1176 struct bgmac_dma_ring *ring;
1177 int handled = 0;
1178
1179 if (bgmac->int_status & BGMAC_IS_TX0) {
1180 ring = &bgmac->tx_ring[0];
1181 bgmac_dma_tx_free(bgmac, ring);
1182 bgmac->int_status &= ~BGMAC_IS_TX0;
1183 }
1184
1185 if (bgmac->int_status & BGMAC_IS_RX) {
1186 ring = &bgmac->rx_ring[0];
1187 handled += bgmac_dma_rx_read(bgmac, ring, weight);
1188 bgmac->int_status &= ~BGMAC_IS_RX;
1189 }
1190
1191 if (bgmac->int_status) {
1192 bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", bgmac->int_status);
1193 bgmac->int_status = 0;
1194 }
1195
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001196 if (handled < weight) {
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001197 napi_complete(napi);
Hauke Mehrtens43f159c2015-01-18 19:49:59 +01001198 bgmac_chip_intrs_on(bgmac);
1199 }
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001200
1201 return handled;
1202}
1203
1204/**************************************************
1205 * net_device_ops
1206 **************************************************/
1207
1208static int bgmac_open(struct net_device *net_dev)
1209{
1210 struct bgmac *bgmac = netdev_priv(net_dev);
1211 int err = 0;
1212
1213 bgmac_chip_reset(bgmac);
1214 /* Specs say about reclaiming rings here, but we do that in DMA init */
1215 bgmac_chip_init(bgmac, true);
1216
1217 err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
1218 KBUILD_MODNAME, net_dev);
1219 if (err < 0) {
1220 bgmac_err(bgmac, "IRQ request error: %d!\n", err);
1221 goto err_out;
1222 }
1223 napi_enable(&bgmac->napi);
1224
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001225 phy_start(bgmac->phy_dev);
1226
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001227 netif_carrier_on(net_dev);
1228
1229err_out:
1230 return err;
1231}
1232
1233static int bgmac_stop(struct net_device *net_dev)
1234{
1235 struct bgmac *bgmac = netdev_priv(net_dev);
1236
1237 netif_carrier_off(net_dev);
1238
Rafał Miłecki4e34da4d2013-12-10 17:19:39 +01001239 phy_stop(bgmac->phy_dev);
1240
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001241 napi_disable(&bgmac->napi);
1242 bgmac_chip_intrs_off(bgmac);
1243 free_irq(bgmac->core->irq, net_dev);
1244
1245 bgmac_chip_reset(bgmac);
1246
1247 return 0;
1248}
1249
1250static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1251 struct net_device *net_dev)
1252{
1253 struct bgmac *bgmac = netdev_priv(net_dev);
1254 struct bgmac_dma_ring *ring;
1255
1256 /* No QOS support yet */
1257 ring = &bgmac->tx_ring[0];
1258 return bgmac_dma_tx_add(bgmac, ring, skb);
1259}
1260
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001261static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1262{
1263 struct bgmac *bgmac = netdev_priv(net_dev);
1264 int ret;
1265
1266 ret = eth_prepare_mac_addr_change(net_dev, addr);
1267 if (ret < 0)
1268 return ret;
1269 bgmac_write_mac_address(bgmac, (u8 *)addr);
1270 eth_commit_mac_addr_change(net_dev, addr);
1271 return 0;
1272}
1273
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001274static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1275{
1276 struct bgmac *bgmac = netdev_priv(net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001277
Hauke Mehrtens69c58852013-12-20 15:34:45 +01001278 if (!netif_running(net_dev))
1279 return -EINVAL;
1280
1281 return phy_mii_ioctl(bgmac->phy_dev, ifr, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001282}
1283
1284static const struct net_device_ops bgmac_netdev_ops = {
1285 .ndo_open = bgmac_open,
1286 .ndo_stop = bgmac_stop,
1287 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001288 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001289 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001290 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001291 .ndo_do_ioctl = bgmac_ioctl,
1292};
1293
1294/**************************************************
1295 * ethtool_ops
1296 **************************************************/
1297
1298static int bgmac_get_settings(struct net_device *net_dev,
1299 struct ethtool_cmd *cmd)
1300{
1301 struct bgmac *bgmac = netdev_priv(net_dev);
1302
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001303 return phy_ethtool_gset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001304}
1305
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001306static int bgmac_set_settings(struct net_device *net_dev,
1307 struct ethtool_cmd *cmd)
1308{
1309 struct bgmac *bgmac = netdev_priv(net_dev);
1310
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001311 return phy_ethtool_sset(bgmac->phy_dev, cmd);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001312}
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001313
1314static void bgmac_get_drvinfo(struct net_device *net_dev,
1315 struct ethtool_drvinfo *info)
1316{
1317 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1318 strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
1319}
1320
1321static const struct ethtool_ops bgmac_ethtool_ops = {
1322 .get_settings = bgmac_get_settings,
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001323 .set_settings = bgmac_set_settings,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001324 .get_drvinfo = bgmac_get_drvinfo,
1325};
1326
1327/**************************************************
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001328 * MII
1329 **************************************************/
1330
1331static int bgmac_mii_read(struct mii_bus *bus, int mii_id, int regnum)
1332{
1333 return bgmac_phy_read(bus->priv, mii_id, regnum);
1334}
1335
1336static int bgmac_mii_write(struct mii_bus *bus, int mii_id, int regnum,
1337 u16 value)
1338{
1339 return bgmac_phy_write(bus->priv, mii_id, regnum, value);
1340}
1341
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001342static void bgmac_adjust_link(struct net_device *net_dev)
1343{
1344 struct bgmac *bgmac = netdev_priv(net_dev);
1345 struct phy_device *phy_dev = bgmac->phy_dev;
1346 bool update = false;
1347
1348 if (phy_dev->link) {
1349 if (phy_dev->speed != bgmac->mac_speed) {
1350 bgmac->mac_speed = phy_dev->speed;
1351 update = true;
1352 }
1353
1354 if (phy_dev->duplex != bgmac->mac_duplex) {
1355 bgmac->mac_duplex = phy_dev->duplex;
1356 update = true;
1357 }
1358 }
1359
1360 if (update) {
1361 bgmac_mac_speed(bgmac);
1362 phy_print_status(phy_dev);
1363 }
1364}
1365
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001366static int bgmac_fixed_phy_register(struct bgmac *bgmac)
1367{
1368 struct fixed_phy_status fphy_status = {
1369 .link = 1,
1370 .speed = SPEED_1000,
1371 .duplex = DUPLEX_FULL,
1372 };
1373 struct phy_device *phy_dev;
1374 int err;
1375
1376 phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
1377 if (!phy_dev || IS_ERR(phy_dev)) {
1378 bgmac_err(bgmac, "Failed to register fixed PHY device\n");
1379 return -ENODEV;
1380 }
1381
1382 err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
1383 PHY_INTERFACE_MODE_MII);
1384 if (err) {
1385 bgmac_err(bgmac, "Connecting PHY failed\n");
1386 return err;
1387 }
1388
1389 bgmac->phy_dev = phy_dev;
1390
1391 return err;
1392}
1393
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001394static int bgmac_mii_register(struct bgmac *bgmac)
1395{
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001396 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001397 struct mii_bus *mii_bus;
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001398 struct phy_device *phy_dev;
1399 char bus_id[MII_BUS_ID_SIZE + 3];
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001400 int i, err = 0;
1401
Rafał Miłeckic25b23b2015-03-20 23:14:31 +01001402 if (ci->id == BCMA_CHIP_ID_BCM4707 ||
1403 ci->id == BCMA_CHIP_ID_BCM53018)
1404 return bgmac_fixed_phy_register(bgmac);
1405
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001406 mii_bus = mdiobus_alloc();
1407 if (!mii_bus)
1408 return -ENOMEM;
1409
1410 mii_bus->name = "bgmac mii bus";
1411 sprintf(mii_bus->id, "%s-%d-%d", "bgmac", bgmac->core->bus->num,
1412 bgmac->core->core_unit);
1413 mii_bus->priv = bgmac;
1414 mii_bus->read = bgmac_mii_read;
1415 mii_bus->write = bgmac_mii_write;
1416 mii_bus->parent = &bgmac->core->dev;
1417 mii_bus->phy_mask = ~(1 << bgmac->phyaddr);
1418
1419 mii_bus->irq = kmalloc_array(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
1420 if (!mii_bus->irq) {
1421 err = -ENOMEM;
1422 goto err_free_bus;
1423 }
1424 for (i = 0; i < PHY_MAX_ADDR; i++)
1425 mii_bus->irq[i] = PHY_POLL;
1426
1427 err = mdiobus_register(mii_bus);
1428 if (err) {
1429 bgmac_err(bgmac, "Registration of mii bus failed\n");
1430 goto err_free_irq;
1431 }
1432
1433 bgmac->mii_bus = mii_bus;
1434
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001435 /* Connect to the PHY */
1436 snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
1437 bgmac->phyaddr);
1438 phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
1439 PHY_INTERFACE_MODE_MII);
1440 if (IS_ERR(phy_dev)) {
1441 bgmac_err(bgmac, "PHY connecton failed\n");
1442 err = PTR_ERR(phy_dev);
1443 goto err_unregister_bus;
1444 }
1445 bgmac->phy_dev = phy_dev;
1446
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001447 return err;
1448
Rafał Miłecki5824d2d2013-12-07 00:53:55 +01001449err_unregister_bus:
1450 mdiobus_unregister(mii_bus);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001451err_free_irq:
1452 kfree(mii_bus->irq);
1453err_free_bus:
1454 mdiobus_free(mii_bus);
1455 return err;
1456}
1457
1458static void bgmac_mii_unregister(struct bgmac *bgmac)
1459{
1460 struct mii_bus *mii_bus = bgmac->mii_bus;
1461
1462 mdiobus_unregister(mii_bus);
1463 kfree(mii_bus->irq);
1464 mdiobus_free(mii_bus);
1465}
1466
1467/**************************************************
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001468 * BCMA bus ops
1469 **************************************************/
1470
1471/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
1472static int bgmac_probe(struct bcma_device *core)
1473{
Rafał Miłecki21697332015-02-11 18:06:34 +01001474 struct bcma_chipinfo *ci = &core->bus->chipinfo;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001475 struct net_device *net_dev;
1476 struct bgmac *bgmac;
1477 struct ssb_sprom *sprom = &core->bus->sprom;
1478 u8 *mac = core->core_unit ? sprom->et1mac : sprom->et0mac;
1479 int err;
1480
1481 /* We don't support 2nd, 3rd, ... units, SPROM has to be adjusted */
1482 if (core->core_unit > 1) {
1483 pr_err("Unsupported core_unit %d\n", core->core_unit);
1484 return -ENOTSUPP;
1485 }
1486
Rafał Miłeckid166f212013-02-07 00:27:17 +00001487 if (!is_valid_ether_addr(mac)) {
1488 dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
1489 eth_random_addr(mac);
1490 dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
1491 }
1492
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001493 /* Allocation and references */
1494 net_dev = alloc_etherdev(sizeof(*bgmac));
1495 if (!net_dev)
1496 return -ENOMEM;
1497 net_dev->netdev_ops = &bgmac_netdev_ops;
1498 net_dev->irq = core->irq;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001499 net_dev->ethtool_ops = &bgmac_ethtool_ops;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001500 bgmac = netdev_priv(net_dev);
1501 bgmac->net_dev = net_dev;
1502 bgmac->core = core;
1503 bcma_set_drvdata(core, bgmac);
1504
1505 /* Defaults */
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001506 memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
1507
1508 /* On BCM4706 we need common core to access PHY */
1509 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
1510 !core->bus->drv_gmac_cmn.core) {
1511 bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
1512 err = -ENODEV;
1513 goto err_netdev_free;
1514 }
1515 bgmac->cmn = core->bus->drv_gmac_cmn.core;
1516
1517 bgmac->phyaddr = core->core_unit ? sprom->et1phyaddr :
1518 sprom->et0phyaddr;
1519 bgmac->phyaddr &= BGMAC_PHY_MASK;
1520 if (bgmac->phyaddr == BGMAC_PHY_MASK) {
1521 bgmac_err(bgmac, "No PHY found\n");
1522 err = -ENODEV;
1523 goto err_netdev_free;
1524 }
1525 bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
1526 bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
1527
1528 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
1529 bgmac_err(bgmac, "PCI setup not implemented\n");
1530 err = -ENOTSUPP;
1531 goto err_netdev_free;
1532 }
1533
1534 bgmac_chip_reset(bgmac);
1535
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001536 /* For Northstar, we have to take all GMAC core out of reset */
Rafał Miłecki21697332015-02-11 18:06:34 +01001537 if (ci->id == BCMA_CHIP_ID_BCM4707 ||
1538 ci->id == BCMA_CHIP_ID_BCM53018) {
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001539 struct bcma_device *ns_core;
1540 int ns_gmac;
1541
1542 /* Northstar has 4 GMAC cores */
1543 for (ns_gmac = 0; ns_gmac < 4; ns_gmac++) {
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001544 /* As Northstar requirement, we have to reset all GMACs
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001545 * before accessing one. bgmac_chip_reset() call
1546 * bcma_core_enable() for this core. Then the other
Hauke Mehrtens0e595932014-01-06 23:24:29 +01001547 * three GMACs didn't reset. We do it here.
Hauke Mehrtens622a5212014-01-05 01:10:46 +01001548 */
1549 ns_core = bcma_find_core_unit(core->bus,
1550 BCMA_CORE_MAC_GBIT,
1551 ns_gmac);
1552 if (ns_core && !bcma_core_is_enabled(ns_core))
1553 bcma_core_enable(ns_core, 0);
1554 }
1555 }
1556
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001557 err = bgmac_dma_alloc(bgmac);
1558 if (err) {
1559 bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
1560 goto err_netdev_free;
1561 }
1562
1563 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
Ralf Baechleedb15d82013-02-21 16:16:55 +01001564 if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001565 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1566
1567 /* TODO: reset the external phy. Specs are needed */
1568 bgmac_phy_reset(bgmac);
1569
1570 bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
1571 BGMAC_BFL_ENETROBO);
1572 if (bgmac->has_robosw)
1573 bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
1574
1575 if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
1576 bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
1577
Hauke Mehrtens62166422015-01-18 19:49:58 +01001578 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1579
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001580 err = bgmac_mii_register(bgmac);
1581 if (err) {
1582 bgmac_err(bgmac, "Cannot register MDIO\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001583 goto err_dma_free;
1584 }
1585
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001586 err = register_netdev(bgmac->net_dev);
1587 if (err) {
1588 bgmac_err(bgmac, "Cannot register net device\n");
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001589 goto err_mii_unregister;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001590 }
1591
1592 netif_carrier_off(net_dev);
1593
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001594 return 0;
1595
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001596err_mii_unregister:
1597 bgmac_mii_unregister(bgmac);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001598err_dma_free:
1599 bgmac_dma_free(bgmac);
1600
1601err_netdev_free:
1602 bcma_set_drvdata(core, NULL);
1603 free_netdev(net_dev);
1604
1605 return err;
1606}
1607
1608static void bgmac_remove(struct bcma_device *core)
1609{
1610 struct bgmac *bgmac = bcma_get_drvdata(core);
1611
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001612 unregister_netdev(bgmac->net_dev);
Rafał Miłecki11e5e762013-03-07 01:53:28 +00001613 bgmac_mii_unregister(bgmac);
Hauke Mehrtens62166422015-01-18 19:49:58 +01001614 netif_napi_del(&bgmac->napi);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001615 bgmac_dma_free(bgmac);
1616 bcma_set_drvdata(core, NULL);
1617 free_netdev(bgmac->net_dev);
1618}
1619
1620static struct bcma_driver bgmac_bcma_driver = {
1621 .name = KBUILD_MODNAME,
1622 .id_table = bgmac_bcma_tbl,
1623 .probe = bgmac_probe,
1624 .remove = bgmac_remove,
1625};
1626
1627static int __init bgmac_init(void)
1628{
1629 int err;
1630
1631 err = bcma_driver_register(&bgmac_bcma_driver);
1632 if (err)
1633 return err;
1634 pr_info("Broadcom 47xx GBit MAC driver loaded\n");
1635
1636 return 0;
1637}
1638
1639static void __exit bgmac_exit(void)
1640{
1641 bcma_driver_unregister(&bgmac_bcma_driver);
1642}
1643
1644module_init(bgmac_init)
1645module_exit(bgmac_exit)
1646
1647MODULE_AUTHOR("Rafał Miłecki");
1648MODULE_LICENSE("GPL");