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 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
| 12 | #include <linux/bcma/bcma.h> |
| 13 | #include <linux/etherdevice.h> |
Florian Westphal | 282ccf6 | 2017-03-29 17:17:31 +0200 | [diff] [blame] | 14 | #include <linux/interrupt.h> |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 15 | #include <linux/bcm47xx_nvram.h> |
Russell King | 13bf776 | 2017-02-07 15:02:58 -0800 | [diff] [blame] | 16 | #include <linux/phy.h> |
| 17 | #include <linux/phy_fixed.h> |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 18 | #include "bgmac.h" |
| 19 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 20 | static bool bgmac_wait_value(struct bgmac *bgmac, u16 reg, u32 mask, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 21 | u32 value, int timeout) |
| 22 | { |
| 23 | u32 val; |
| 24 | int i; |
| 25 | |
| 26 | for (i = 0; i < timeout / 10; i++) { |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 27 | val = bgmac_read(bgmac, reg); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 28 | if ((val & mask) == value) |
| 29 | return true; |
| 30 | udelay(10); |
| 31 | } |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 32 | dev_err(bgmac->dev, "Timeout waiting for reg 0x%X\n", reg); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 33 | return false; |
| 34 | } |
| 35 | |
| 36 | /************************************************** |
| 37 | * DMA |
| 38 | **************************************************/ |
| 39 | |
| 40 | static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring) |
| 41 | { |
| 42 | u32 val; |
| 43 | int i; |
| 44 | |
| 45 | if (!ring->mmio_base) |
| 46 | return; |
| 47 | |
| 48 | /* Suspend DMA TX ring first. |
| 49 | * bgmac_wait_value doesn't support waiting for any of few values, so |
| 50 | * implement whole loop here. |
| 51 | */ |
| 52 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, |
| 53 | BGMAC_DMA_TX_SUSPEND); |
| 54 | for (i = 0; i < 10000 / 10; i++) { |
| 55 | val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS); |
| 56 | val &= BGMAC_DMA_TX_STAT; |
| 57 | if (val == BGMAC_DMA_TX_STAT_DISABLED || |
| 58 | val == BGMAC_DMA_TX_STAT_IDLEWAIT || |
| 59 | val == BGMAC_DMA_TX_STAT_STOPPED) { |
| 60 | i = 0; |
| 61 | break; |
| 62 | } |
| 63 | udelay(10); |
| 64 | } |
| 65 | if (i) |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 66 | dev_err(bgmac->dev, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n", |
| 67 | ring->mmio_base, val); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 68 | |
| 69 | /* Remove SUSPEND bit */ |
| 70 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 71 | if (!bgmac_wait_value(bgmac, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 72 | ring->mmio_base + BGMAC_DMA_TX_STATUS, |
| 73 | BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED, |
| 74 | 10000)) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 75 | dev_warn(bgmac->dev, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n", |
| 76 | ring->mmio_base); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 77 | udelay(300); |
| 78 | val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS); |
| 79 | if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED) |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 80 | dev_err(bgmac->dev, "Reset of DMA TX ring 0x%X failed\n", |
| 81 | ring->mmio_base); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | static void bgmac_dma_tx_enable(struct bgmac *bgmac, |
| 86 | struct bgmac_dma_ring *ring) |
| 87 | { |
| 88 | u32 ctl; |
| 89 | |
| 90 | ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL); |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 91 | if (bgmac->feature_flags & BGMAC_FEAT_TX_MASK_SETUP) { |
Hauke Mehrtens | 56ceecd | 2014-01-05 01:10:44 +0100 | [diff] [blame] | 92 | ctl &= ~BGMAC_DMA_TX_BL_MASK; |
| 93 | ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT; |
| 94 | |
| 95 | ctl &= ~BGMAC_DMA_TX_MR_MASK; |
| 96 | ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT; |
| 97 | |
| 98 | ctl &= ~BGMAC_DMA_TX_PC_MASK; |
| 99 | ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT; |
| 100 | |
| 101 | ctl &= ~BGMAC_DMA_TX_PT_MASK; |
| 102 | ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT; |
| 103 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 104 | ctl |= BGMAC_DMA_TX_ENABLE; |
| 105 | ctl |= BGMAC_DMA_TX_PARITY_DISABLE; |
| 106 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl); |
| 107 | } |
| 108 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 109 | static void |
| 110 | bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring, |
| 111 | int i, int len, u32 ctl0) |
| 112 | { |
| 113 | struct bgmac_slot_info *slot; |
| 114 | struct bgmac_dma_desc *dma_desc; |
| 115 | u32 ctl1; |
| 116 | |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 117 | if (i == BGMAC_TX_RING_SLOTS - 1) |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 118 | ctl0 |= BGMAC_DESC_CTL0_EOT; |
| 119 | |
| 120 | ctl1 = len & BGMAC_DESC_CTL1_LEN; |
| 121 | |
| 122 | slot = &ring->slots[i]; |
| 123 | dma_desc = &ring->cpu_base[i]; |
| 124 | dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr)); |
| 125 | dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr)); |
| 126 | dma_desc->ctl0 = cpu_to_le32(ctl0); |
| 127 | dma_desc->ctl1 = cpu_to_le32(ctl1); |
| 128 | } |
| 129 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 130 | static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac, |
| 131 | struct bgmac_dma_ring *ring, |
| 132 | struct sk_buff *skb) |
| 133 | { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 134 | struct device *dma_dev = bgmac->dma_dev; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 135 | struct net_device *net_dev = bgmac->net_dev; |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 136 | int index = ring->end % BGMAC_TX_RING_SLOTS; |
| 137 | struct bgmac_slot_info *slot = &ring->slots[index]; |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 138 | int nr_frags; |
| 139 | u32 flags; |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 140 | int i; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 141 | |
| 142 | if (skb->len > BGMAC_DESC_CTL1_LEN) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 143 | netdev_err(bgmac->net_dev, "Too long skb (%d)\n", skb->len); |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 144 | goto err_drop; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 147 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
| 148 | skb_checksum_help(skb); |
| 149 | |
| 150 | nr_frags = skb_shinfo(skb)->nr_frags; |
| 151 | |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 152 | /* ring->end - ring->start will return the number of valid slots, |
| 153 | * even when ring->end overflows |
| 154 | */ |
| 155 | if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 156 | netdev_err(bgmac->net_dev, "TX ring is full, queue should be stopped!\n"); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 157 | netif_stop_queue(net_dev); |
| 158 | return NETDEV_TX_BUSY; |
| 159 | } |
| 160 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 161 | 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] | 162 | DMA_TO_DEVICE); |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 163 | if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr))) |
| 164 | goto err_dma_head; |
| 165 | |
| 166 | flags = BGMAC_DESC_CTL0_SOF; |
| 167 | if (!nr_frags) |
| 168 | flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC; |
| 169 | |
| 170 | bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags); |
| 171 | flags = 0; |
| 172 | |
| 173 | for (i = 0; i < nr_frags; i++) { |
| 174 | struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i]; |
| 175 | int len = skb_frag_size(frag); |
| 176 | |
| 177 | index = (index + 1) % BGMAC_TX_RING_SLOTS; |
| 178 | slot = &ring->slots[index]; |
| 179 | slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0, |
| 180 | len, DMA_TO_DEVICE); |
| 181 | if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr))) |
| 182 | goto err_dma; |
| 183 | |
| 184 | if (i == nr_frags - 1) |
| 185 | flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC; |
| 186 | |
| 187 | bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 190 | slot->skb = skb; |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 191 | ring->end += nr_frags + 1; |
Hauke Mehrtens | 49a467b | 2013-09-29 13:54:58 +0200 | [diff] [blame] | 192 | netdev_sent_queue(net_dev, skb->len); |
| 193 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 194 | wmb(); |
| 195 | |
| 196 | /* Increase ring->end to point empty slot. We tell hardware the first |
| 197 | * slot it should *not* read. |
| 198 | */ |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 199 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX, |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 200 | ring->index_base + |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 201 | (ring->end % BGMAC_TX_RING_SLOTS) * |
| 202 | sizeof(struct bgmac_dma_desc)); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 203 | |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 204 | if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 205 | netif_stop_queue(net_dev); |
| 206 | |
| 207 | return NETDEV_TX_OK; |
| 208 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 209 | err_dma: |
| 210 | dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb), |
| 211 | DMA_TO_DEVICE); |
| 212 | |
Florian Fainelli | e86663c | 2016-07-15 15:42:52 -0700 | [diff] [blame] | 213 | while (i-- > 0) { |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 214 | int index = (ring->end + i) % BGMAC_TX_RING_SLOTS; |
| 215 | struct bgmac_slot_info *slot = &ring->slots[index]; |
| 216 | u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1); |
| 217 | int len = ctl1 & BGMAC_DESC_CTL1_LEN; |
| 218 | |
| 219 | dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE); |
| 220 | } |
| 221 | |
| 222 | err_dma_head: |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 223 | netdev_err(bgmac->net_dev, "Mapping error of skb on ring 0x%X\n", |
| 224 | ring->mmio_base); |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 225 | |
| 226 | err_drop: |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 227 | dev_kfree_skb(skb); |
Florian Fainelli | 6d490f62 | 2016-06-07 15:06:15 -0700 | [diff] [blame] | 228 | net_dev->stats.tx_dropped++; |
| 229 | net_dev->stats.tx_errors++; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 230 | return NETDEV_TX_OK; |
| 231 | } |
| 232 | |
| 233 | /* Free transmitted packets */ |
| 234 | static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring) |
| 235 | { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 236 | struct device *dma_dev = bgmac->dma_dev; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 237 | int empty_slot; |
| 238 | bool freed = false; |
Hauke Mehrtens | 49a467b | 2013-09-29 13:54:58 +0200 | [diff] [blame] | 239 | unsigned bytes_compl = 0, pkts_compl = 0; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 240 | |
| 241 | /* The last slot that hardware didn't consume yet */ |
| 242 | empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS); |
| 243 | empty_slot &= BGMAC_DMA_TX_STATDPTR; |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 244 | empty_slot -= ring->index_base; |
| 245 | empty_slot &= BGMAC_DMA_TX_STATDPTR; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 246 | empty_slot /= sizeof(struct bgmac_dma_desc); |
| 247 | |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 248 | while (ring->start != ring->end) { |
| 249 | int slot_idx = ring->start % BGMAC_TX_RING_SLOTS; |
| 250 | struct bgmac_slot_info *slot = &ring->slots[slot_idx]; |
Florian Fainelli | d2b1323 | 2016-06-23 14:23:12 -0700 | [diff] [blame] | 251 | u32 ctl0, ctl1; |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 252 | int len; |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 253 | |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 254 | if (slot_idx == empty_slot) |
| 255 | break; |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 256 | |
Florian Fainelli | d2b1323 | 2016-06-23 14:23:12 -0700 | [diff] [blame] | 257 | ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0); |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 258 | ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1); |
| 259 | len = ctl1 & BGMAC_DESC_CTL1_LEN; |
Florian Fainelli | d2b1323 | 2016-06-23 14:23:12 -0700 | [diff] [blame] | 260 | if (ctl0 & BGMAC_DESC_CTL0_SOF) |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 261 | /* Unmap no longer used buffer */ |
| 262 | dma_unmap_single(dma_dev, slot->dma_addr, len, |
| 263 | DMA_TO_DEVICE); |
| 264 | else |
| 265 | dma_unmap_page(dma_dev, slot->dma_addr, len, |
| 266 | DMA_TO_DEVICE); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 267 | |
| 268 | if (slot->skb) { |
Florian Fainelli | 6d490f62 | 2016-06-07 15:06:15 -0700 | [diff] [blame] | 269 | bgmac->net_dev->stats.tx_bytes += slot->skb->len; |
| 270 | bgmac->net_dev->stats.tx_packets++; |
Hauke Mehrtens | 49a467b | 2013-09-29 13:54:58 +0200 | [diff] [blame] | 271 | bytes_compl += slot->skb->len; |
| 272 | pkts_compl++; |
| 273 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 274 | /* Free memory! :) */ |
| 275 | dev_kfree_skb(slot->skb); |
| 276 | slot->skb = NULL; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 279 | slot->dma_addr = 0; |
Felix Fietkau | b38c83d | 2015-04-14 12:07:54 +0200 | [diff] [blame] | 280 | ring->start++; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 281 | freed = true; |
| 282 | } |
| 283 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 284 | if (!pkts_compl) |
| 285 | return; |
| 286 | |
Hauke Mehrtens | 49a467b | 2013-09-29 13:54:58 +0200 | [diff] [blame] | 287 | netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl); |
| 288 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 289 | if (netif_queue_stopped(bgmac->net_dev)) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 290 | netif_wake_queue(bgmac->net_dev); |
| 291 | } |
| 292 | |
| 293 | static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring) |
| 294 | { |
| 295 | if (!ring->mmio_base) |
| 296 | return; |
| 297 | |
| 298 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 299 | if (!bgmac_wait_value(bgmac, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 300 | ring->mmio_base + BGMAC_DMA_RX_STATUS, |
| 301 | BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED, |
| 302 | 10000)) |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 303 | dev_err(bgmac->dev, "Reset of ring 0x%X RX failed\n", |
| 304 | ring->mmio_base); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static void bgmac_dma_rx_enable(struct bgmac *bgmac, |
| 308 | struct bgmac_dma_ring *ring) |
| 309 | { |
| 310 | u32 ctl; |
| 311 | |
| 312 | ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL); |
Andy Gospodarek | fcdefcc | 2016-10-31 13:32:03 -0400 | [diff] [blame] | 313 | |
| 314 | /* preserve ONLY bits 16-17 from current hardware value */ |
| 315 | ctl &= BGMAC_DMA_RX_ADDREXT_MASK; |
| 316 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 317 | if (bgmac->feature_flags & BGMAC_FEAT_RX_MASK_SETUP) { |
Hauke Mehrtens | 56ceecd | 2014-01-05 01:10:44 +0100 | [diff] [blame] | 318 | ctl &= ~BGMAC_DMA_RX_BL_MASK; |
| 319 | ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT; |
| 320 | |
| 321 | ctl &= ~BGMAC_DMA_RX_PC_MASK; |
| 322 | ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT; |
| 323 | |
| 324 | ctl &= ~BGMAC_DMA_RX_PT_MASK; |
| 325 | ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT; |
| 326 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 327 | ctl |= BGMAC_DMA_RX_ENABLE; |
| 328 | ctl |= BGMAC_DMA_RX_PARITY_DISABLE; |
| 329 | ctl |= BGMAC_DMA_RX_OVERFLOW_CONT; |
| 330 | ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT; |
| 331 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl); |
| 332 | } |
| 333 | |
| 334 | static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac, |
| 335 | struct bgmac_slot_info *slot) |
| 336 | { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 337 | struct device *dma_dev = bgmac->dma_dev; |
Nathan Hintz | b757a62 | 2013-10-29 19:32:01 -0700 | [diff] [blame] | 338 | dma_addr_t dma_addr; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 339 | struct bgmac_rx_header *rx; |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 340 | void *buf; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 341 | |
| 342 | /* Alloc skb */ |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 343 | buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE); |
| 344 | if (!buf) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 345 | return -ENOMEM; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 346 | |
| 347 | /* Poison - if everything goes fine, hardware will overwrite it */ |
Felix Fietkau | 4b62dce | 2015-04-14 12:07:56 +0200 | [diff] [blame] | 348 | rx = buf + BGMAC_RX_BUF_OFFSET; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 349 | rx->len = cpu_to_le16(0xdead); |
| 350 | rx->flags = cpu_to_le16(0xbeef); |
| 351 | |
| 352 | /* Map skb for the DMA */ |
Felix Fietkau | 4b62dce | 2015-04-14 12:07:56 +0200 | [diff] [blame] | 353 | dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET, |
| 354 | BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE); |
Nathan Hintz | b757a62 | 2013-10-29 19:32:01 -0700 | [diff] [blame] | 355 | if (dma_mapping_error(dma_dev, dma_addr)) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 356 | netdev_err(bgmac->net_dev, "DMA mapping error\n"); |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 357 | put_page(virt_to_head_page(buf)); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 358 | return -ENOMEM; |
| 359 | } |
Nathan Hintz | b757a62 | 2013-10-29 19:32:01 -0700 | [diff] [blame] | 360 | |
| 361 | /* Update the slot */ |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 362 | slot->buf = buf; |
Nathan Hintz | b757a62 | 2013-10-29 19:32:01 -0700 | [diff] [blame] | 363 | slot->dma_addr = dma_addr; |
| 364 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 365 | return 0; |
| 366 | } |
| 367 | |
Felix Fietkau | 4668ae1 | 2015-04-14 12:08:01 +0200 | [diff] [blame] | 368 | static void bgmac_dma_rx_update_index(struct bgmac *bgmac, |
| 369 | struct bgmac_dma_ring *ring) |
| 370 | { |
| 371 | dma_wmb(); |
| 372 | |
| 373 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX, |
| 374 | ring->index_base + |
| 375 | ring->end * sizeof(struct bgmac_dma_desc)); |
| 376 | } |
| 377 | |
Rafał Miłecki | d549c76b | 2013-10-28 14:40:29 +0100 | [diff] [blame] | 378 | static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac, |
| 379 | struct bgmac_dma_ring *ring, int desc_idx) |
| 380 | { |
| 381 | struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx; |
| 382 | u32 ctl0 = 0, ctl1 = 0; |
| 383 | |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 384 | if (desc_idx == BGMAC_RX_RING_SLOTS - 1) |
Rafał Miłecki | d549c76b | 2013-10-28 14:40:29 +0100 | [diff] [blame] | 385 | ctl0 |= BGMAC_DESC_CTL0_EOT; |
| 386 | ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN; |
| 387 | /* Is there any BGMAC device that requires extension? */ |
| 388 | /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) & |
| 389 | * B43_DMA64_DCTL1_ADDREXT_MASK; |
| 390 | */ |
| 391 | |
| 392 | dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr)); |
| 393 | dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr)); |
| 394 | dma_desc->ctl0 = cpu_to_le32(ctl0); |
| 395 | dma_desc->ctl1 = cpu_to_le32(ctl1); |
Felix Fietkau | 4668ae1 | 2015-04-14 12:08:01 +0200 | [diff] [blame] | 396 | |
| 397 | ring->end = desc_idx; |
Rafał Miłecki | d549c76b | 2013-10-28 14:40:29 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 400 | static void bgmac_dma_rx_poison_buf(struct device *dma_dev, |
| 401 | struct bgmac_slot_info *slot) |
| 402 | { |
| 403 | struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET; |
| 404 | |
| 405 | dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE, |
| 406 | DMA_FROM_DEVICE); |
| 407 | rx->len = cpu_to_le16(0xdead); |
| 408 | rx->flags = cpu_to_le16(0xbeef); |
| 409 | dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE, |
| 410 | DMA_FROM_DEVICE); |
| 411 | } |
| 412 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 413 | static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring, |
| 414 | int weight) |
| 415 | { |
| 416 | u32 end_slot; |
| 417 | int handled = 0; |
| 418 | |
| 419 | end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS); |
| 420 | end_slot &= BGMAC_DMA_RX_STATDPTR; |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 421 | end_slot -= ring->index_base; |
| 422 | end_slot &= BGMAC_DMA_RX_STATDPTR; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 423 | end_slot /= sizeof(struct bgmac_dma_desc); |
| 424 | |
Felix Fietkau | 4668ae1 | 2015-04-14 12:08:01 +0200 | [diff] [blame] | 425 | while (ring->start != end_slot) { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 426 | struct device *dma_dev = bgmac->dma_dev; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 427 | struct bgmac_slot_info *slot = &ring->slots[ring->start]; |
Felix Fietkau | 4b62dce | 2015-04-14 12:07:56 +0200 | [diff] [blame] | 428 | struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET; |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 429 | struct sk_buff *skb; |
| 430 | void *buf = slot->buf; |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 431 | dma_addr_t dma_addr = slot->dma_addr; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 432 | u16 len, flags; |
| 433 | |
Rafał Miłecki | 92b9ccd3 | 2013-10-30 08:00:00 +0100 | [diff] [blame] | 434 | do { |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 435 | /* Prepare new skb as replacement */ |
| 436 | if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) { |
| 437 | bgmac_dma_rx_poison_buf(dma_dev, slot); |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | /* Unmap buffer to make it accessible to the CPU */ |
| 442 | dma_unmap_single(dma_dev, dma_addr, |
| 443 | BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE); |
| 444 | |
| 445 | /* Get info from the header */ |
| 446 | len = le16_to_cpu(rx->len); |
| 447 | flags = le16_to_cpu(rx->flags); |
Rafał Miłecki | 92b9ccd3 | 2013-10-30 08:00:00 +0100 | [diff] [blame] | 448 | |
| 449 | /* Check for poison and drop or pass the packet */ |
| 450 | if (len == 0xdead && flags == 0xbeef) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 451 | netdev_err(bgmac->net_dev, "Found poisoned packet at slot %d, DMA issue!\n", |
| 452 | ring->start); |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 453 | put_page(virt_to_head_page(buf)); |
Florian Fainelli | 6d490f62 | 2016-06-07 15:06:15 -0700 | [diff] [blame] | 454 | bgmac->net_dev->stats.rx_errors++; |
Rafał Miłecki | 92b9ccd3 | 2013-10-30 08:00:00 +0100 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | |
Felix Fietkau | 6a6c708 | 2015-04-14 12:07:58 +0200 | [diff] [blame] | 458 | if (len > BGMAC_RX_ALLOC_SIZE) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 459 | netdev_err(bgmac->net_dev, "Found oversized packet at slot %d, DMA issue!\n", |
| 460 | ring->start); |
Felix Fietkau | 6a6c708 | 2015-04-14 12:07:58 +0200 | [diff] [blame] | 461 | put_page(virt_to_head_page(buf)); |
Florian Fainelli | 6d490f62 | 2016-06-07 15:06:15 -0700 | [diff] [blame] | 462 | bgmac->net_dev->stats.rx_length_errors++; |
| 463 | bgmac->net_dev->stats.rx_errors++; |
Felix Fietkau | 6a6c708 | 2015-04-14 12:07:58 +0200 | [diff] [blame] | 464 | break; |
| 465 | } |
| 466 | |
Hauke Mehrtens | 02e7112 | 2013-02-28 07:16:54 +0000 | [diff] [blame] | 467 | /* Omit CRC. */ |
| 468 | len -= ETH_FCS_LEN; |
| 469 | |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 470 | skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE); |
David S. Miller | 750afbf | 2016-01-15 16:07:13 -0500 | [diff] [blame] | 471 | if (unlikely(!skb)) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 472 | netdev_err(bgmac->net_dev, "build_skb failed\n"); |
wangweidong | f1640c3 | 2016-01-13 11:06:41 +0800 | [diff] [blame] | 473 | put_page(virt_to_head_page(buf)); |
Florian Fainelli | 6d490f62 | 2016-06-07 15:06:15 -0700 | [diff] [blame] | 474 | bgmac->net_dev->stats.rx_errors++; |
wangweidong | f1640c3 | 2016-01-13 11:06:41 +0800 | [diff] [blame] | 475 | break; |
| 476 | } |
Felix Fietkau | 4b62dce | 2015-04-14 12:07:56 +0200 | [diff] [blame] | 477 | skb_put(skb, BGMAC_RX_FRAME_OFFSET + |
| 478 | BGMAC_RX_BUF_OFFSET + len); |
| 479 | skb_pull(skb, BGMAC_RX_FRAME_OFFSET + |
| 480 | BGMAC_RX_BUF_OFFSET); |
Rafał Miłecki | 92b9ccd3 | 2013-10-30 08:00:00 +0100 | [diff] [blame] | 481 | |
| 482 | skb_checksum_none_assert(skb); |
| 483 | skb->protocol = eth_type_trans(skb, bgmac->net_dev); |
Florian Fainelli | 6d490f62 | 2016-06-07 15:06:15 -0700 | [diff] [blame] | 484 | bgmac->net_dev->stats.rx_bytes += len; |
| 485 | bgmac->net_dev->stats.rx_packets++; |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 486 | napi_gro_receive(&bgmac->napi, skb); |
Rafał Miłecki | 92b9ccd3 | 2013-10-30 08:00:00 +0100 | [diff] [blame] | 487 | handled++; |
| 488 | } while (0); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 489 | |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 490 | bgmac_dma_rx_setup_desc(bgmac, ring, ring->start); |
| 491 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 492 | if (++ring->start >= BGMAC_RX_RING_SLOTS) |
| 493 | ring->start = 0; |
| 494 | |
| 495 | if (handled >= weight) /* Should never be greater */ |
| 496 | break; |
| 497 | } |
| 498 | |
Felix Fietkau | 4668ae1 | 2015-04-14 12:08:01 +0200 | [diff] [blame] | 499 | bgmac_dma_rx_update_index(bgmac, ring); |
| 500 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 501 | return handled; |
| 502 | } |
| 503 | |
| 504 | /* Does ring support unaligned addressing? */ |
| 505 | static bool bgmac_dma_unaligned(struct bgmac *bgmac, |
| 506 | struct bgmac_dma_ring *ring, |
| 507 | enum bgmac_dma_ring_type ring_type) |
| 508 | { |
| 509 | switch (ring_type) { |
| 510 | case BGMAC_DMA_RING_TX: |
| 511 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO, |
| 512 | 0xff0); |
| 513 | if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO)) |
| 514 | return true; |
| 515 | break; |
| 516 | case BGMAC_DMA_RING_RX: |
| 517 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO, |
| 518 | 0xff0); |
| 519 | if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO)) |
| 520 | return true; |
| 521 | break; |
| 522 | } |
| 523 | return false; |
| 524 | } |
| 525 | |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 526 | static void bgmac_dma_tx_ring_free(struct bgmac *bgmac, |
| 527 | struct bgmac_dma_ring *ring) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 528 | { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 529 | struct device *dma_dev = bgmac->dma_dev; |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 530 | struct bgmac_dma_desc *dma_desc = ring->cpu_base; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 531 | struct bgmac_slot_info *slot; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 532 | int i; |
| 533 | |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 534 | for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) { |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 535 | int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN; |
| 536 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 537 | slot = &ring->slots[i]; |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 538 | dev_kfree_skb(slot->skb); |
| 539 | |
| 540 | if (!slot->dma_addr) |
| 541 | continue; |
| 542 | |
| 543 | if (slot->skb) |
| 544 | dma_unmap_single(dma_dev, slot->dma_addr, |
| 545 | len, DMA_TO_DEVICE); |
| 546 | else |
| 547 | dma_unmap_page(dma_dev, slot->dma_addr, |
| 548 | len, DMA_TO_DEVICE); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 549 | } |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 550 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 551 | |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 552 | static void bgmac_dma_rx_ring_free(struct bgmac *bgmac, |
| 553 | struct bgmac_dma_ring *ring) |
| 554 | { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 555 | struct device *dma_dev = bgmac->dma_dev; |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 556 | struct bgmac_slot_info *slot; |
| 557 | int i; |
| 558 | |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 559 | for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) { |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 560 | slot = &ring->slots[i]; |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 561 | if (!slot->dma_addr) |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 562 | continue; |
| 563 | |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 564 | dma_unmap_single(dma_dev, slot->dma_addr, |
| 565 | BGMAC_RX_BUF_SIZE, |
| 566 | DMA_FROM_DEVICE); |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 567 | put_page(virt_to_head_page(slot->buf)); |
Felix Fietkau | 56faacd | 2015-04-14 12:07:57 +0200 | [diff] [blame] | 568 | slot->dma_addr = 0; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 572 | static void bgmac_dma_ring_desc_free(struct bgmac *bgmac, |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 573 | struct bgmac_dma_ring *ring, |
| 574 | int num_slots) |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 575 | { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 576 | struct device *dma_dev = bgmac->dma_dev; |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 577 | int size; |
| 578 | |
| 579 | if (!ring->cpu_base) |
| 580 | return; |
| 581 | |
| 582 | /* Free ring of descriptors */ |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 583 | size = num_slots * sizeof(struct bgmac_dma_desc); |
Felix Fietkau | 45c9b3c | 2015-03-23 12:35:36 +0100 | [diff] [blame] | 584 | dma_free_coherent(dma_dev, size, ring->cpu_base, |
| 585 | ring->dma_base); |
| 586 | } |
| 587 | |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 588 | static void bgmac_dma_cleanup(struct bgmac *bgmac) |
| 589 | { |
| 590 | int i; |
| 591 | |
| 592 | for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) |
| 593 | bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]); |
| 594 | |
| 595 | for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) |
| 596 | bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]); |
| 597 | } |
| 598 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 599 | static void bgmac_dma_free(struct bgmac *bgmac) |
| 600 | { |
| 601 | int i; |
| 602 | |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 603 | for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 604 | bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i], |
| 605 | BGMAC_TX_RING_SLOTS); |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 606 | |
| 607 | for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 608 | bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i], |
| 609 | BGMAC_RX_RING_SLOTS); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | static int bgmac_dma_alloc(struct bgmac *bgmac) |
| 613 | { |
Jon Mason | a0b6848 | 2016-07-07 19:08:54 -0400 | [diff] [blame] | 614 | struct device *dma_dev = bgmac->dma_dev; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 615 | struct bgmac_dma_ring *ring; |
| 616 | static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1, |
| 617 | BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, }; |
| 618 | int size; /* ring size: different for Tx and Rx */ |
| 619 | int err; |
| 620 | int i; |
| 621 | |
| 622 | BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base)); |
| 623 | BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base)); |
| 624 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 625 | if (!(bgmac_idm_read(bgmac, BCMA_IOST) & BCMA_IOST_DMA64)) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 626 | dev_err(bgmac->dev, "Core does not report 64-bit DMA\n"); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 627 | return -ENOTSUPP; |
| 628 | } |
| 629 | |
| 630 | for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) { |
| 631 | ring = &bgmac->tx_ring[i]; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 632 | ring->mmio_base = ring_base[i]; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 633 | |
| 634 | /* Alloc ring of descriptors */ |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 635 | size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 636 | ring->cpu_base = dma_zalloc_coherent(dma_dev, size, |
| 637 | &ring->dma_base, |
| 638 | GFP_KERNEL); |
| 639 | if (!ring->cpu_base) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 640 | dev_err(bgmac->dev, "Allocation of TX ring 0x%X failed\n", |
| 641 | ring->mmio_base); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 642 | goto err_dma_free; |
| 643 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 644 | |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 645 | ring->unaligned = bgmac_dma_unaligned(bgmac, ring, |
| 646 | BGMAC_DMA_RING_TX); |
| 647 | if (ring->unaligned) |
| 648 | ring->index_base = lower_32_bits(ring->dma_base); |
| 649 | else |
| 650 | ring->index_base = 0; |
| 651 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 652 | /* No need to alloc TX slots yet */ |
| 653 | } |
| 654 | |
| 655 | for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) { |
| 656 | ring = &bgmac->rx_ring[i]; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 657 | ring->mmio_base = ring_base[i]; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 658 | |
| 659 | /* Alloc ring of descriptors */ |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 660 | size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 661 | ring->cpu_base = dma_zalloc_coherent(dma_dev, size, |
| 662 | &ring->dma_base, |
| 663 | GFP_KERNEL); |
| 664 | if (!ring->cpu_base) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 665 | dev_err(bgmac->dev, "Allocation of RX ring 0x%X failed\n", |
| 666 | ring->mmio_base); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 667 | err = -ENOMEM; |
| 668 | goto err_dma_free; |
| 669 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 670 | |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 671 | ring->unaligned = bgmac_dma_unaligned(bgmac, ring, |
| 672 | BGMAC_DMA_RING_RX); |
| 673 | if (ring->unaligned) |
| 674 | ring->index_base = lower_32_bits(ring->dma_base); |
| 675 | else |
| 676 | ring->index_base = 0; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | return 0; |
| 680 | |
| 681 | err_dma_free: |
| 682 | bgmac_dma_free(bgmac); |
| 683 | return -ENOMEM; |
| 684 | } |
| 685 | |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 686 | static int bgmac_dma_init(struct bgmac *bgmac) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 687 | { |
| 688 | struct bgmac_dma_ring *ring; |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 689 | int i, err; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 690 | |
| 691 | for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) { |
| 692 | ring = &bgmac->tx_ring[i]; |
| 693 | |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 694 | if (!ring->unaligned) |
| 695 | bgmac_dma_tx_enable(bgmac, ring); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 696 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO, |
| 697 | lower_32_bits(ring->dma_base)); |
| 698 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI, |
| 699 | upper_32_bits(ring->dma_base)); |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 700 | if (ring->unaligned) |
| 701 | bgmac_dma_tx_enable(bgmac, ring); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 702 | |
| 703 | ring->start = 0; |
| 704 | ring->end = 0; /* Points the slot that should *not* be read */ |
| 705 | } |
| 706 | |
| 707 | for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) { |
Rafał Miłecki | 70a737b | 2013-02-25 08:22:26 +0000 | [diff] [blame] | 708 | int j; |
| 709 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 710 | ring = &bgmac->rx_ring[i]; |
| 711 | |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 712 | if (!ring->unaligned) |
| 713 | bgmac_dma_rx_enable(bgmac, ring); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 714 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO, |
| 715 | lower_32_bits(ring->dma_base)); |
| 716 | bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI, |
| 717 | upper_32_bits(ring->dma_base)); |
Rafał Miłecki | 9900303 | 2013-09-15 23:13:18 +0200 | [diff] [blame] | 718 | if (ring->unaligned) |
| 719 | bgmac_dma_rx_enable(bgmac, ring); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 720 | |
Felix Fietkau | 4668ae1 | 2015-04-14 12:08:01 +0200 | [diff] [blame] | 721 | ring->start = 0; |
| 722 | ring->end = 0; |
Felix Fietkau | 29ba877 | 2015-04-14 12:08:02 +0200 | [diff] [blame] | 723 | for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) { |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 724 | err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]); |
| 725 | if (err) |
| 726 | goto error; |
| 727 | |
Rafał Miłecki | d549c76b | 2013-10-28 14:40:29 +0100 | [diff] [blame] | 728 | bgmac_dma_rx_setup_desc(bgmac, ring, j); |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 729 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 730 | |
Felix Fietkau | 4668ae1 | 2015-04-14 12:08:01 +0200 | [diff] [blame] | 731 | bgmac_dma_rx_update_index(bgmac, ring); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 732 | } |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 733 | |
| 734 | return 0; |
| 735 | |
| 736 | error: |
| 737 | bgmac_dma_cleanup(bgmac); |
| 738 | return err; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 741 | |
| 742 | /************************************************** |
| 743 | * Chip ops |
| 744 | **************************************************/ |
| 745 | |
| 746 | /* TODO: can we just drop @force? Can we don't reset MAC at all if there is |
| 747 | * nothing to change? Try if after stabilizng driver. |
| 748 | */ |
| 749 | static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set, |
| 750 | bool force) |
| 751 | { |
| 752 | u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG); |
| 753 | u32 new_val = (cmdcfg & mask) | set; |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 754 | u32 cmdcfg_sr; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 755 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 756 | if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4) |
| 757 | cmdcfg_sr = BGMAC_CMDCFG_SR_REV4; |
| 758 | else |
| 759 | cmdcfg_sr = BGMAC_CMDCFG_SR_REV0; |
| 760 | |
| 761 | bgmac_set(bgmac, BGMAC_CMDCFG, cmdcfg_sr); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 762 | udelay(2); |
| 763 | |
| 764 | if (new_val != cmdcfg || force) |
| 765 | bgmac_write(bgmac, BGMAC_CMDCFG, new_val); |
| 766 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 767 | bgmac_mask(bgmac, BGMAC_CMDCFG, ~cmdcfg_sr); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 768 | udelay(2); |
| 769 | } |
| 770 | |
Hauke Mehrtens | 4e20900 | 2013-02-06 04:44:58 +0000 | [diff] [blame] | 771 | static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr) |
| 772 | { |
| 773 | u32 tmp; |
| 774 | |
| 775 | tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]; |
| 776 | bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp); |
| 777 | tmp = (addr[4] << 8) | addr[5]; |
| 778 | bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp); |
| 779 | } |
| 780 | |
Hauke Mehrtens | c6edfe1 | 2013-02-06 05:51:49 +0000 | [diff] [blame] | 781 | static void bgmac_set_rx_mode(struct net_device *net_dev) |
| 782 | { |
| 783 | struct bgmac *bgmac = netdev_priv(net_dev); |
| 784 | |
| 785 | if (net_dev->flags & IFF_PROMISC) |
Rafał Miłecki | e9ba103 | 2013-02-07 05:40:38 +0000 | [diff] [blame] | 786 | bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true); |
Hauke Mehrtens | c6edfe1 | 2013-02-06 05:51:49 +0000 | [diff] [blame] | 787 | else |
Rafał Miłecki | e9ba103 | 2013-02-07 05:40:38 +0000 | [diff] [blame] | 788 | bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true); |
Hauke Mehrtens | c6edfe1 | 2013-02-06 05:51:49 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 791 | #if 0 /* We don't use that regs yet */ |
| 792 | static void bgmac_chip_stats_update(struct bgmac *bgmac) |
| 793 | { |
| 794 | int i; |
| 795 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 796 | if (!(bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)) { |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 797 | for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++) |
| 798 | bgmac->mib_tx_regs[i] = |
| 799 | bgmac_read(bgmac, |
| 800 | BGMAC_TX_GOOD_OCTETS + (i * 4)); |
| 801 | for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++) |
| 802 | bgmac->mib_rx_regs[i] = |
| 803 | bgmac_read(bgmac, |
| 804 | BGMAC_RX_GOOD_OCTETS + (i * 4)); |
| 805 | } |
| 806 | |
| 807 | /* TODO: what else? how to handle BCM4706? Specs are needed */ |
| 808 | } |
| 809 | #endif |
| 810 | |
| 811 | static void bgmac_clear_mib(struct bgmac *bgmac) |
| 812 | { |
| 813 | int i; |
| 814 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 815 | if (bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 816 | return; |
| 817 | |
| 818 | bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR); |
| 819 | for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++) |
| 820 | bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4)); |
| 821 | for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++) |
| 822 | bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4)); |
| 823 | } |
| 824 | |
| 825 | /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */ |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 826 | static void bgmac_mac_speed(struct bgmac *bgmac) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 827 | { |
| 828 | u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD); |
| 829 | u32 set = 0; |
| 830 | |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 831 | switch (bgmac->mac_speed) { |
| 832 | case SPEED_10: |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 833 | set |= BGMAC_CMDCFG_ES_10; |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 834 | break; |
| 835 | case SPEED_100: |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 836 | set |= BGMAC_CMDCFG_ES_100; |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 837 | break; |
| 838 | case SPEED_1000: |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 839 | set |= BGMAC_CMDCFG_ES_1000; |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 840 | break; |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 841 | case SPEED_2500: |
| 842 | set |= BGMAC_CMDCFG_ES_2500; |
| 843 | break; |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 844 | default: |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 845 | dev_err(bgmac->dev, "Unsupported speed: %d\n", |
| 846 | bgmac->mac_speed); |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | if (bgmac->mac_duplex == DUPLEX_HALF) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 850 | set |= BGMAC_CMDCFG_HD; |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 851 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 852 | bgmac_cmdcfg_maskset(bgmac, mask, set, true); |
| 853 | } |
| 854 | |
| 855 | static void bgmac_miiconfig(struct bgmac *bgmac) |
| 856 | { |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 857 | if (bgmac->feature_flags & BGMAC_FEAT_FORCE_SPEED_2500) { |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 858 | bgmac_idm_write(bgmac, BCMA_IOCTL, |
| 859 | bgmac_idm_read(bgmac, BCMA_IOCTL) | 0x40 | |
| 860 | BGMAC_BCMA_IOCTL_SW_CLKEN); |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 861 | bgmac->mac_speed = SPEED_2500; |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 862 | bgmac->mac_duplex = DUPLEX_FULL; |
| 863 | bgmac_mac_speed(bgmac); |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 864 | } else { |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 865 | u8 imode; |
| 866 | |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 867 | imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & |
| 868 | BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT; |
| 869 | if (imode == 0 || imode == 1) { |
| 870 | bgmac->mac_speed = SPEED_100; |
| 871 | bgmac->mac_duplex = DUPLEX_FULL; |
| 872 | bgmac_mac_speed(bgmac); |
| 873 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
| 877 | /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */ |
| 878 | static void bgmac_chip_reset(struct bgmac *bgmac) |
| 879 | { |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 880 | u32 cmdcfg_sr; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 881 | u32 iost; |
| 882 | int i; |
| 883 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 884 | if (bgmac_clk_enabled(bgmac)) { |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 885 | if (!bgmac->stats_grabbed) { |
| 886 | /* bgmac_chip_stats_update(bgmac); */ |
| 887 | bgmac->stats_grabbed = true; |
| 888 | } |
| 889 | |
| 890 | for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) |
| 891 | bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]); |
| 892 | |
| 893 | bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false); |
| 894 | udelay(1); |
| 895 | |
| 896 | for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) |
| 897 | bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]); |
| 898 | |
| 899 | /* TODO: Clear software multicast filter list */ |
| 900 | } |
| 901 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 902 | iost = bgmac_idm_read(bgmac, BCMA_IOST); |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 903 | if (bgmac->feature_flags & BGMAC_FEAT_IOST_ATTACHED) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 904 | iost &= ~BGMAC_BCMA_IOST_ATTACHED; |
| 905 | |
Rafał Miłecki | 9e4e620 | 2016-02-22 22:51:13 +0100 | [diff] [blame] | 906 | /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */ |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 907 | if (!(bgmac->feature_flags & BGMAC_FEAT_NO_RESET)) { |
| 908 | u32 flags = 0; |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 909 | if (iost & BGMAC_BCMA_IOST_ATTACHED) { |
| 910 | flags = BGMAC_BCMA_IOCTL_SW_CLKEN; |
| 911 | if (!bgmac->has_robosw) |
| 912 | flags |= BGMAC_BCMA_IOCTL_SW_RESET; |
| 913 | } |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 914 | bgmac_clk_enable(bgmac, flags); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 917 | /* Request Misc PLL for corerev > 2 */ |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 918 | if (bgmac->feature_flags & BGMAC_FEAT_MISC_PLL_REQ) { |
Rafał Miłecki | 1a0ab76 | 2013-12-11 08:44:37 +0100 | [diff] [blame] | 919 | bgmac_set(bgmac, BCMA_CLKCTLST, |
| 920 | BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 921 | bgmac_wait_value(bgmac, BCMA_CLKCTLST, |
Rafał Miłecki | 1a0ab76 | 2013-12-11 08:44:37 +0100 | [diff] [blame] | 922 | BGMAC_BCMA_CLKCTLST_MISC_PLL_ST, |
| 923 | BGMAC_BCMA_CLKCTLST_MISC_PLL_ST, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 924 | 1000); |
| 925 | } |
| 926 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 927 | if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_PHY) { |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 928 | u8 et_swtype = 0; |
| 929 | u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY | |
Rafał Miłecki | 6a391e7 | 2013-09-15 00:22:47 +0200 | [diff] [blame] | 930 | BGMAC_CHIPCTL_1_IF_TYPE_MII; |
Hauke Mehrtens | 3647268 | 2013-09-15 22:49:08 +0200 | [diff] [blame] | 931 | char buf[4]; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 932 | |
Hauke Mehrtens | 3647268 | 2013-09-15 22:49:08 +0200 | [diff] [blame] | 933 | if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) { |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 934 | if (kstrtou8(buf, 0, &et_swtype)) |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 935 | dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n", |
| 936 | buf); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 937 | et_swtype &= 0x0f; |
| 938 | et_swtype <<= 4; |
| 939 | sw_type = et_swtype; |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 940 | } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_EPHYRMII) { |
Rafał Miłecki | e2d8f64 | 2016-08-17 23:11:52 +0200 | [diff] [blame] | 941 | sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RMII | |
| 942 | BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII; |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 943 | } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_RGMII) { |
Hauke Mehrtens | b5a4c2f | 2013-02-06 04:44:57 +0000 | [diff] [blame] | 944 | sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII | |
| 945 | BGMAC_CHIPCTL_1_SW_TYPE_RGMII; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 946 | } |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 947 | bgmac_cco_ctl_maskset(bgmac, 1, ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK | |
| 948 | BGMAC_CHIPCTL_1_SW_TYPE_MASK), |
| 949 | sw_type); |
Rafał Miłecki | 1cb94db | 2016-08-17 23:00:30 +0200 | [diff] [blame] | 950 | } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE) { |
| 951 | u32 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_MII | |
| 952 | BGMAC_CHIPCTL_4_SW_TYPE_EPHY; |
| 953 | u8 et_swtype = 0; |
| 954 | char buf[4]; |
| 955 | |
| 956 | if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) { |
| 957 | if (kstrtou8(buf, 0, &et_swtype)) |
| 958 | dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n", |
| 959 | buf); |
| 960 | sw_type = (et_swtype & 0x0f) << 12; |
| 961 | } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII) { |
| 962 | sw_type = BGMAC_CHIPCTL_4_IF_TYPE_RGMII | |
| 963 | BGMAC_CHIPCTL_4_SW_TYPE_RGMII; |
| 964 | } |
| 965 | bgmac_cco_ctl_maskset(bgmac, 4, ~(BGMAC_CHIPCTL_4_IF_TYPE_MASK | |
| 966 | BGMAC_CHIPCTL_4_SW_TYPE_MASK), |
| 967 | sw_type); |
| 968 | } else if (bgmac->feature_flags & BGMAC_FEAT_CC7_IF_TYPE_RGMII) { |
| 969 | bgmac_cco_ctl_maskset(bgmac, 7, ~BGMAC_CHIPCTL_7_IF_TYPE_MASK, |
| 970 | BGMAC_CHIPCTL_7_IF_TYPE_RGMII); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw) |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 974 | bgmac_idm_write(bgmac, BCMA_IOCTL, |
| 975 | bgmac_idm_read(bgmac, BCMA_IOCTL) & |
| 976 | ~BGMAC_BCMA_IOCTL_SW_RESET); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 977 | |
| 978 | /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset |
| 979 | * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine |
| 980 | * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to |
| 981 | * be keps until taking MAC out of the reset. |
| 982 | */ |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 983 | if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4) |
| 984 | cmdcfg_sr = BGMAC_CMDCFG_SR_REV4; |
| 985 | else |
| 986 | cmdcfg_sr = BGMAC_CMDCFG_SR_REV0; |
| 987 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 988 | bgmac_cmdcfg_maskset(bgmac, |
| 989 | ~(BGMAC_CMDCFG_TE | |
| 990 | BGMAC_CMDCFG_RE | |
| 991 | BGMAC_CMDCFG_RPI | |
| 992 | BGMAC_CMDCFG_TAI | |
| 993 | BGMAC_CMDCFG_HD | |
| 994 | BGMAC_CMDCFG_ML | |
| 995 | BGMAC_CMDCFG_CFE | |
| 996 | BGMAC_CMDCFG_RL | |
| 997 | BGMAC_CMDCFG_RED | |
| 998 | BGMAC_CMDCFG_PE | |
| 999 | BGMAC_CMDCFG_TPI | |
| 1000 | BGMAC_CMDCFG_PAD_EN | |
| 1001 | BGMAC_CMDCFG_PF), |
| 1002 | BGMAC_CMDCFG_PROM | |
| 1003 | BGMAC_CMDCFG_NLC | |
| 1004 | BGMAC_CMDCFG_CFE | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1005 | cmdcfg_sr, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1006 | false); |
Rafał Miłecki | d469962 | 2013-12-11 07:44:14 +0100 | [diff] [blame] | 1007 | bgmac->mac_speed = SPEED_UNKNOWN; |
| 1008 | bgmac->mac_duplex = DUPLEX_UNKNOWN; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1009 | |
| 1010 | bgmac_clear_mib(bgmac); |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1011 | if (bgmac->feature_flags & BGMAC_FEAT_CMN_PHY_CTL) |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1012 | bgmac_cmn_maskset32(bgmac, BCMA_GMAC_CMN_PHY_CTL, ~0, |
| 1013 | BCMA_GMAC_CMN_PC_MTE); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1014 | else |
| 1015 | bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE); |
| 1016 | bgmac_miiconfig(bgmac); |
Jon Mason | 55954f3 | 2016-07-07 19:08:55 -0400 | [diff] [blame] | 1017 | if (bgmac->mii_bus) |
| 1018 | bgmac->mii_bus->reset(bgmac->mii_bus); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1019 | |
Hauke Mehrtens | 49a467b | 2013-09-29 13:54:58 +0200 | [diff] [blame] | 1020 | netdev_reset_queue(bgmac->net_dev); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | static void bgmac_chip_intrs_on(struct bgmac *bgmac) |
| 1024 | { |
| 1025 | bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask); |
| 1026 | } |
| 1027 | |
| 1028 | static void bgmac_chip_intrs_off(struct bgmac *bgmac) |
| 1029 | { |
| 1030 | bgmac_write(bgmac, BGMAC_INT_MASK, 0); |
Nathan Hintz | 4160815 | 2013-02-13 19:14:10 +0000 | [diff] [blame] | 1031 | bgmac_read(bgmac, BGMAC_INT_MASK); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */ |
| 1035 | static void bgmac_enable(struct bgmac *bgmac) |
| 1036 | { |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1037 | u32 cmdcfg_sr; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1038 | u32 cmdcfg; |
| 1039 | u32 mode; |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1040 | |
| 1041 | if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4) |
| 1042 | cmdcfg_sr = BGMAC_CMDCFG_SR_REV4; |
| 1043 | else |
| 1044 | cmdcfg_sr = BGMAC_CMDCFG_SR_REV0; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1045 | |
| 1046 | cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG); |
| 1047 | bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE), |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1048 | cmdcfg_sr, true); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1049 | udelay(2); |
| 1050 | cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE; |
| 1051 | bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg); |
| 1052 | |
| 1053 | mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >> |
| 1054 | BGMAC_DS_MM_SHIFT; |
Rafał Miłecki | cdb26d3 | 2016-11-07 13:53:27 +0100 | [diff] [blame] | 1055 | if (bgmac->feature_flags & BGMAC_FEAT_CLKCTLST || mode != 0) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1056 | bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT); |
Rafał Miłecki | cdb26d3 | 2016-11-07 13:53:27 +0100 | [diff] [blame] | 1057 | if (!(bgmac->feature_flags & BGMAC_FEAT_CLKCTLST) && mode == 2) |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1058 | bgmac_cco_ctl_maskset(bgmac, 1, ~0, |
| 1059 | BGMAC_CHIPCTL_1_RXC_DLL_BYPASS); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1060 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1061 | if (bgmac->feature_flags & (BGMAC_FEAT_FLW_CTRL1 | |
| 1062 | BGMAC_FEAT_FLW_CTRL2)) { |
| 1063 | u32 fl_ctl; |
| 1064 | |
| 1065 | if (bgmac->feature_flags & BGMAC_FEAT_FLW_CTRL1) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1066 | fl_ctl = 0x2300e1; |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1067 | else |
| 1068 | fl_ctl = 0x03cb04cb; |
| 1069 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1070 | bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl); |
| 1071 | bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Jon Mason | db791eb | 2016-07-07 19:08:56 -0400 | [diff] [blame] | 1074 | if (bgmac->feature_flags & BGMAC_FEAT_SET_RXQ_CLK) { |
| 1075 | u32 rxq_ctl; |
| 1076 | u16 bp_clk; |
| 1077 | u8 mdp; |
| 1078 | |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 1079 | rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL); |
| 1080 | rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK; |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1081 | bp_clk = bgmac_get_bus_clock(bgmac) / 1000000; |
Hauke Mehrtens | 6df4aff | 2014-01-05 01:10:47 +0100 | [diff] [blame] | 1082 | mdp = (bp_clk * 128 / 1000) - 3; |
| 1083 | rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT); |
| 1084 | bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl); |
| 1085 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */ |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 1089 | static void bgmac_chip_init(struct bgmac *bgmac) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1090 | { |
Jon Mason | dd5c5d0 | 2016-11-04 01:11:01 -0400 | [diff] [blame] | 1091 | /* Clear any erroneously pending interrupts */ |
| 1092 | bgmac_write(bgmac, BGMAC_INT_STATUS, ~0); |
| 1093 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1094 | /* 1 interrupt per received frame */ |
| 1095 | bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT); |
| 1096 | |
| 1097 | /* Enable 802.3x tx flow control (honor received PAUSE frames) */ |
| 1098 | bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true); |
| 1099 | |
Hauke Mehrtens | c6edfe1 | 2013-02-06 05:51:49 +0000 | [diff] [blame] | 1100 | bgmac_set_rx_mode(bgmac->net_dev); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1101 | |
Hauke Mehrtens | 4e20900 | 2013-02-06 04:44:58 +0000 | [diff] [blame] | 1102 | bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (bgmac->loopback) |
Rafał Miłecki | e9ba103 | 2013-02-07 05:40:38 +0000 | [diff] [blame] | 1105 | bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1106 | else |
Rafał Miłecki | e9ba103 | 2013-02-07 05:40:38 +0000 | [diff] [blame] | 1107 | bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1108 | |
| 1109 | bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN); |
| 1110 | |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 1111 | bgmac_chip_intrs_on(bgmac); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1112 | |
| 1113 | bgmac_enable(bgmac); |
| 1114 | } |
| 1115 | |
| 1116 | static irqreturn_t bgmac_interrupt(int irq, void *dev_id) |
| 1117 | { |
| 1118 | struct bgmac *bgmac = netdev_priv(dev_id); |
| 1119 | |
| 1120 | u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS); |
| 1121 | int_status &= bgmac->int_mask; |
| 1122 | |
| 1123 | if (!int_status) |
| 1124 | return IRQ_NONE; |
| 1125 | |
Felix Fietkau | eb64e29 | 2015-04-14 12:07:55 +0200 | [diff] [blame] | 1126 | int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX); |
| 1127 | if (int_status) |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 1128 | dev_err(bgmac->dev, "Unknown IRQs: 0x%08X\n", int_status); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1129 | |
| 1130 | /* Disable new interrupts until handling existing ones */ |
| 1131 | bgmac_chip_intrs_off(bgmac); |
| 1132 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1133 | napi_schedule(&bgmac->napi); |
| 1134 | |
| 1135 | return IRQ_HANDLED; |
| 1136 | } |
| 1137 | |
| 1138 | static int bgmac_poll(struct napi_struct *napi, int weight) |
| 1139 | { |
| 1140 | struct bgmac *bgmac = container_of(napi, struct bgmac, napi); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1141 | int handled = 0; |
| 1142 | |
Felix Fietkau | eb64e29 | 2015-04-14 12:07:55 +0200 | [diff] [blame] | 1143 | /* Ack */ |
| 1144 | bgmac_write(bgmac, BGMAC_INT_STATUS, ~0); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1145 | |
Felix Fietkau | eb64e29 | 2015-04-14 12:07:55 +0200 | [diff] [blame] | 1146 | bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]); |
| 1147 | handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1148 | |
Felix Fietkau | eb64e29 | 2015-04-14 12:07:55 +0200 | [diff] [blame] | 1149 | /* Poll again if more events arrived in the meantime */ |
| 1150 | 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] | 1151 | return weight; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1152 | |
Hauke Mehrtens | 43f159c | 2015-01-18 19:49:59 +0100 | [diff] [blame] | 1153 | if (handled < weight) { |
Eric Dumazet | 6ad2016 | 2017-01-30 08:22:01 -0800 | [diff] [blame] | 1154 | napi_complete_done(napi, handled); |
Hauke Mehrtens | 43f159c | 2015-01-18 19:49:59 +0100 | [diff] [blame] | 1155 | bgmac_chip_intrs_on(bgmac); |
| 1156 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1157 | |
| 1158 | return handled; |
| 1159 | } |
| 1160 | |
| 1161 | /************************************************** |
| 1162 | * net_device_ops |
| 1163 | **************************************************/ |
| 1164 | |
| 1165 | static int bgmac_open(struct net_device *net_dev) |
| 1166 | { |
| 1167 | struct bgmac *bgmac = netdev_priv(net_dev); |
| 1168 | int err = 0; |
| 1169 | |
| 1170 | bgmac_chip_reset(bgmac); |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 1171 | |
| 1172 | err = bgmac_dma_init(bgmac); |
| 1173 | if (err) |
| 1174 | return err; |
| 1175 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1176 | /* 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] | 1177 | bgmac_chip_init(bgmac); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1178 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1179 | err = request_irq(bgmac->irq, bgmac_interrupt, IRQF_SHARED, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1180 | KBUILD_MODNAME, net_dev); |
| 1181 | if (err < 0) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 1182 | dev_err(bgmac->dev, "IRQ request error: %d!\n", err); |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 1183 | bgmac_dma_cleanup(bgmac); |
| 1184 | return err; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1185 | } |
| 1186 | napi_enable(&bgmac->napi); |
| 1187 | |
Philippe Reynes | b21fcb2 | 2016-06-19 22:37:05 +0200 | [diff] [blame] | 1188 | phy_start(net_dev->phydev); |
Rafał Miłecki | 4e34da4d | 2013-12-10 17:19:39 +0100 | [diff] [blame] | 1189 | |
Florian Fainelli | c3897f2 | 2016-06-23 14:25:32 -0700 | [diff] [blame] | 1190 | netif_start_queue(net_dev); |
| 1191 | |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 1192 | return 0; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | static int bgmac_stop(struct net_device *net_dev) |
| 1196 | { |
| 1197 | struct bgmac *bgmac = netdev_priv(net_dev); |
| 1198 | |
| 1199 | netif_carrier_off(net_dev); |
| 1200 | |
Philippe Reynes | b21fcb2 | 2016-06-19 22:37:05 +0200 | [diff] [blame] | 1201 | phy_stop(net_dev->phydev); |
Rafał Miłecki | 4e34da4d | 2013-12-10 17:19:39 +0100 | [diff] [blame] | 1202 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1203 | napi_disable(&bgmac->napi); |
| 1204 | bgmac_chip_intrs_off(bgmac); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1205 | free_irq(bgmac->irq, net_dev); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1206 | |
| 1207 | bgmac_chip_reset(bgmac); |
Felix Fietkau | 74b6f29 | 2015-04-14 12:08:00 +0200 | [diff] [blame] | 1208 | bgmac_dma_cleanup(bgmac); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb, |
| 1214 | struct net_device *net_dev) |
| 1215 | { |
| 1216 | struct bgmac *bgmac = netdev_priv(net_dev); |
| 1217 | struct bgmac_dma_ring *ring; |
| 1218 | |
| 1219 | /* No QOS support yet */ |
| 1220 | ring = &bgmac->tx_ring[0]; |
| 1221 | return bgmac_dma_tx_add(bgmac, ring, skb); |
| 1222 | } |
| 1223 | |
Hauke Mehrtens | 4e20900 | 2013-02-06 04:44:58 +0000 | [diff] [blame] | 1224 | static int bgmac_set_mac_address(struct net_device *net_dev, void *addr) |
| 1225 | { |
| 1226 | struct bgmac *bgmac = netdev_priv(net_dev); |
Hari Vyas | fa42245 | 2017-03-02 17:59:57 -0500 | [diff] [blame] | 1227 | struct sockaddr *sa = addr; |
Hauke Mehrtens | 4e20900 | 2013-02-06 04:44:58 +0000 | [diff] [blame] | 1228 | int ret; |
| 1229 | |
| 1230 | ret = eth_prepare_mac_addr_change(net_dev, addr); |
| 1231 | if (ret < 0) |
| 1232 | return ret; |
Hari Vyas | fa42245 | 2017-03-02 17:59:57 -0500 | [diff] [blame] | 1233 | |
| 1234 | ether_addr_copy(net_dev->dev_addr, sa->sa_data); |
| 1235 | bgmac_write_mac_address(bgmac, net_dev->dev_addr); |
| 1236 | |
Hauke Mehrtens | 4e20900 | 2013-02-06 04:44:58 +0000 | [diff] [blame] | 1237 | eth_commit_mac_addr_change(net_dev, addr); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1241 | static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd) |
| 1242 | { |
Hauke Mehrtens | 69c5885 | 2013-12-20 15:34:45 +0100 | [diff] [blame] | 1243 | if (!netif_running(net_dev)) |
| 1244 | return -EINVAL; |
| 1245 | |
Philippe Reynes | b21fcb2 | 2016-06-19 22:37:05 +0200 | [diff] [blame] | 1246 | return phy_mii_ioctl(net_dev->phydev, ifr, cmd); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | static const struct net_device_ops bgmac_netdev_ops = { |
| 1250 | .ndo_open = bgmac_open, |
| 1251 | .ndo_stop = bgmac_stop, |
| 1252 | .ndo_start_xmit = bgmac_start_xmit, |
Hauke Mehrtens | c6edfe1 | 2013-02-06 05:51:49 +0000 | [diff] [blame] | 1253 | .ndo_set_rx_mode = bgmac_set_rx_mode, |
Hauke Mehrtens | 4e20900 | 2013-02-06 04:44:58 +0000 | [diff] [blame] | 1254 | .ndo_set_mac_address = bgmac_set_mac_address, |
Hauke Mehrtens | 522c590 | 2013-02-06 04:44:59 +0000 | [diff] [blame] | 1255 | .ndo_validate_addr = eth_validate_addr, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1256 | .ndo_do_ioctl = bgmac_ioctl, |
| 1257 | }; |
| 1258 | |
| 1259 | /************************************************** |
| 1260 | * ethtool_ops |
| 1261 | **************************************************/ |
| 1262 | |
Florian Fainelli | f6613d4 | 2016-06-07 15:06:14 -0700 | [diff] [blame] | 1263 | struct bgmac_stat { |
| 1264 | u8 size; |
| 1265 | u32 offset; |
| 1266 | const char *name; |
| 1267 | }; |
| 1268 | |
| 1269 | static struct bgmac_stat bgmac_get_strings_stats[] = { |
| 1270 | { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" }, |
| 1271 | { 4, BGMAC_TX_GOOD_PKTS, "tx_good" }, |
| 1272 | { 8, BGMAC_TX_OCTETS, "tx_octets" }, |
| 1273 | { 4, BGMAC_TX_PKTS, "tx_pkts" }, |
| 1274 | { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" }, |
| 1275 | { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" }, |
| 1276 | { 4, BGMAC_TX_LEN_64, "tx_64" }, |
| 1277 | { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" }, |
| 1278 | { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" }, |
| 1279 | { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" }, |
| 1280 | { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" }, |
| 1281 | { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" }, |
| 1282 | { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" }, |
| 1283 | { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" }, |
| 1284 | { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" }, |
| 1285 | { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" }, |
| 1286 | { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" }, |
| 1287 | { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" }, |
| 1288 | { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" }, |
| 1289 | { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" }, |
| 1290 | { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" }, |
| 1291 | { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" }, |
| 1292 | { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" }, |
| 1293 | { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" }, |
| 1294 | { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" }, |
| 1295 | { 4, BGMAC_TX_DEFERED, "tx_defered" }, |
| 1296 | { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" }, |
| 1297 | { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" }, |
| 1298 | { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" }, |
| 1299 | { 4, BGMAC_TX_Q0_PKTS, "tx_q0" }, |
| 1300 | { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" }, |
| 1301 | { 4, BGMAC_TX_Q1_PKTS, "tx_q1" }, |
| 1302 | { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" }, |
| 1303 | { 4, BGMAC_TX_Q2_PKTS, "tx_q2" }, |
| 1304 | { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" }, |
| 1305 | { 4, BGMAC_TX_Q3_PKTS, "tx_q3" }, |
| 1306 | { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" }, |
| 1307 | { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" }, |
| 1308 | { 4, BGMAC_RX_GOOD_PKTS, "rx_good" }, |
| 1309 | { 8, BGMAC_RX_OCTETS, "rx_octets" }, |
| 1310 | { 4, BGMAC_RX_PKTS, "rx_pkts" }, |
| 1311 | { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" }, |
| 1312 | { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" }, |
| 1313 | { 4, BGMAC_RX_LEN_64, "rx_64" }, |
| 1314 | { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" }, |
| 1315 | { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" }, |
| 1316 | { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" }, |
| 1317 | { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" }, |
| 1318 | { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" }, |
| 1319 | { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" }, |
| 1320 | { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" }, |
| 1321 | { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" }, |
| 1322 | { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" }, |
| 1323 | { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" }, |
| 1324 | { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" }, |
| 1325 | { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" }, |
| 1326 | { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" }, |
| 1327 | { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" }, |
| 1328 | { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" }, |
| 1329 | { 4, BGMAC_RX_CRC_ERRS, "rx_crc" }, |
| 1330 | { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" }, |
| 1331 | { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" }, |
| 1332 | { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" }, |
| 1333 | { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" }, |
| 1334 | { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" }, |
| 1335 | { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" }, |
| 1336 | }; |
| 1337 | |
| 1338 | #define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats) |
| 1339 | |
| 1340 | static int bgmac_get_sset_count(struct net_device *dev, int string_set) |
| 1341 | { |
| 1342 | switch (string_set) { |
| 1343 | case ETH_SS_STATS: |
| 1344 | return BGMAC_STATS_LEN; |
| 1345 | } |
| 1346 | |
| 1347 | return -EOPNOTSUPP; |
| 1348 | } |
| 1349 | |
| 1350 | static void bgmac_get_strings(struct net_device *dev, u32 stringset, |
| 1351 | u8 *data) |
| 1352 | { |
| 1353 | int i; |
| 1354 | |
| 1355 | if (stringset != ETH_SS_STATS) |
| 1356 | return; |
| 1357 | |
| 1358 | for (i = 0; i < BGMAC_STATS_LEN; i++) |
| 1359 | strlcpy(data + i * ETH_GSTRING_LEN, |
| 1360 | bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN); |
| 1361 | } |
| 1362 | |
| 1363 | static void bgmac_get_ethtool_stats(struct net_device *dev, |
| 1364 | struct ethtool_stats *ss, uint64_t *data) |
| 1365 | { |
| 1366 | struct bgmac *bgmac = netdev_priv(dev); |
| 1367 | const struct bgmac_stat *s; |
| 1368 | unsigned int i; |
| 1369 | u64 val; |
| 1370 | |
| 1371 | if (!netif_running(dev)) |
| 1372 | return; |
| 1373 | |
| 1374 | for (i = 0; i < BGMAC_STATS_LEN; i++) { |
| 1375 | s = &bgmac_get_strings_stats[i]; |
| 1376 | val = 0; |
| 1377 | if (s->size == 8) |
| 1378 | val = (u64)bgmac_read(bgmac, s->offset + 4) << 32; |
| 1379 | val |= bgmac_read(bgmac, s->offset); |
| 1380 | data[i] = val; |
| 1381 | } |
| 1382 | } |
| 1383 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1384 | static void bgmac_get_drvinfo(struct net_device *net_dev, |
| 1385 | struct ethtool_drvinfo *info) |
| 1386 | { |
| 1387 | strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver)); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1388 | strlcpy(info->bus_info, "AXI", sizeof(info->bus_info)); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | static const struct ethtool_ops bgmac_ethtool_ops = { |
Florian Fainelli | f6613d4 | 2016-06-07 15:06:14 -0700 | [diff] [blame] | 1392 | .get_strings = bgmac_get_strings, |
| 1393 | .get_sset_count = bgmac_get_sset_count, |
| 1394 | .get_ethtool_stats = bgmac_get_ethtool_stats, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1395 | .get_drvinfo = bgmac_get_drvinfo, |
Philippe Reynes | 904632a | 2016-06-19 22:37:06 +0200 | [diff] [blame] | 1396 | .get_link_ksettings = phy_ethtool_get_link_ksettings, |
| 1397 | .set_link_ksettings = phy_ethtool_set_link_ksettings, |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1398 | }; |
| 1399 | |
| 1400 | /************************************************** |
Rafał Miłecki | 11e5e76 | 2013-03-07 01:53:28 +0000 | [diff] [blame] | 1401 | * MII |
| 1402 | **************************************************/ |
| 1403 | |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 1404 | void bgmac_adjust_link(struct net_device *net_dev) |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 1405 | { |
| 1406 | struct bgmac *bgmac = netdev_priv(net_dev); |
Philippe Reynes | b21fcb2 | 2016-06-19 22:37:05 +0200 | [diff] [blame] | 1407 | struct phy_device *phy_dev = net_dev->phydev; |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 1408 | bool update = false; |
| 1409 | |
| 1410 | if (phy_dev->link) { |
| 1411 | if (phy_dev->speed != bgmac->mac_speed) { |
| 1412 | bgmac->mac_speed = phy_dev->speed; |
| 1413 | update = true; |
| 1414 | } |
| 1415 | |
| 1416 | if (phy_dev->duplex != bgmac->mac_duplex) { |
| 1417 | bgmac->mac_duplex = phy_dev->duplex; |
| 1418 | update = true; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | if (update) { |
| 1423 | bgmac_mac_speed(bgmac); |
| 1424 | phy_print_status(phy_dev); |
| 1425 | } |
| 1426 | } |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 1427 | EXPORT_SYMBOL_GPL(bgmac_adjust_link); |
Rafał Miłecki | 5824d2d | 2013-12-07 00:53:55 +0100 | [diff] [blame] | 1428 | |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 1429 | int bgmac_phy_connect_direct(struct bgmac *bgmac) |
Rafał Miłecki | c25b23b | 2015-03-20 23:14:31 +0100 | [diff] [blame] | 1430 | { |
| 1431 | struct fixed_phy_status fphy_status = { |
| 1432 | .link = 1, |
| 1433 | .speed = SPEED_1000, |
| 1434 | .duplex = DUPLEX_FULL, |
| 1435 | }; |
| 1436 | struct phy_device *phy_dev; |
| 1437 | int err; |
| 1438 | |
Fabio Estevam | 4db78d3 | 2015-09-02 13:25:59 -0300 | [diff] [blame] | 1439 | phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL); |
Rafał Miłecki | c25b23b | 2015-03-20 23:14:31 +0100 | [diff] [blame] | 1440 | if (!phy_dev || IS_ERR(phy_dev)) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 1441 | dev_err(bgmac->dev, "Failed to register fixed PHY device\n"); |
Rafał Miłecki | c25b23b | 2015-03-20 23:14:31 +0100 | [diff] [blame] | 1442 | return -ENODEV; |
| 1443 | } |
| 1444 | |
| 1445 | err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link, |
| 1446 | PHY_INTERFACE_MODE_MII); |
| 1447 | if (err) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 1448 | dev_err(bgmac->dev, "Connecting PHY failed\n"); |
Rafał Miłecki | c25b23b | 2015-03-20 23:14:31 +0100 | [diff] [blame] | 1449 | return err; |
| 1450 | } |
| 1451 | |
Rafał Miłecki | c25b23b | 2015-03-20 23:14:31 +0100 | [diff] [blame] | 1452 | return err; |
| 1453 | } |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 1454 | EXPORT_SYMBOL_GPL(bgmac_phy_connect_direct); |
Rafał Miłecki | 11e5e76 | 2013-03-07 01:53:28 +0000 | [diff] [blame] | 1455 | |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1456 | struct bgmac *bgmac_alloc(struct device *dev) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1457 | { |
| 1458 | struct net_device *net_dev; |
| 1459 | struct bgmac *bgmac; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1460 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1461 | /* Allocation and references */ |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1462 | net_dev = devm_alloc_etherdev(dev, sizeof(*bgmac)); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1463 | if (!net_dev) |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1464 | return NULL; |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1465 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1466 | net_dev->netdev_ops = &bgmac_netdev_ops; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 1467 | net_dev->ethtool_ops = &bgmac_ethtool_ops; |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1468 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1469 | bgmac = netdev_priv(net_dev); |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1470 | bgmac->dev = dev; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1471 | bgmac->net_dev = net_dev; |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1472 | |
| 1473 | return bgmac; |
| 1474 | } |
| 1475 | EXPORT_SYMBOL_GPL(bgmac_alloc); |
| 1476 | |
| 1477 | int bgmac_enet_probe(struct bgmac *bgmac) |
| 1478 | { |
| 1479 | struct net_device *net_dev = bgmac->net_dev; |
| 1480 | int err; |
| 1481 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1482 | net_dev->irq = bgmac->irq; |
| 1483 | SET_NETDEV_DEV(net_dev, bgmac->dev); |
Joey Zhong | f3537b3 | 2017-02-28 13:51:01 -0500 | [diff] [blame] | 1484 | dev_set_drvdata(bgmac->dev, bgmac); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1485 | |
Tobias Klauser | 6850f8b | 2017-02-16 15:11:19 +0100 | [diff] [blame] | 1486 | if (!is_valid_ether_addr(net_dev->dev_addr)) { |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1487 | dev_err(bgmac->dev, "Invalid MAC addr: %pM\n", |
Tobias Klauser | 6850f8b | 2017-02-16 15:11:19 +0100 | [diff] [blame] | 1488 | net_dev->dev_addr); |
| 1489 | eth_hw_addr_random(net_dev); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1490 | dev_warn(bgmac->dev, "Using random MAC: %pM\n", |
Tobias Klauser | 6850f8b | 2017-02-16 15:11:19 +0100 | [diff] [blame] | 1491 | net_dev->dev_addr); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1492 | } |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1493 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1494 | /* This (reset &) enable is not preset in specs or reference driver but |
| 1495 | * Broadcom does it in arch PCI code when enabling fake PCI device. |
| 1496 | */ |
| 1497 | bgmac_clk_enable(bgmac, 0); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1498 | |
Rafał Miłecki | 1cb94db | 2016-08-17 23:00:30 +0200 | [diff] [blame] | 1499 | /* This seems to be fixing IRQ by assigning OOB #6 to the core */ |
| 1500 | if (bgmac->feature_flags & BGMAC_FEAT_IRQ_ID_OOB_6) |
| 1501 | bgmac_idm_write(bgmac, BCMA_OOB_SEL_OUT_A30, 0x86); |
| 1502 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1503 | bgmac_chip_reset(bgmac); |
| 1504 | |
| 1505 | err = bgmac_dma_alloc(bgmac); |
| 1506 | if (err) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 1507 | dev_err(bgmac->dev, "Unable to alloc memory for DMA\n"); |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1508 | goto err_out; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | 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] | 1512 | if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1513 | bgmac->int_mask &= ~BGMAC_IS_TX_MASK; |
| 1514 | |
Hauke Mehrtens | 6216642 | 2015-01-18 19:49:58 +0100 | [diff] [blame] | 1515 | netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT); |
| 1516 | |
Jon Mason | 1676aba5ef | 2016-11-04 01:11:00 -0400 | [diff] [blame] | 1517 | err = bgmac_phy_connect(bgmac); |
Rafał Miłecki | 11e5e76 | 2013-03-07 01:53:28 +0000 | [diff] [blame] | 1518 | if (err) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 1519 | dev_err(bgmac->dev, "Cannot connect to phy\n"); |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1520 | goto err_dma_free; |
Rafał Miłecki | 11e5e76 | 2013-03-07 01:53:28 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Felix Fietkau | 9cde945 | 2015-03-23 12:35:37 +0100 | [diff] [blame] | 1523 | net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM; |
| 1524 | net_dev->hw_features = net_dev->features; |
| 1525 | net_dev->vlan_features = net_dev->features; |
| 1526 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1527 | err = register_netdev(bgmac->net_dev); |
| 1528 | if (err) { |
Jon Mason | d00a828 | 2016-07-07 19:08:53 -0400 | [diff] [blame] | 1529 | dev_err(bgmac->dev, "Cannot register net device\n"); |
Jon Mason | 55954f3 | 2016-07-07 19:08:55 -0400 | [diff] [blame] | 1530 | goto err_phy_disconnect; |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | netif_carrier_off(net_dev); |
| 1534 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1535 | return 0; |
| 1536 | |
Jon Mason | 55954f3 | 2016-07-07 19:08:55 -0400 | [diff] [blame] | 1537 | err_phy_disconnect: |
| 1538 | phy_disconnect(net_dev->phydev); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1539 | err_dma_free: |
| 1540 | bgmac_dma_free(bgmac); |
Rafał Miłecki | 34a5102 | 2017-01-31 19:37:54 +0100 | [diff] [blame] | 1541 | err_out: |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1542 | |
| 1543 | return err; |
| 1544 | } |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1545 | EXPORT_SYMBOL_GPL(bgmac_enet_probe); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1546 | |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1547 | void bgmac_enet_remove(struct bgmac *bgmac) |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1548 | { |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1549 | unregister_netdev(bgmac->net_dev); |
Jon Mason | 55954f3 | 2016-07-07 19:08:55 -0400 | [diff] [blame] | 1550 | phy_disconnect(bgmac->net_dev->phydev); |
Hauke Mehrtens | 6216642 | 2015-01-18 19:49:58 +0100 | [diff] [blame] | 1551 | netif_napi_del(&bgmac->napi); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1552 | bgmac_dma_free(bgmac); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1553 | free_netdev(bgmac->net_dev); |
| 1554 | } |
Jon Mason | f6a95a2 | 2016-07-07 19:08:57 -0400 | [diff] [blame] | 1555 | EXPORT_SYMBOL_GPL(bgmac_enet_remove); |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1556 | |
Joey Zhong | f3537b3 | 2017-02-28 13:51:01 -0500 | [diff] [blame] | 1557 | int bgmac_enet_suspend(struct bgmac *bgmac) |
| 1558 | { |
| 1559 | if (!netif_running(bgmac->net_dev)) |
| 1560 | return 0; |
| 1561 | |
| 1562 | phy_stop(bgmac->net_dev->phydev); |
| 1563 | |
| 1564 | netif_stop_queue(bgmac->net_dev); |
| 1565 | |
| 1566 | napi_disable(&bgmac->napi); |
| 1567 | |
| 1568 | netif_tx_lock(bgmac->net_dev); |
| 1569 | netif_device_detach(bgmac->net_dev); |
| 1570 | netif_tx_unlock(bgmac->net_dev); |
| 1571 | |
| 1572 | bgmac_chip_intrs_off(bgmac); |
| 1573 | bgmac_chip_reset(bgmac); |
| 1574 | bgmac_dma_cleanup(bgmac); |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | EXPORT_SYMBOL_GPL(bgmac_enet_suspend); |
| 1579 | |
| 1580 | int bgmac_enet_resume(struct bgmac *bgmac) |
| 1581 | { |
| 1582 | int rc; |
| 1583 | |
| 1584 | if (!netif_running(bgmac->net_dev)) |
| 1585 | return 0; |
| 1586 | |
| 1587 | rc = bgmac_dma_init(bgmac); |
| 1588 | if (rc) |
| 1589 | return rc; |
| 1590 | |
| 1591 | bgmac_chip_init(bgmac); |
| 1592 | |
| 1593 | napi_enable(&bgmac->napi); |
| 1594 | |
| 1595 | netif_tx_lock(bgmac->net_dev); |
| 1596 | netif_device_attach(bgmac->net_dev); |
| 1597 | netif_tx_unlock(bgmac->net_dev); |
| 1598 | |
| 1599 | netif_start_queue(bgmac->net_dev); |
| 1600 | |
| 1601 | phy_start(bgmac->net_dev->phydev); |
| 1602 | |
| 1603 | return 0; |
| 1604 | } |
| 1605 | EXPORT_SYMBOL_GPL(bgmac_enet_resume); |
| 1606 | |
Rafał Miłecki | dd4544f | 2013-01-08 20:06:23 +0000 | [diff] [blame] | 1607 | MODULE_AUTHOR("Rafał Miłecki"); |
| 1608 | MODULE_LICENSE("GPL"); |