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