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