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