blob: 380084bad129e1fbb4a3c395f03d3eda90773748 [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>
16#include <linux/interrupt.h>
17#include <linux/dma-mapping.h>
18#include <asm/mach-bcm47xx/nvram.h>
19
20static const struct bcma_device_id bgmac_bcma_tbl[] = {
21 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_4706_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
22 BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
23 BCMA_CORETABLE_END
24};
25MODULE_DEVICE_TABLE(bcma, bgmac_bcma_tbl);
26
27static bool bgmac_wait_value(struct bcma_device *core, u16 reg, u32 mask,
28 u32 value, int timeout)
29{
30 u32 val;
31 int i;
32
33 for (i = 0; i < timeout / 10; i++) {
34 val = bcma_read32(core, reg);
35 if ((val & mask) == value)
36 return true;
37 udelay(10);
38 }
39 pr_err("Timeout waiting for reg 0x%X\n", reg);
40 return false;
41}
42
43/**************************************************
44 * DMA
45 **************************************************/
46
47static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
48{
49 u32 val;
50 int i;
51
52 if (!ring->mmio_base)
53 return;
54
55 /* Suspend DMA TX ring first.
56 * bgmac_wait_value doesn't support waiting for any of few values, so
57 * implement whole loop here.
58 */
59 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
60 BGMAC_DMA_TX_SUSPEND);
61 for (i = 0; i < 10000 / 10; i++) {
62 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
63 val &= BGMAC_DMA_TX_STAT;
64 if (val == BGMAC_DMA_TX_STAT_DISABLED ||
65 val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
66 val == BGMAC_DMA_TX_STAT_STOPPED) {
67 i = 0;
68 break;
69 }
70 udelay(10);
71 }
72 if (i)
73 bgmac_err(bgmac, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
74 ring->mmio_base, val);
75
76 /* Remove SUSPEND bit */
77 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
78 if (!bgmac_wait_value(bgmac->core,
79 ring->mmio_base + BGMAC_DMA_TX_STATUS,
80 BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
81 10000)) {
82 bgmac_warn(bgmac, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
83 ring->mmio_base);
84 udelay(300);
85 val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
86 if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
87 bgmac_err(bgmac, "Reset of DMA TX ring 0x%X failed\n",
88 ring->mmio_base);
89 }
90}
91
92static void bgmac_dma_tx_enable(struct bgmac *bgmac,
93 struct bgmac_dma_ring *ring)
94{
95 u32 ctl;
96
97 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
98 ctl |= BGMAC_DMA_TX_ENABLE;
99 ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
100 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
101}
102
103static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
104 struct bgmac_dma_ring *ring,
105 struct sk_buff *skb)
106{
107 struct device *dma_dev = bgmac->core->dma_dev;
108 struct net_device *net_dev = bgmac->net_dev;
109 struct bgmac_dma_desc *dma_desc;
110 struct bgmac_slot_info *slot;
111 u32 ctl0, ctl1;
112 int free_slots;
113
114 if (skb->len > BGMAC_DESC_CTL1_LEN) {
115 bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
116 goto err_stop_drop;
117 }
118
119 if (ring->start <= ring->end)
120 free_slots = ring->start - ring->end + BGMAC_TX_RING_SLOTS;
121 else
122 free_slots = ring->start - ring->end;
123 if (free_slots == 1) {
124 bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
125 netif_stop_queue(net_dev);
126 return NETDEV_TX_BUSY;
127 }
128
129 slot = &ring->slots[ring->end];
130 slot->skb = skb;
131 slot->dma_addr = dma_map_single(dma_dev, skb->data, skb->len,
132 DMA_TO_DEVICE);
133 if (dma_mapping_error(dma_dev, slot->dma_addr)) {
134 bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n",
135 ring->mmio_base);
136 goto err_stop_drop;
137 }
138
139 ctl0 = BGMAC_DESC_CTL0_IOC | BGMAC_DESC_CTL0_SOF | BGMAC_DESC_CTL0_EOF;
140 if (ring->end == ring->num_slots - 1)
141 ctl0 |= BGMAC_DESC_CTL0_EOT;
142 ctl1 = skb->len & BGMAC_DESC_CTL1_LEN;
143
144 dma_desc = ring->cpu_base;
145 dma_desc += ring->end;
146 dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
147 dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
148 dma_desc->ctl0 = cpu_to_le32(ctl0);
149 dma_desc->ctl1 = cpu_to_le32(ctl1);
150
151 wmb();
152
153 /* Increase ring->end to point empty slot. We tell hardware the first
154 * slot it should *not* read.
155 */
156 if (++ring->end >= BGMAC_TX_RING_SLOTS)
157 ring->end = 0;
158 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
159 ring->end * sizeof(struct bgmac_dma_desc));
160
161 /* Always keep one slot free to allow detecting bugged calls. */
162 if (--free_slots == 1)
163 netif_stop_queue(net_dev);
164
165 return NETDEV_TX_OK;
166
167err_stop_drop:
168 netif_stop_queue(net_dev);
169 dev_kfree_skb(skb);
170 return NETDEV_TX_OK;
171}
172
173/* Free transmitted packets */
174static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
175{
176 struct device *dma_dev = bgmac->core->dma_dev;
177 int empty_slot;
178 bool freed = false;
179
180 /* The last slot that hardware didn't consume yet */
181 empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
182 empty_slot &= BGMAC_DMA_TX_STATDPTR;
183 empty_slot /= sizeof(struct bgmac_dma_desc);
184
185 while (ring->start != empty_slot) {
186 struct bgmac_slot_info *slot = &ring->slots[ring->start];
187
188 if (slot->skb) {
189 /* Unmap no longer used buffer */
190 dma_unmap_single(dma_dev, slot->dma_addr,
191 slot->skb->len, DMA_TO_DEVICE);
192 slot->dma_addr = 0;
193
194 /* Free memory! :) */
195 dev_kfree_skb(slot->skb);
196 slot->skb = NULL;
197 } else {
198 bgmac_err(bgmac, "Hardware reported transmission for empty TX ring slot %d! End of ring: %d\n",
199 ring->start, ring->end);
200 }
201
202 if (++ring->start >= BGMAC_TX_RING_SLOTS)
203 ring->start = 0;
204 freed = true;
205 }
206
207 if (freed && netif_queue_stopped(bgmac->net_dev))
208 netif_wake_queue(bgmac->net_dev);
209}
210
211static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
212{
213 if (!ring->mmio_base)
214 return;
215
216 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
217 if (!bgmac_wait_value(bgmac->core,
218 ring->mmio_base + BGMAC_DMA_RX_STATUS,
219 BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
220 10000))
221 bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
222 ring->mmio_base);
223}
224
225static void bgmac_dma_rx_enable(struct bgmac *bgmac,
226 struct bgmac_dma_ring *ring)
227{
228 u32 ctl;
229
230 ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
231 ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
232 ctl |= BGMAC_DMA_RX_ENABLE;
233 ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
234 ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
235 ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
236 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
237}
238
239static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
240 struct bgmac_slot_info *slot)
241{
242 struct device *dma_dev = bgmac->core->dma_dev;
243 struct bgmac_rx_header *rx;
244
245 /* Alloc skb */
246 slot->skb = netdev_alloc_skb(bgmac->net_dev, BGMAC_RX_BUF_SIZE);
247 if (!slot->skb) {
248 bgmac_err(bgmac, "Allocation of skb failed!\n");
249 return -ENOMEM;
250 }
251
252 /* Poison - if everything goes fine, hardware will overwrite it */
253 rx = (struct bgmac_rx_header *)slot->skb->data;
254 rx->len = cpu_to_le16(0xdead);
255 rx->flags = cpu_to_le16(0xbeef);
256
257 /* Map skb for the DMA */
258 slot->dma_addr = dma_map_single(dma_dev, slot->skb->data,
259 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
260 if (dma_mapping_error(dma_dev, slot->dma_addr)) {
261 bgmac_err(bgmac, "DMA mapping error\n");
262 return -ENOMEM;
263 }
264 if (slot->dma_addr & 0xC0000000)
265 bgmac_warn(bgmac, "DMA address using 0xC0000000 bit(s), it may need translation trick\n");
266
267 return 0;
268}
269
270static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
271 int weight)
272{
273 u32 end_slot;
274 int handled = 0;
275
276 end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
277 end_slot &= BGMAC_DMA_RX_STATDPTR;
278 end_slot /= sizeof(struct bgmac_dma_desc);
279
280 ring->end = end_slot;
281
282 while (ring->start != ring->end) {
283 struct device *dma_dev = bgmac->core->dma_dev;
284 struct bgmac_slot_info *slot = &ring->slots[ring->start];
285 struct sk_buff *skb = slot->skb;
286 struct sk_buff *new_skb;
287 struct bgmac_rx_header *rx;
288 u16 len, flags;
289
290 /* Unmap buffer to make it accessible to the CPU */
291 dma_sync_single_for_cpu(dma_dev, slot->dma_addr,
292 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
293
294 /* Get info from the header */
295 rx = (struct bgmac_rx_header *)skb->data;
296 len = le16_to_cpu(rx->len);
297 flags = le16_to_cpu(rx->flags);
298
299 /* Check for poison and drop or pass the packet */
300 if (len == 0xdead && flags == 0xbeef) {
301 bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
302 ring->start);
303 } else {
304 new_skb = netdev_alloc_skb(bgmac->net_dev, len);
305 if (new_skb) {
306 skb_put(new_skb, len);
307 skb_copy_from_linear_data_offset(skb, BGMAC_RX_FRAME_OFFSET,
308 new_skb->data,
309 len);
310 new_skb->protocol =
311 eth_type_trans(new_skb, bgmac->net_dev);
312 netif_receive_skb(new_skb);
313 handled++;
314 } else {
315 bgmac->net_dev->stats.rx_dropped++;
316 bgmac_err(bgmac, "Allocation of skb for copying packet failed!\n");
317 }
318
319 /* Poison the old skb */
320 rx->len = cpu_to_le16(0xdead);
321 rx->flags = cpu_to_le16(0xbeef);
322 }
323
324 /* Make it back accessible to the hardware */
325 dma_sync_single_for_device(dma_dev, slot->dma_addr,
326 BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
327
328 if (++ring->start >= BGMAC_RX_RING_SLOTS)
329 ring->start = 0;
330
331 if (handled >= weight) /* Should never be greater */
332 break;
333 }
334
335 return handled;
336}
337
338/* Does ring support unaligned addressing? */
339static bool bgmac_dma_unaligned(struct bgmac *bgmac,
340 struct bgmac_dma_ring *ring,
341 enum bgmac_dma_ring_type ring_type)
342{
343 switch (ring_type) {
344 case BGMAC_DMA_RING_TX:
345 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
346 0xff0);
347 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
348 return true;
349 break;
350 case BGMAC_DMA_RING_RX:
351 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
352 0xff0);
353 if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
354 return true;
355 break;
356 }
357 return false;
358}
359
360static void bgmac_dma_ring_free(struct bgmac *bgmac,
361 struct bgmac_dma_ring *ring)
362{
363 struct device *dma_dev = bgmac->core->dma_dev;
364 struct bgmac_slot_info *slot;
365 int size;
366 int i;
367
368 for (i = 0; i < ring->num_slots; i++) {
369 slot = &ring->slots[i];
370 if (slot->skb) {
371 if (slot->dma_addr)
372 dma_unmap_single(dma_dev, slot->dma_addr,
373 slot->skb->len, DMA_TO_DEVICE);
374 dev_kfree_skb(slot->skb);
375 }
376 }
377
378 if (ring->cpu_base) {
379 /* Free ring of descriptors */
380 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
381 dma_free_coherent(dma_dev, size, ring->cpu_base,
382 ring->dma_base);
383 }
384}
385
386static void bgmac_dma_free(struct bgmac *bgmac)
387{
388 int i;
389
390 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
391 bgmac_dma_ring_free(bgmac, &bgmac->tx_ring[i]);
392 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
393 bgmac_dma_ring_free(bgmac, &bgmac->rx_ring[i]);
394}
395
396static int bgmac_dma_alloc(struct bgmac *bgmac)
397{
398 struct device *dma_dev = bgmac->core->dma_dev;
399 struct bgmac_dma_ring *ring;
400 static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
401 BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
402 int size; /* ring size: different for Tx and Rx */
403 int err;
404 int i;
405
406 BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
407 BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
408
409 if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
410 bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
411 return -ENOTSUPP;
412 }
413
414 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
415 ring = &bgmac->tx_ring[i];
416 ring->num_slots = BGMAC_TX_RING_SLOTS;
417 ring->mmio_base = ring_base[i];
418 if (bgmac_dma_unaligned(bgmac, ring, BGMAC_DMA_RING_TX))
419 bgmac_warn(bgmac, "TX on ring 0x%X supports unaligned addressing but this feature is not implemented\n",
420 ring->mmio_base);
421
422 /* Alloc ring of descriptors */
423 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
424 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
425 &ring->dma_base,
426 GFP_KERNEL);
427 if (!ring->cpu_base) {
428 bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
429 ring->mmio_base);
430 goto err_dma_free;
431 }
432 if (ring->dma_base & 0xC0000000)
433 bgmac_warn(bgmac, "DMA address using 0xC0000000 bit(s), it may need translation trick\n");
434
435 /* No need to alloc TX slots yet */
436 }
437
438 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
439 ring = &bgmac->rx_ring[i];
440 ring->num_slots = BGMAC_RX_RING_SLOTS;
441 ring->mmio_base = ring_base[i];
442 if (bgmac_dma_unaligned(bgmac, ring, BGMAC_DMA_RING_RX))
443 bgmac_warn(bgmac, "RX on ring 0x%X supports unaligned addressing but this feature is not implemented\n",
444 ring->mmio_base);
445
446 /* Alloc ring of descriptors */
447 size = ring->num_slots * sizeof(struct bgmac_dma_desc);
448 ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
449 &ring->dma_base,
450 GFP_KERNEL);
451 if (!ring->cpu_base) {
452 bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
453 ring->mmio_base);
454 err = -ENOMEM;
455 goto err_dma_free;
456 }
457 if (ring->dma_base & 0xC0000000)
458 bgmac_warn(bgmac, "DMA address using 0xC0000000 bit(s), it may need translation trick\n");
459
460 /* Alloc RX slots */
461 for (i = 0; i < ring->num_slots; i++) {
462 err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[i]);
463 if (err) {
464 bgmac_err(bgmac, "Can't allocate skb for slot in RX ring\n");
465 goto err_dma_free;
466 }
467 }
468 }
469
470 return 0;
471
472err_dma_free:
473 bgmac_dma_free(bgmac);
474 return -ENOMEM;
475}
476
477static void bgmac_dma_init(struct bgmac *bgmac)
478{
479 struct bgmac_dma_ring *ring;
480 struct bgmac_dma_desc *dma_desc;
481 u32 ctl0, ctl1;
482 int i;
483
484 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
485 ring = &bgmac->tx_ring[i];
486
487 /* We don't implement unaligned addressing, so enable first */
488 bgmac_dma_tx_enable(bgmac, ring);
489 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
490 lower_32_bits(ring->dma_base));
491 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
492 upper_32_bits(ring->dma_base));
493
494 ring->start = 0;
495 ring->end = 0; /* Points the slot that should *not* be read */
496 }
497
498 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
499 ring = &bgmac->rx_ring[i];
500
501 /* We don't implement unaligned addressing, so enable first */
502 bgmac_dma_rx_enable(bgmac, ring);
503 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
504 lower_32_bits(ring->dma_base));
505 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
506 upper_32_bits(ring->dma_base));
507
508 for (i = 0, dma_desc = ring->cpu_base; i < ring->num_slots;
509 i++, dma_desc++) {
510 ctl0 = ctl1 = 0;
511
512 if (i == ring->num_slots - 1)
513 ctl0 |= BGMAC_DESC_CTL0_EOT;
514 ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
515 /* Is there any BGMAC device that requires extension? */
516 /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
517 * B43_DMA64_DCTL1_ADDREXT_MASK;
518 */
519
520 dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[i].dma_addr));
521 dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[i].dma_addr));
522 dma_desc->ctl0 = cpu_to_le32(ctl0);
523 dma_desc->ctl1 = cpu_to_le32(ctl1);
524 }
525
526 bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
527 ring->num_slots * sizeof(struct bgmac_dma_desc));
528
529 ring->start = 0;
530 ring->end = 0;
531 }
532}
533
534/**************************************************
535 * PHY ops
536 **************************************************/
537
538u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
539{
540 struct bcma_device *core;
541 u16 phy_access_addr;
542 u16 phy_ctl_addr;
543 u32 tmp;
544
545 BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
546 BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
547 BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
548 BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
549 BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
550 BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
551 BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
552 BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
553 BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
554 BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
555 BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
556
557 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
558 core = bgmac->core->bus->drv_gmac_cmn.core;
559 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
560 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
561 } else {
562 core = bgmac->core;
563 phy_access_addr = BGMAC_PHY_ACCESS;
564 phy_ctl_addr = BGMAC_PHY_CNTL;
565 }
566
567 tmp = bcma_read32(core, phy_ctl_addr);
568 tmp &= ~BGMAC_PC_EPA_MASK;
569 tmp |= phyaddr;
570 bcma_write32(core, phy_ctl_addr, tmp);
571
572 tmp = BGMAC_PA_START;
573 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
574 tmp |= reg << BGMAC_PA_REG_SHIFT;
575 bcma_write32(core, phy_access_addr, tmp);
576
577 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
578 bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
579 phyaddr, reg);
580 return 0xffff;
581 }
582
583 return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
584}
585
586/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
587void bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
588{
589 struct bcma_device *core;
590 u16 phy_access_addr;
591 u16 phy_ctl_addr;
592 u32 tmp;
593
594 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
595 core = bgmac->core->bus->drv_gmac_cmn.core;
596 phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
597 phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
598 } else {
599 core = bgmac->core;
600 phy_access_addr = BGMAC_PHY_ACCESS;
601 phy_ctl_addr = BGMAC_PHY_CNTL;
602 }
603
604 tmp = bcma_read32(core, phy_ctl_addr);
605 tmp &= ~BGMAC_PC_EPA_MASK;
606 tmp |= phyaddr;
607 bcma_write32(core, phy_ctl_addr, tmp);
608
609 bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
610 if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
611 bgmac_warn(bgmac, "Error setting MDIO int\n");
612
613 tmp = BGMAC_PA_START;
614 tmp |= BGMAC_PA_WRITE;
615 tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
616 tmp |= reg << BGMAC_PA_REG_SHIFT;
617 tmp |= value;
618 bcma_write32(core, phy_access_addr, tmp);
619
620 if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000))
621 bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
622 phyaddr, reg);
623}
624
625/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyforce */
626static void bgmac_phy_force(struct bgmac *bgmac)
627{
628 u16 ctl;
629 u16 mask = ~(BGMAC_PHY_CTL_SPEED | BGMAC_PHY_CTL_SPEED_MSB |
630 BGMAC_PHY_CTL_ANENAB | BGMAC_PHY_CTL_DUPLEX);
631
632 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
633 return;
634
635 if (bgmac->autoneg)
636 return;
637
638 ctl = bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL);
639 ctl &= mask;
640 if (bgmac->full_duplex)
641 ctl |= BGMAC_PHY_CTL_DUPLEX;
642 if (bgmac->speed == BGMAC_SPEED_100)
643 ctl |= BGMAC_PHY_CTL_SPEED_100;
644 else if (bgmac->speed == BGMAC_SPEED_1000)
645 ctl |= BGMAC_PHY_CTL_SPEED_1000;
646 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL, ctl);
647}
648
649/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyadvertise */
650static void bgmac_phy_advertise(struct bgmac *bgmac)
651{
652 u16 adv;
653
654 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
655 return;
656
657 if (!bgmac->autoneg)
658 return;
659
660 /* Adv selected 10/100 speeds */
661 adv = bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV);
662 adv &= ~(BGMAC_PHY_ADV_10HALF | BGMAC_PHY_ADV_10FULL |
663 BGMAC_PHY_ADV_100HALF | BGMAC_PHY_ADV_100FULL);
664 if (!bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_10)
665 adv |= BGMAC_PHY_ADV_10HALF;
666 if (!bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_100)
667 adv |= BGMAC_PHY_ADV_100HALF;
668 if (bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_10)
669 adv |= BGMAC_PHY_ADV_10FULL;
670 if (bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_100)
671 adv |= BGMAC_PHY_ADV_100FULL;
672 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV, adv);
673
674 /* Adv selected 1000 speeds */
675 adv = bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV2);
676 adv &= ~(BGMAC_PHY_ADV2_1000HALF | BGMAC_PHY_ADV2_1000FULL);
677 if (!bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_1000)
678 adv |= BGMAC_PHY_ADV2_1000HALF;
679 if (bgmac->full_duplex && bgmac->speed & BGMAC_SPEED_1000)
680 adv |= BGMAC_PHY_ADV2_1000FULL;
681 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_ADV2, adv);
682
683 /* Restart */
684 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL,
685 bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL) |
686 BGMAC_PHY_CTL_RESTART);
687}
688
689/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
690static void bgmac_phy_init(struct bgmac *bgmac)
691{
692 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
693 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
694 u8 i;
695
696 if (ci->id == BCMA_CHIP_ID_BCM5356) {
697 for (i = 0; i < 5; i++) {
698 bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
699 bgmac_phy_write(bgmac, i, 0x15, 0x0100);
700 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
701 bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
702 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
703 }
704 }
705 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
706 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
707 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
708 bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
709 bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
710 for (i = 0; i < 5; i++) {
711 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
712 bgmac_phy_write(bgmac, i, 0x16, 0x5284);
713 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
714 bgmac_phy_write(bgmac, i, 0x17, 0x0010);
715 bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
716 bgmac_phy_write(bgmac, i, 0x16, 0x5296);
717 bgmac_phy_write(bgmac, i, 0x17, 0x1073);
718 bgmac_phy_write(bgmac, i, 0x17, 0x9073);
719 bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
720 bgmac_phy_write(bgmac, i, 0x17, 0x9273);
721 bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
722 }
723 }
724}
725
726/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
727static void bgmac_phy_reset(struct bgmac *bgmac)
728{
729 if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
730 return;
731
732 bgmac_phy_write(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL,
733 BGMAC_PHY_CTL_RESET);
734 udelay(100);
735 if (bgmac_phy_read(bgmac, bgmac->phyaddr, BGMAC_PHY_CTL) &
736 BGMAC_PHY_CTL_RESET)
737 bgmac_err(bgmac, "PHY reset failed\n");
738 bgmac_phy_init(bgmac);
739}
740
741/**************************************************
742 * Chip ops
743 **************************************************/
744
745/* TODO: can we just drop @force? Can we don't reset MAC at all if there is
746 * nothing to change? Try if after stabilizng driver.
747 */
748static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
749 bool force)
750{
751 u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
752 u32 new_val = (cmdcfg & mask) | set;
753
754 bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR);
755 udelay(2);
756
757 if (new_val != cmdcfg || force)
758 bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
759
760 bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR);
761 udelay(2);
762}
763
Hauke Mehrtens4e209002013-02-06 04:44:58 +0000764static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
765{
766 u32 tmp;
767
768 tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
769 bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
770 tmp = (addr[4] << 8) | addr[5];
771 bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
772}
773
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +0000774static void bgmac_set_rx_mode(struct net_device *net_dev)
775{
776 struct bgmac *bgmac = netdev_priv(net_dev);
777
778 if (net_dev->flags & IFF_PROMISC)
779 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, false);
780 else
781 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, false);
782}
783
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000784#if 0 /* We don't use that regs yet */
785static void bgmac_chip_stats_update(struct bgmac *bgmac)
786{
787 int i;
788
789 if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
790 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
791 bgmac->mib_tx_regs[i] =
792 bgmac_read(bgmac,
793 BGMAC_TX_GOOD_OCTETS + (i * 4));
794 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
795 bgmac->mib_rx_regs[i] =
796 bgmac_read(bgmac,
797 BGMAC_RX_GOOD_OCTETS + (i * 4));
798 }
799
800 /* TODO: what else? how to handle BCM4706? Specs are needed */
801}
802#endif
803
804static void bgmac_clear_mib(struct bgmac *bgmac)
805{
806 int i;
807
808 if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
809 return;
810
811 bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
812 for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
813 bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
814 for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
815 bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
816}
817
818/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
819static void bgmac_speed(struct bgmac *bgmac, int speed)
820{
821 u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
822 u32 set = 0;
823
824 if (speed & BGMAC_SPEED_10)
825 set |= BGMAC_CMDCFG_ES_10;
826 if (speed & BGMAC_SPEED_100)
827 set |= BGMAC_CMDCFG_ES_100;
828 if (speed & BGMAC_SPEED_1000)
829 set |= BGMAC_CMDCFG_ES_1000;
830 if (!bgmac->full_duplex)
831 set |= BGMAC_CMDCFG_HD;
832 bgmac_cmdcfg_maskset(bgmac, mask, set, true);
833}
834
835static void bgmac_miiconfig(struct bgmac *bgmac)
836{
837 u8 imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
838 BGMAC_DS_MM_SHIFT;
839 if (imode == 0 || imode == 1) {
840 if (bgmac->autoneg)
841 bgmac_speed(bgmac, BGMAC_SPEED_100);
842 else
843 bgmac_speed(bgmac, bgmac->speed);
844 }
845}
846
847/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
848static void bgmac_chip_reset(struct bgmac *bgmac)
849{
850 struct bcma_device *core = bgmac->core;
851 struct bcma_bus *bus = core->bus;
852 struct bcma_chipinfo *ci = &bus->chipinfo;
853 u32 flags = 0;
854 u32 iost;
855 int i;
856
857 if (bcma_core_is_enabled(core)) {
858 if (!bgmac->stats_grabbed) {
859 /* bgmac_chip_stats_update(bgmac); */
860 bgmac->stats_grabbed = true;
861 }
862
863 for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
864 bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
865
866 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
867 udelay(1);
868
869 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
870 bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
871
872 /* TODO: Clear software multicast filter list */
873 }
874
875 iost = bcma_aread32(core, BCMA_IOST);
876 if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == 10) ||
877 (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
878 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == 9))
879 iost &= ~BGMAC_BCMA_IOST_ATTACHED;
880
881 if (iost & BGMAC_BCMA_IOST_ATTACHED) {
882 flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
883 if (!bgmac->has_robosw)
884 flags |= BGMAC_BCMA_IOCTL_SW_RESET;
885 }
886
887 bcma_core_enable(core, flags);
888
889 if (core->id.rev > 2) {
890 bgmac_set(bgmac, BCMA_CLKCTLST, 1 << 8);
891 bgmac_wait_value(bgmac->core, BCMA_CLKCTLST, 1 << 24, 1 << 24,
892 1000);
893 }
894
895 if (ci->id == BCMA_CHIP_ID_BCM5357 || ci->id == BCMA_CHIP_ID_BCM4749 ||
896 ci->id == BCMA_CHIP_ID_BCM53572) {
897 struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
898 u8 et_swtype = 0;
899 u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
900 BGMAC_CHIPCTL_1_IF_TYPE_RMII;
901 char buf[2];
902
903 if (nvram_getenv("et_swtype", buf, 1) > 0) {
904 if (kstrtou8(buf, 0, &et_swtype))
905 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
906 buf);
907 et_swtype &= 0x0f;
908 et_swtype <<= 4;
909 sw_type = et_swtype;
910 } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == 9) {
911 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
Hauke Mehrtensb5a4c2f2013-02-06 04:44:57 +0000912 } else if ((ci->id != BCMA_CHIP_ID_BCM53572 && ci->pkg == 10) ||
913 (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == 9)) {
914 sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
915 BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +0000916 }
917 bcma_chipco_chipctl_maskset(cc, 1,
918 ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
919 BGMAC_CHIPCTL_1_SW_TYPE_MASK),
920 sw_type);
921 }
922
923 if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
924 bcma_awrite32(core, BCMA_IOCTL,
925 bcma_aread32(core, BCMA_IOCTL) &
926 ~BGMAC_BCMA_IOCTL_SW_RESET);
927
928 /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
929 * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
930 * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
931 * be keps until taking MAC out of the reset.
932 */
933 bgmac_cmdcfg_maskset(bgmac,
934 ~(BGMAC_CMDCFG_TE |
935 BGMAC_CMDCFG_RE |
936 BGMAC_CMDCFG_RPI |
937 BGMAC_CMDCFG_TAI |
938 BGMAC_CMDCFG_HD |
939 BGMAC_CMDCFG_ML |
940 BGMAC_CMDCFG_CFE |
941 BGMAC_CMDCFG_RL |
942 BGMAC_CMDCFG_RED |
943 BGMAC_CMDCFG_PE |
944 BGMAC_CMDCFG_TPI |
945 BGMAC_CMDCFG_PAD_EN |
946 BGMAC_CMDCFG_PF),
947 BGMAC_CMDCFG_PROM |
948 BGMAC_CMDCFG_NLC |
949 BGMAC_CMDCFG_CFE |
950 BGMAC_CMDCFG_SR,
951 false);
952
953 bgmac_clear_mib(bgmac);
954 if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
955 bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
956 BCMA_GMAC_CMN_PC_MTE);
957 else
958 bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
959 bgmac_miiconfig(bgmac);
960 bgmac_phy_init(bgmac);
961
962 bgmac->int_status = 0;
963}
964
965static void bgmac_chip_intrs_on(struct bgmac *bgmac)
966{
967 bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
968}
969
970static void bgmac_chip_intrs_off(struct bgmac *bgmac)
971{
972 bgmac_write(bgmac, BGMAC_INT_MASK, 0);
973}
974
975/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
976static void bgmac_enable(struct bgmac *bgmac)
977{
978 struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
979 u32 cmdcfg;
980 u32 mode;
981 u32 rxq_ctl;
982 u32 fl_ctl;
983 u16 bp_clk;
984 u8 mdp;
985
986 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
987 bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
988 BGMAC_CMDCFG_SR, true);
989 udelay(2);
990 cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
991 bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
992
993 mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
994 BGMAC_DS_MM_SHIFT;
995 if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
996 bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
997 if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
998 bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
999 BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
1000
1001 switch (ci->id) {
1002 case BCMA_CHIP_ID_BCM5357:
1003 case BCMA_CHIP_ID_BCM4749:
1004 case BCMA_CHIP_ID_BCM53572:
1005 case BCMA_CHIP_ID_BCM4716:
1006 case BCMA_CHIP_ID_BCM47162:
1007 fl_ctl = 0x03cb04cb;
1008 if (ci->id == BCMA_CHIP_ID_BCM5357 ||
1009 ci->id == BCMA_CHIP_ID_BCM4749 ||
1010 ci->id == BCMA_CHIP_ID_BCM53572)
1011 fl_ctl = 0x2300e1;
1012 bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
1013 bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
1014 break;
1015 }
1016
1017 rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
1018 rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
1019 bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) / 1000000;
1020 mdp = (bp_clk * 128 / 1000) - 3;
1021 rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
1022 bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
1023}
1024
1025/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
1026static void bgmac_chip_init(struct bgmac *bgmac, bool full_init)
1027{
1028 struct bgmac_dma_ring *ring;
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001029 int i;
1030
1031 /* 1 interrupt per received frame */
1032 bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
1033
1034 /* Enable 802.3x tx flow control (honor received PAUSE frames) */
1035 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
1036
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001037 bgmac_set_rx_mode(bgmac->net_dev);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001038
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001039 bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001040
1041 if (bgmac->loopback)
1042 bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, true);
1043 else
1044 bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, true);
1045
1046 bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
1047
1048 if (!bgmac->autoneg) {
1049 bgmac_speed(bgmac, bgmac->speed);
1050 bgmac_phy_force(bgmac);
1051 } else if (bgmac->speed) { /* if there is anything to adv */
1052 bgmac_phy_advertise(bgmac);
1053 }
1054
1055 if (full_init) {
1056 bgmac_dma_init(bgmac);
1057 if (1) /* FIXME: is there any case we don't want IRQs? */
1058 bgmac_chip_intrs_on(bgmac);
1059 } else {
1060 for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
1061 ring = &bgmac->rx_ring[i];
1062 bgmac_dma_rx_enable(bgmac, ring);
1063 }
1064 }
1065
1066 bgmac_enable(bgmac);
1067}
1068
1069static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
1070{
1071 struct bgmac *bgmac = netdev_priv(dev_id);
1072
1073 u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
1074 int_status &= bgmac->int_mask;
1075
1076 if (!int_status)
1077 return IRQ_NONE;
1078
1079 /* Ack */
1080 bgmac_write(bgmac, BGMAC_INT_STATUS, int_status);
1081
1082 /* Disable new interrupts until handling existing ones */
1083 bgmac_chip_intrs_off(bgmac);
1084
1085 bgmac->int_status = int_status;
1086
1087 napi_schedule(&bgmac->napi);
1088
1089 return IRQ_HANDLED;
1090}
1091
1092static int bgmac_poll(struct napi_struct *napi, int weight)
1093{
1094 struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
1095 struct bgmac_dma_ring *ring;
1096 int handled = 0;
1097
1098 if (bgmac->int_status & BGMAC_IS_TX0) {
1099 ring = &bgmac->tx_ring[0];
1100 bgmac_dma_tx_free(bgmac, ring);
1101 bgmac->int_status &= ~BGMAC_IS_TX0;
1102 }
1103
1104 if (bgmac->int_status & BGMAC_IS_RX) {
1105 ring = &bgmac->rx_ring[0];
1106 handled += bgmac_dma_rx_read(bgmac, ring, weight);
1107 bgmac->int_status &= ~BGMAC_IS_RX;
1108 }
1109
1110 if (bgmac->int_status) {
1111 bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", bgmac->int_status);
1112 bgmac->int_status = 0;
1113 }
1114
1115 if (handled < weight)
1116 napi_complete(napi);
1117
1118 bgmac_chip_intrs_on(bgmac);
1119
1120 return handled;
1121}
1122
1123/**************************************************
1124 * net_device_ops
1125 **************************************************/
1126
1127static int bgmac_open(struct net_device *net_dev)
1128{
1129 struct bgmac *bgmac = netdev_priv(net_dev);
1130 int err = 0;
1131
1132 bgmac_chip_reset(bgmac);
1133 /* Specs say about reclaiming rings here, but we do that in DMA init */
1134 bgmac_chip_init(bgmac, true);
1135
1136 err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
1137 KBUILD_MODNAME, net_dev);
1138 if (err < 0) {
1139 bgmac_err(bgmac, "IRQ request error: %d!\n", err);
1140 goto err_out;
1141 }
1142 napi_enable(&bgmac->napi);
1143
1144 netif_carrier_on(net_dev);
1145
1146err_out:
1147 return err;
1148}
1149
1150static int bgmac_stop(struct net_device *net_dev)
1151{
1152 struct bgmac *bgmac = netdev_priv(net_dev);
1153
1154 netif_carrier_off(net_dev);
1155
1156 napi_disable(&bgmac->napi);
1157 bgmac_chip_intrs_off(bgmac);
1158 free_irq(bgmac->core->irq, net_dev);
1159
1160 bgmac_chip_reset(bgmac);
1161
1162 return 0;
1163}
1164
1165static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
1166 struct net_device *net_dev)
1167{
1168 struct bgmac *bgmac = netdev_priv(net_dev);
1169 struct bgmac_dma_ring *ring;
1170
1171 /* No QOS support yet */
1172 ring = &bgmac->tx_ring[0];
1173 return bgmac_dma_tx_add(bgmac, ring, skb);
1174}
1175
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001176static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
1177{
1178 struct bgmac *bgmac = netdev_priv(net_dev);
1179 int ret;
1180
1181 ret = eth_prepare_mac_addr_change(net_dev, addr);
1182 if (ret < 0)
1183 return ret;
1184 bgmac_write_mac_address(bgmac, (u8 *)addr);
1185 eth_commit_mac_addr_change(net_dev, addr);
1186 return 0;
1187}
1188
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001189static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1190{
1191 struct bgmac *bgmac = netdev_priv(net_dev);
1192 struct mii_ioctl_data *data = if_mii(ifr);
1193
1194 switch (cmd) {
1195 case SIOCGMIIPHY:
1196 data->phy_id = bgmac->phyaddr;
1197 /* fallthru */
1198 case SIOCGMIIREG:
1199 if (!netif_running(net_dev))
1200 return -EAGAIN;
1201 data->val_out = bgmac_phy_read(bgmac, data->phy_id,
1202 data->reg_num & 0x1f);
1203 return 0;
1204 case SIOCSMIIREG:
1205 if (!netif_running(net_dev))
1206 return -EAGAIN;
1207 bgmac_phy_write(bgmac, data->phy_id, data->reg_num & 0x1f,
1208 data->val_in);
1209 return 0;
1210 default:
1211 return -EOPNOTSUPP;
1212 }
1213}
1214
1215static const struct net_device_ops bgmac_netdev_ops = {
1216 .ndo_open = bgmac_open,
1217 .ndo_stop = bgmac_stop,
1218 .ndo_start_xmit = bgmac_start_xmit,
Hauke Mehrtensc6edfe12013-02-06 05:51:49 +00001219 .ndo_set_rx_mode = bgmac_set_rx_mode,
Hauke Mehrtens4e209002013-02-06 04:44:58 +00001220 .ndo_set_mac_address = bgmac_set_mac_address,
Hauke Mehrtens522c5902013-02-06 04:44:59 +00001221 .ndo_validate_addr = eth_validate_addr,
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001222 .ndo_do_ioctl = bgmac_ioctl,
1223};
1224
1225/**************************************************
1226 * ethtool_ops
1227 **************************************************/
1228
1229static int bgmac_get_settings(struct net_device *net_dev,
1230 struct ethtool_cmd *cmd)
1231{
1232 struct bgmac *bgmac = netdev_priv(net_dev);
1233
1234 cmd->supported = SUPPORTED_10baseT_Half |
1235 SUPPORTED_10baseT_Full |
1236 SUPPORTED_100baseT_Half |
1237 SUPPORTED_100baseT_Full |
1238 SUPPORTED_1000baseT_Half |
1239 SUPPORTED_1000baseT_Full |
1240 SUPPORTED_Autoneg;
1241
1242 if (bgmac->autoneg) {
1243 WARN_ON(cmd->advertising);
1244 if (bgmac->full_duplex) {
1245 if (bgmac->speed & BGMAC_SPEED_10)
1246 cmd->advertising |= ADVERTISED_10baseT_Full;
1247 if (bgmac->speed & BGMAC_SPEED_100)
1248 cmd->advertising |= ADVERTISED_100baseT_Full;
1249 if (bgmac->speed & BGMAC_SPEED_1000)
1250 cmd->advertising |= ADVERTISED_1000baseT_Full;
1251 } else {
1252 if (bgmac->speed & BGMAC_SPEED_10)
1253 cmd->advertising |= ADVERTISED_10baseT_Half;
1254 if (bgmac->speed & BGMAC_SPEED_100)
1255 cmd->advertising |= ADVERTISED_100baseT_Half;
1256 if (bgmac->speed & BGMAC_SPEED_1000)
1257 cmd->advertising |= ADVERTISED_1000baseT_Half;
1258 }
1259 } else {
1260 switch (bgmac->speed) {
1261 case BGMAC_SPEED_10:
1262 ethtool_cmd_speed_set(cmd, SPEED_10);
1263 break;
1264 case BGMAC_SPEED_100:
1265 ethtool_cmd_speed_set(cmd, SPEED_100);
1266 break;
1267 case BGMAC_SPEED_1000:
1268 ethtool_cmd_speed_set(cmd, SPEED_1000);
1269 break;
1270 }
1271 }
1272
1273 cmd->duplex = bgmac->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
1274
1275 cmd->autoneg = bgmac->autoneg;
1276
1277 return 0;
1278}
1279
1280#if 0
1281static int bgmac_set_settings(struct net_device *net_dev,
1282 struct ethtool_cmd *cmd)
1283{
1284 struct bgmac *bgmac = netdev_priv(net_dev);
1285
1286 return -1;
1287}
1288#endif
1289
1290static void bgmac_get_drvinfo(struct net_device *net_dev,
1291 struct ethtool_drvinfo *info)
1292{
1293 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
1294 strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
1295}
1296
1297static const struct ethtool_ops bgmac_ethtool_ops = {
1298 .get_settings = bgmac_get_settings,
1299 .get_drvinfo = bgmac_get_drvinfo,
1300};
1301
1302/**************************************************
1303 * BCMA bus ops
1304 **************************************************/
1305
1306/* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
1307static int bgmac_probe(struct bcma_device *core)
1308{
1309 struct net_device *net_dev;
1310 struct bgmac *bgmac;
1311 struct ssb_sprom *sprom = &core->bus->sprom;
1312 u8 *mac = core->core_unit ? sprom->et1mac : sprom->et0mac;
1313 int err;
1314
1315 /* We don't support 2nd, 3rd, ... units, SPROM has to be adjusted */
1316 if (core->core_unit > 1) {
1317 pr_err("Unsupported core_unit %d\n", core->core_unit);
1318 return -ENOTSUPP;
1319 }
1320
Rafał Miłeckid166f212013-02-07 00:27:17 +00001321 if (!is_valid_ether_addr(mac)) {
1322 dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
1323 eth_random_addr(mac);
1324 dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
1325 }
1326
Rafał Miłeckidd4544f2013-01-08 20:06:23 +00001327 /* Allocation and references */
1328 net_dev = alloc_etherdev(sizeof(*bgmac));
1329 if (!net_dev)
1330 return -ENOMEM;
1331 net_dev->netdev_ops = &bgmac_netdev_ops;
1332 net_dev->irq = core->irq;
1333 SET_ETHTOOL_OPS(net_dev, &bgmac_ethtool_ops);
1334 bgmac = netdev_priv(net_dev);
1335 bgmac->net_dev = net_dev;
1336 bgmac->core = core;
1337 bcma_set_drvdata(core, bgmac);
1338
1339 /* Defaults */
1340 bgmac->autoneg = true;
1341 bgmac->full_duplex = true;
1342 bgmac->speed = BGMAC_SPEED_10 | BGMAC_SPEED_100 | BGMAC_SPEED_1000;
1343 memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
1344
1345 /* On BCM4706 we need common core to access PHY */
1346 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
1347 !core->bus->drv_gmac_cmn.core) {
1348 bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
1349 err = -ENODEV;
1350 goto err_netdev_free;
1351 }
1352 bgmac->cmn = core->bus->drv_gmac_cmn.core;
1353
1354 bgmac->phyaddr = core->core_unit ? sprom->et1phyaddr :
1355 sprom->et0phyaddr;
1356 bgmac->phyaddr &= BGMAC_PHY_MASK;
1357 if (bgmac->phyaddr == BGMAC_PHY_MASK) {
1358 bgmac_err(bgmac, "No PHY found\n");
1359 err = -ENODEV;
1360 goto err_netdev_free;
1361 }
1362 bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
1363 bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
1364
1365 if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
1366 bgmac_err(bgmac, "PCI setup not implemented\n");
1367 err = -ENOTSUPP;
1368 goto err_netdev_free;
1369 }
1370
1371 bgmac_chip_reset(bgmac);
1372
1373 err = bgmac_dma_alloc(bgmac);
1374 if (err) {
1375 bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
1376 goto err_netdev_free;
1377 }
1378
1379 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
1380 if (nvram_getenv("et0_no_txint", NULL, 0) == 0)
1381 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
1382
1383 /* TODO: reset the external phy. Specs are needed */
1384 bgmac_phy_reset(bgmac);
1385
1386 bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
1387 BGMAC_BFL_ENETROBO);
1388 if (bgmac->has_robosw)
1389 bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
1390
1391 if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
1392 bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
1393
1394 err = register_netdev(bgmac->net_dev);
1395 if (err) {
1396 bgmac_err(bgmac, "Cannot register net device\n");
1397 err = -ENOTSUPP;
1398 goto err_dma_free;
1399 }
1400
1401 netif_carrier_off(net_dev);
1402
1403 netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
1404
1405 return 0;
1406
1407err_dma_free:
1408 bgmac_dma_free(bgmac);
1409
1410err_netdev_free:
1411 bcma_set_drvdata(core, NULL);
1412 free_netdev(net_dev);
1413
1414 return err;
1415}
1416
1417static void bgmac_remove(struct bcma_device *core)
1418{
1419 struct bgmac *bgmac = bcma_get_drvdata(core);
1420
1421 netif_napi_del(&bgmac->napi);
1422 unregister_netdev(bgmac->net_dev);
1423 bgmac_dma_free(bgmac);
1424 bcma_set_drvdata(core, NULL);
1425 free_netdev(bgmac->net_dev);
1426}
1427
1428static struct bcma_driver bgmac_bcma_driver = {
1429 .name = KBUILD_MODNAME,
1430 .id_table = bgmac_bcma_tbl,
1431 .probe = bgmac_probe,
1432 .remove = bgmac_remove,
1433};
1434
1435static int __init bgmac_init(void)
1436{
1437 int err;
1438
1439 err = bcma_driver_register(&bgmac_bcma_driver);
1440 if (err)
1441 return err;
1442 pr_info("Broadcom 47xx GBit MAC driver loaded\n");
1443
1444 return 0;
1445}
1446
1447static void __exit bgmac_exit(void)
1448{
1449 bcma_driver_unregister(&bgmac_bcma_driver);
1450}
1451
1452module_init(bgmac_init)
1453module_exit(bgmac_exit)
1454
1455MODULE_AUTHOR("Rafał Miłecki");
1456MODULE_LICENSE("GPL");