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