Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Xilinx TEMAC Ethernet device |
| 3 | * |
| 4 | * Copyright (c) 2008 Nissin Systems Co., Ltd., Yoshio Kashiwagi |
| 5 | * Copyright (c) 2005-2008 DLA Systems, David H. Lynch Jr. <dhlii@dlasys.net> |
| 6 | * Copyright (c) 2008-2009 Secret Lab Technologies Ltd. |
| 7 | * |
| 8 | * This is a driver for the Xilinx ll_temac ipcore which is often used |
| 9 | * in the Virtex and Spartan series of chips. |
| 10 | * |
| 11 | * Notes: |
| 12 | * - The ll_temac hardware uses indirect access for many of the TEMAC |
| 13 | * registers, include the MDIO bus. However, indirect access to MDIO |
| 14 | * registers take considerably more clock cycles than to TEMAC registers. |
| 15 | * MDIO accesses are long, so threads doing them should probably sleep |
| 16 | * rather than busywait. However, since only one indirect access can be |
| 17 | * in progress at any given time, that means that *all* indirect accesses |
| 18 | * could end up sleeping (to wait for an MDIO access to complete). |
| 19 | * Fortunately none of the indirect accesses are on the 'hot' path for tx |
| 20 | * or rx, so this should be okay. |
| 21 | * |
| 22 | * TODO: |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 23 | * - Factor out locallink DMA code into separate driver |
| 24 | * - Fix multicast assignment. |
| 25 | * - Fix support for hardware checksumming. |
| 26 | * - Testing. Lots and lots of testing. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/etherdevice.h> |
| 32 | #include <linux/init.h> |
| 33 | #include <linux/mii.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/mutex.h> |
| 36 | #include <linux/netdevice.h> |
| 37 | #include <linux/of.h> |
| 38 | #include <linux/of_device.h> |
| 39 | #include <linux/of_mdio.h> |
| 40 | #include <linux/of_platform.h> |
Michal Simek | 9f1a1fc | 2010-09-01 08:55:23 -0600 | [diff] [blame] | 41 | #include <linux/of_address.h> |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 42 | #include <linux/skbuff.h> |
| 43 | #include <linux/spinlock.h> |
| 44 | #include <linux/tcp.h> /* needed for sizeof(tcphdr) */ |
| 45 | #include <linux/udp.h> /* needed for sizeof(udphdr) */ |
| 46 | #include <linux/phy.h> |
| 47 | #include <linux/in.h> |
| 48 | #include <linux/io.h> |
| 49 | #include <linux/ip.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 50 | #include <linux/slab.h> |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 51 | |
| 52 | #include "ll_temac.h" |
| 53 | |
| 54 | #define TX_BD_NUM 64 |
| 55 | #define RX_BD_NUM 128 |
| 56 | |
| 57 | /* --------------------------------------------------------------------- |
| 58 | * Low level register access functions |
| 59 | */ |
| 60 | |
| 61 | u32 temac_ior(struct temac_local *lp, int offset) |
| 62 | { |
| 63 | return in_be32((u32 *)(lp->regs + offset)); |
| 64 | } |
| 65 | |
| 66 | void temac_iow(struct temac_local *lp, int offset, u32 value) |
| 67 | { |
| 68 | out_be32((u32 *) (lp->regs + offset), value); |
| 69 | } |
| 70 | |
| 71 | int temac_indirect_busywait(struct temac_local *lp) |
| 72 | { |
| 73 | long end = jiffies + 2; |
| 74 | |
| 75 | while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) { |
| 76 | if (end - jiffies <= 0) { |
| 77 | WARN_ON(1); |
| 78 | return -ETIMEDOUT; |
| 79 | } |
| 80 | msleep(1); |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * temac_indirect_in32 |
| 87 | * |
| 88 | * lp->indirect_mutex must be held when calling this function |
| 89 | */ |
| 90 | u32 temac_indirect_in32(struct temac_local *lp, int reg) |
| 91 | { |
| 92 | u32 val; |
| 93 | |
| 94 | if (temac_indirect_busywait(lp)) |
| 95 | return -ETIMEDOUT; |
| 96 | temac_iow(lp, XTE_CTL0_OFFSET, reg); |
| 97 | if (temac_indirect_busywait(lp)) |
| 98 | return -ETIMEDOUT; |
| 99 | val = temac_ior(lp, XTE_LSW0_OFFSET); |
| 100 | |
| 101 | return val; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * temac_indirect_out32 |
| 106 | * |
| 107 | * lp->indirect_mutex must be held when calling this function |
| 108 | */ |
| 109 | void temac_indirect_out32(struct temac_local *lp, int reg, u32 value) |
| 110 | { |
| 111 | if (temac_indirect_busywait(lp)) |
| 112 | return; |
| 113 | temac_iow(lp, XTE_LSW0_OFFSET, value); |
| 114 | temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg); |
| 115 | } |
| 116 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 117 | /** |
| 118 | * temac_dma_in32 - Memory mapped DMA read, this function expects a |
| 119 | * register input that is based on DCR word addresses which |
| 120 | * are then converted to memory mapped byte addresses |
| 121 | */ |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 122 | static u32 temac_dma_in32(struct temac_local *lp, int reg) |
| 123 | { |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 124 | return in_be32((u32 *)(lp->sdma_regs + (reg << 2))); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * temac_dma_out32 - Memory mapped DMA read, this function expects a |
| 129 | * register input that is based on DCR word addresses which |
| 130 | * are then converted to memory mapped byte addresses |
| 131 | */ |
| 132 | static void temac_dma_out32(struct temac_local *lp, int reg, u32 value) |
| 133 | { |
| 134 | out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value); |
| 135 | } |
| 136 | |
| 137 | /* DMA register access functions can be DCR based or memory mapped. |
| 138 | * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both |
| 139 | * memory mapped. |
| 140 | */ |
| 141 | #ifdef CONFIG_PPC_DCR |
| 142 | |
| 143 | /** |
| 144 | * temac_dma_dcr_in32 - DCR based DMA read |
| 145 | */ |
| 146 | static u32 temac_dma_dcr_in(struct temac_local *lp, int reg) |
| 147 | { |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 148 | return dcr_read(lp->sdma_dcrs, reg); |
| 149 | } |
| 150 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 151 | /** |
| 152 | * temac_dma_dcr_out32 - DCR based DMA write |
| 153 | */ |
| 154 | static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value) |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 155 | { |
| 156 | dcr_write(lp->sdma_dcrs, reg, value); |
| 157 | } |
| 158 | |
| 159 | /** |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 160 | * temac_dcr_setup - If the DMA is DCR based, then setup the address and |
| 161 | * I/O functions |
| 162 | */ |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 163 | static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op, |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 164 | struct device_node *np) |
| 165 | { |
| 166 | unsigned int dcrs; |
| 167 | |
| 168 | /* setup the dcr address mapping if it's in the device tree */ |
| 169 | |
| 170 | dcrs = dcr_resource_start(np, 0); |
| 171 | if (dcrs != 0) { |
| 172 | lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0)); |
| 173 | lp->dma_in = temac_dma_dcr_in; |
| 174 | lp->dma_out = temac_dma_dcr_out; |
| 175 | dev_dbg(&op->dev, "DCR base: %x\n", dcrs); |
| 176 | return 0; |
| 177 | } |
| 178 | /* no DCR in the device tree, indicate a failure */ |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | #else |
| 183 | |
| 184 | /* |
| 185 | * temac_dcr_setup - This is a stub for when DCR is not supported, |
| 186 | * such as with MicroBlaze |
| 187 | */ |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 188 | static int temac_dcr_setup(struct temac_local *lp, struct platform_device *op, |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 189 | struct device_node *np) |
| 190 | { |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | #endif |
| 195 | |
| 196 | /** |
Denis Kirjanov | 301e9d9 | 2010-07-08 10:24:51 +0000 | [diff] [blame] | 197 | * * temac_dma_bd_release - Release buffer descriptor rings |
| 198 | */ |
| 199 | static void temac_dma_bd_release(struct net_device *ndev) |
| 200 | { |
| 201 | struct temac_local *lp = netdev_priv(ndev); |
| 202 | int i; |
| 203 | |
| 204 | for (i = 0; i < RX_BD_NUM; i++) { |
| 205 | if (!lp->rx_skb[i]) |
| 206 | break; |
| 207 | else { |
| 208 | dma_unmap_single(ndev->dev.parent, lp->rx_bd_v[i].phys, |
| 209 | XTE_MAX_JUMBO_FRAME_SIZE, DMA_FROM_DEVICE); |
| 210 | dev_kfree_skb(lp->rx_skb[i]); |
| 211 | } |
| 212 | } |
| 213 | if (lp->rx_bd_v) |
| 214 | dma_free_coherent(ndev->dev.parent, |
| 215 | sizeof(*lp->rx_bd_v) * RX_BD_NUM, |
| 216 | lp->rx_bd_v, lp->rx_bd_p); |
| 217 | if (lp->tx_bd_v) |
| 218 | dma_free_coherent(ndev->dev.parent, |
| 219 | sizeof(*lp->tx_bd_v) * TX_BD_NUM, |
| 220 | lp->tx_bd_v, lp->tx_bd_p); |
| 221 | if (lp->rx_skb) |
| 222 | kfree(lp->rx_skb); |
| 223 | } |
| 224 | |
| 225 | /** |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 226 | * temac_dma_bd_init - Setup buffer descriptor rings |
| 227 | */ |
| 228 | static int temac_dma_bd_init(struct net_device *ndev) |
| 229 | { |
| 230 | struct temac_local *lp = netdev_priv(ndev); |
| 231 | struct sk_buff *skb; |
| 232 | int i; |
| 233 | |
Julia Lawall | 5d66fe9 | 2009-12-29 09:15:42 +0000 | [diff] [blame] | 234 | lp->rx_skb = kzalloc(sizeof(*lp->rx_skb) * RX_BD_NUM, GFP_KERNEL); |
Denis Kirjanov | fe62c29 | 2010-06-30 23:39:05 +0000 | [diff] [blame] | 235 | if (!lp->rx_skb) { |
| 236 | dev_err(&ndev->dev, |
| 237 | "can't allocate memory for DMA RX buffer\n"); |
| 238 | goto out; |
| 239 | } |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 240 | /* allocate the tx and rx ring buffer descriptors. */ |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 241 | /* returns a virtual address and a physical address. */ |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 242 | lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent, |
| 243 | sizeof(*lp->tx_bd_v) * TX_BD_NUM, |
| 244 | &lp->tx_bd_p, GFP_KERNEL); |
Denis Kirjanov | fe62c29 | 2010-06-30 23:39:05 +0000 | [diff] [blame] | 245 | if (!lp->tx_bd_v) { |
| 246 | dev_err(&ndev->dev, |
| 247 | "unable to allocate DMA TX buffer descriptors"); |
| 248 | goto out; |
| 249 | } |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 250 | lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent, |
| 251 | sizeof(*lp->rx_bd_v) * RX_BD_NUM, |
| 252 | &lp->rx_bd_p, GFP_KERNEL); |
Denis Kirjanov | fe62c29 | 2010-06-30 23:39:05 +0000 | [diff] [blame] | 253 | if (!lp->rx_bd_v) { |
| 254 | dev_err(&ndev->dev, |
| 255 | "unable to allocate DMA RX buffer descriptors"); |
| 256 | goto out; |
| 257 | } |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 258 | |
| 259 | memset(lp->tx_bd_v, 0, sizeof(*lp->tx_bd_v) * TX_BD_NUM); |
| 260 | for (i = 0; i < TX_BD_NUM; i++) { |
| 261 | lp->tx_bd_v[i].next = lp->tx_bd_p + |
| 262 | sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM); |
| 263 | } |
| 264 | |
| 265 | memset(lp->rx_bd_v, 0, sizeof(*lp->rx_bd_v) * RX_BD_NUM); |
| 266 | for (i = 0; i < RX_BD_NUM; i++) { |
| 267 | lp->rx_bd_v[i].next = lp->rx_bd_p + |
| 268 | sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM); |
| 269 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 270 | skb = netdev_alloc_skb_ip_align(ndev, |
| 271 | XTE_MAX_JUMBO_FRAME_SIZE); |
| 272 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 273 | if (skb == 0) { |
| 274 | dev_err(&ndev->dev, "alloc_skb error %d\n", i); |
Denis Kirjanov | fe62c29 | 2010-06-30 23:39:05 +0000 | [diff] [blame] | 275 | goto out; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 276 | } |
| 277 | lp->rx_skb[i] = skb; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 278 | /* returns physical address of skb->data */ |
| 279 | lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent, |
| 280 | skb->data, |
| 281 | XTE_MAX_JUMBO_FRAME_SIZE, |
| 282 | DMA_FROM_DEVICE); |
| 283 | lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE; |
| 284 | lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND; |
| 285 | } |
| 286 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 287 | lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 288 | CHNL_CTRL_IRQ_EN | |
| 289 | CHNL_CTRL_IRQ_DLY_EN | |
| 290 | CHNL_CTRL_IRQ_COAL_EN); |
| 291 | /* 0x10220483 */ |
| 292 | /* 0x00100483 */ |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 293 | lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 294 | CHNL_CTRL_IRQ_EN | |
| 295 | CHNL_CTRL_IRQ_DLY_EN | |
| 296 | CHNL_CTRL_IRQ_COAL_EN | |
| 297 | CHNL_CTRL_IRQ_IOE); |
| 298 | /* 0xff010283 */ |
| 299 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 300 | lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p); |
| 301 | lp->dma_out(lp, RX_TAILDESC_PTR, |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 302 | lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1))); |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 303 | lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 304 | |
| 305 | return 0; |
Denis Kirjanov | fe62c29 | 2010-06-30 23:39:05 +0000 | [diff] [blame] | 306 | |
| 307 | out: |
Denis Kirjanov | 301e9d9 | 2010-07-08 10:24:51 +0000 | [diff] [blame] | 308 | temac_dma_bd_release(ndev); |
Denis Kirjanov | fe62c29 | 2010-06-30 23:39:05 +0000 | [diff] [blame] | 309 | return -ENOMEM; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* --------------------------------------------------------------------- |
| 313 | * net_device_ops |
| 314 | */ |
| 315 | |
| 316 | static int temac_set_mac_address(struct net_device *ndev, void *address) |
| 317 | { |
| 318 | struct temac_local *lp = netdev_priv(ndev); |
| 319 | |
| 320 | if (address) |
| 321 | memcpy(ndev->dev_addr, address, ETH_ALEN); |
| 322 | |
| 323 | if (!is_valid_ether_addr(ndev->dev_addr)) |
| 324 | random_ether_addr(ndev->dev_addr); |
| 325 | |
| 326 | /* set up unicast MAC address filter set its mac address */ |
| 327 | mutex_lock(&lp->indirect_mutex); |
| 328 | temac_indirect_out32(lp, XTE_UAW0_OFFSET, |
| 329 | (ndev->dev_addr[0]) | |
| 330 | (ndev->dev_addr[1] << 8) | |
| 331 | (ndev->dev_addr[2] << 16) | |
| 332 | (ndev->dev_addr[3] << 24)); |
| 333 | /* There are reserved bits in EUAW1 |
| 334 | * so don't affect them Set MAC bits [47:32] in EUAW1 */ |
| 335 | temac_indirect_out32(lp, XTE_UAW1_OFFSET, |
| 336 | (ndev->dev_addr[4] & 0x000000ff) | |
| 337 | (ndev->dev_addr[5] << 8)); |
| 338 | mutex_unlock(&lp->indirect_mutex); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
Steven J. Magnani | 8ea7a37 | 2010-02-17 07:55:07 +0000 | [diff] [blame] | 343 | static int netdev_set_mac_address(struct net_device *ndev, void *p) |
| 344 | { |
| 345 | struct sockaddr *addr = p; |
| 346 | |
| 347 | return temac_set_mac_address(ndev, addr->sa_data); |
| 348 | } |
| 349 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 350 | static void temac_set_multicast_list(struct net_device *ndev) |
| 351 | { |
| 352 | struct temac_local *lp = netdev_priv(ndev); |
| 353 | u32 multi_addr_msw, multi_addr_lsw, val; |
| 354 | int i; |
| 355 | |
| 356 | mutex_lock(&lp->indirect_mutex); |
Joe Perches | 8e95a20 | 2009-12-03 07:58:21 +0000 | [diff] [blame] | 357 | if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) || |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 358 | netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) { |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 359 | /* |
| 360 | * We must make the kernel realise we had to move |
| 361 | * into promisc mode or we start all out war on |
| 362 | * the cable. If it was a promisc request the |
| 363 | * flag is already set. If not we assert it. |
| 364 | */ |
| 365 | ndev->flags |= IFF_PROMISC; |
| 366 | temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK); |
| 367 | dev_info(&ndev->dev, "Promiscuous mode enabled.\n"); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 368 | } else if (!netdev_mc_empty(ndev)) { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 369 | struct netdev_hw_addr *ha; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 370 | |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 371 | i = 0; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 372 | netdev_for_each_mc_addr(ha, ndev) { |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 373 | if (i >= MULTICAST_CAM_TABLE_NUM) |
| 374 | break; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 375 | multi_addr_msw = ((ha->addr[3] << 24) | |
| 376 | (ha->addr[2] << 16) | |
| 377 | (ha->addr[1] << 8) | |
| 378 | (ha->addr[0])); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 379 | temac_indirect_out32(lp, XTE_MAW0_OFFSET, |
| 380 | multi_addr_msw); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 381 | multi_addr_lsw = ((ha->addr[5] << 8) | |
| 382 | (ha->addr[4]) | (i << 16)); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 383 | temac_indirect_out32(lp, XTE_MAW1_OFFSET, |
| 384 | multi_addr_lsw); |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 385 | i++; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 386 | } |
| 387 | } else { |
| 388 | val = temac_indirect_in32(lp, XTE_AFM_OFFSET); |
| 389 | temac_indirect_out32(lp, XTE_AFM_OFFSET, |
| 390 | val & ~XTE_AFM_EPPRM_MASK); |
| 391 | temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0); |
| 392 | temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0); |
| 393 | dev_info(&ndev->dev, "Promiscuous mode disabled.\n"); |
| 394 | } |
| 395 | mutex_unlock(&lp->indirect_mutex); |
| 396 | } |
| 397 | |
| 398 | struct temac_option { |
| 399 | int flg; |
| 400 | u32 opt; |
| 401 | u32 reg; |
| 402 | u32 m_or; |
| 403 | u32 m_and; |
| 404 | } temac_options[] = { |
| 405 | /* Turn on jumbo packet support for both Rx and Tx */ |
| 406 | { |
| 407 | .opt = XTE_OPTION_JUMBO, |
| 408 | .reg = XTE_TXC_OFFSET, |
| 409 | .m_or = XTE_TXC_TXJMBO_MASK, |
| 410 | }, |
| 411 | { |
| 412 | .opt = XTE_OPTION_JUMBO, |
| 413 | .reg = XTE_RXC1_OFFSET, |
| 414 | .m_or =XTE_RXC1_RXJMBO_MASK, |
| 415 | }, |
| 416 | /* Turn on VLAN packet support for both Rx and Tx */ |
| 417 | { |
| 418 | .opt = XTE_OPTION_VLAN, |
| 419 | .reg = XTE_TXC_OFFSET, |
| 420 | .m_or =XTE_TXC_TXVLAN_MASK, |
| 421 | }, |
| 422 | { |
| 423 | .opt = XTE_OPTION_VLAN, |
| 424 | .reg = XTE_RXC1_OFFSET, |
| 425 | .m_or =XTE_RXC1_RXVLAN_MASK, |
| 426 | }, |
| 427 | /* Turn on FCS stripping on receive packets */ |
| 428 | { |
| 429 | .opt = XTE_OPTION_FCS_STRIP, |
| 430 | .reg = XTE_RXC1_OFFSET, |
| 431 | .m_or =XTE_RXC1_RXFCS_MASK, |
| 432 | }, |
| 433 | /* Turn on FCS insertion on transmit packets */ |
| 434 | { |
| 435 | .opt = XTE_OPTION_FCS_INSERT, |
| 436 | .reg = XTE_TXC_OFFSET, |
| 437 | .m_or =XTE_TXC_TXFCS_MASK, |
| 438 | }, |
| 439 | /* Turn on length/type field checking on receive packets */ |
| 440 | { |
| 441 | .opt = XTE_OPTION_LENTYPE_ERR, |
| 442 | .reg = XTE_RXC1_OFFSET, |
| 443 | .m_or =XTE_RXC1_RXLT_MASK, |
| 444 | }, |
| 445 | /* Turn on flow control */ |
| 446 | { |
| 447 | .opt = XTE_OPTION_FLOW_CONTROL, |
| 448 | .reg = XTE_FCC_OFFSET, |
| 449 | .m_or =XTE_FCC_RXFLO_MASK, |
| 450 | }, |
| 451 | /* Turn on flow control */ |
| 452 | { |
| 453 | .opt = XTE_OPTION_FLOW_CONTROL, |
| 454 | .reg = XTE_FCC_OFFSET, |
| 455 | .m_or =XTE_FCC_TXFLO_MASK, |
| 456 | }, |
| 457 | /* Turn on promiscuous frame filtering (all frames are received ) */ |
| 458 | { |
| 459 | .opt = XTE_OPTION_PROMISC, |
| 460 | .reg = XTE_AFM_OFFSET, |
| 461 | .m_or =XTE_AFM_EPPRM_MASK, |
| 462 | }, |
| 463 | /* Enable transmitter if not already enabled */ |
| 464 | { |
| 465 | .opt = XTE_OPTION_TXEN, |
| 466 | .reg = XTE_TXC_OFFSET, |
| 467 | .m_or =XTE_TXC_TXEN_MASK, |
| 468 | }, |
| 469 | /* Enable receiver? */ |
| 470 | { |
| 471 | .opt = XTE_OPTION_RXEN, |
| 472 | .reg = XTE_RXC1_OFFSET, |
| 473 | .m_or =XTE_RXC1_RXEN_MASK, |
| 474 | }, |
| 475 | {} |
| 476 | }; |
| 477 | |
| 478 | /** |
| 479 | * temac_setoptions |
| 480 | */ |
| 481 | static u32 temac_setoptions(struct net_device *ndev, u32 options) |
| 482 | { |
| 483 | struct temac_local *lp = netdev_priv(ndev); |
| 484 | struct temac_option *tp = &temac_options[0]; |
| 485 | int reg; |
| 486 | |
| 487 | mutex_lock(&lp->indirect_mutex); |
| 488 | while (tp->opt) { |
| 489 | reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or; |
| 490 | if (options & tp->opt) |
| 491 | reg |= tp->m_or; |
| 492 | temac_indirect_out32(lp, tp->reg, reg); |
| 493 | tp++; |
| 494 | } |
| 495 | lp->options |= options; |
| 496 | mutex_unlock(&lp->indirect_mutex); |
| 497 | |
Eric Dumazet | 807540b | 2010-09-23 05:40:09 +0000 | [diff] [blame] | 498 | return 0; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 501 | /* Initialize temac */ |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 502 | static void temac_device_reset(struct net_device *ndev) |
| 503 | { |
| 504 | struct temac_local *lp = netdev_priv(ndev); |
| 505 | u32 timeout; |
| 506 | u32 val; |
| 507 | |
| 508 | /* Perform a software reset */ |
| 509 | |
| 510 | /* 0x300 host enable bit ? */ |
| 511 | /* reset PHY through control register ?:1 */ |
| 512 | |
| 513 | dev_dbg(&ndev->dev, "%s()\n", __func__); |
| 514 | |
| 515 | mutex_lock(&lp->indirect_mutex); |
| 516 | /* Reset the receiver and wait for it to finish reset */ |
| 517 | temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK); |
| 518 | timeout = 1000; |
| 519 | while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) { |
| 520 | udelay(1); |
| 521 | if (--timeout == 0) { |
| 522 | dev_err(&ndev->dev, |
| 523 | "temac_device_reset RX reset timeout!!\n"); |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | /* Reset the transmitter and wait for it to finish reset */ |
| 529 | temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK); |
| 530 | timeout = 1000; |
| 531 | while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) { |
| 532 | udelay(1); |
| 533 | if (--timeout == 0) { |
| 534 | dev_err(&ndev->dev, |
| 535 | "temac_device_reset TX reset timeout!!\n"); |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /* Disable the receiver */ |
| 541 | val = temac_indirect_in32(lp, XTE_RXC1_OFFSET); |
| 542 | temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK); |
| 543 | |
| 544 | /* Reset Local Link (DMA) */ |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 545 | lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 546 | timeout = 1000; |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 547 | while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) { |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 548 | udelay(1); |
| 549 | if (--timeout == 0) { |
| 550 | dev_err(&ndev->dev, |
| 551 | "temac_device_reset DMA reset timeout!!\n"); |
| 552 | break; |
| 553 | } |
| 554 | } |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 555 | lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 556 | |
Denis Kirjanov | fe62c29 | 2010-06-30 23:39:05 +0000 | [diff] [blame] | 557 | if (temac_dma_bd_init(ndev)) { |
| 558 | dev_err(&ndev->dev, |
| 559 | "temac_device_reset descriptor allocation failed\n"); |
| 560 | } |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 561 | |
| 562 | temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0); |
| 563 | temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0); |
| 564 | temac_indirect_out32(lp, XTE_TXC_OFFSET, 0); |
| 565 | temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK); |
| 566 | |
| 567 | mutex_unlock(&lp->indirect_mutex); |
| 568 | |
| 569 | /* Sync default options with HW |
| 570 | * but leave receiver and transmitter disabled. */ |
| 571 | temac_setoptions(ndev, |
| 572 | lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN)); |
| 573 | |
| 574 | temac_set_mac_address(ndev, NULL); |
| 575 | |
| 576 | /* Set address filter table */ |
| 577 | temac_set_multicast_list(ndev); |
| 578 | if (temac_setoptions(ndev, lp->options)) |
| 579 | dev_err(&ndev->dev, "Error setting TEMAC options\n"); |
| 580 | |
| 581 | /* Init Driver variable */ |
Eric Dumazet | 1ae5dc3 | 2010-05-10 05:01:31 -0700 | [diff] [blame] | 582 | ndev->trans_start = jiffies; /* prevent tx timeout */ |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | void temac_adjust_link(struct net_device *ndev) |
| 586 | { |
| 587 | struct temac_local *lp = netdev_priv(ndev); |
| 588 | struct phy_device *phy = lp->phy_dev; |
| 589 | u32 mii_speed; |
| 590 | int link_state; |
| 591 | |
| 592 | /* hash together the state values to decide if something has changed */ |
| 593 | link_state = phy->speed | (phy->duplex << 1) | phy->link; |
| 594 | |
| 595 | mutex_lock(&lp->indirect_mutex); |
| 596 | if (lp->last_link != link_state) { |
| 597 | mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET); |
| 598 | mii_speed &= ~XTE_EMCFG_LINKSPD_MASK; |
| 599 | |
| 600 | switch (phy->speed) { |
| 601 | case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break; |
| 602 | case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break; |
| 603 | case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break; |
| 604 | } |
| 605 | |
| 606 | /* Write new speed setting out to TEMAC */ |
| 607 | temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed); |
| 608 | lp->last_link = link_state; |
| 609 | phy_print_status(phy); |
| 610 | } |
| 611 | mutex_unlock(&lp->indirect_mutex); |
| 612 | } |
| 613 | |
| 614 | static void temac_start_xmit_done(struct net_device *ndev) |
| 615 | { |
| 616 | struct temac_local *lp = netdev_priv(ndev); |
| 617 | struct cdmac_bd *cur_p; |
| 618 | unsigned int stat = 0; |
| 619 | |
| 620 | cur_p = &lp->tx_bd_v[lp->tx_bd_ci]; |
| 621 | stat = cur_p->app0; |
| 622 | |
| 623 | while (stat & STS_CTRL_APP0_CMPLT) { |
| 624 | dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len, |
| 625 | DMA_TO_DEVICE); |
| 626 | if (cur_p->app4) |
| 627 | dev_kfree_skb_irq((struct sk_buff *)cur_p->app4); |
| 628 | cur_p->app0 = 0; |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 629 | cur_p->app1 = 0; |
| 630 | cur_p->app2 = 0; |
| 631 | cur_p->app3 = 0; |
| 632 | cur_p->app4 = 0; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 633 | |
| 634 | ndev->stats.tx_packets++; |
| 635 | ndev->stats.tx_bytes += cur_p->len; |
| 636 | |
| 637 | lp->tx_bd_ci++; |
| 638 | if (lp->tx_bd_ci >= TX_BD_NUM) |
| 639 | lp->tx_bd_ci = 0; |
| 640 | |
| 641 | cur_p = &lp->tx_bd_v[lp->tx_bd_ci]; |
| 642 | stat = cur_p->app0; |
| 643 | } |
| 644 | |
| 645 | netif_wake_queue(ndev); |
| 646 | } |
| 647 | |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 648 | static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag) |
| 649 | { |
| 650 | struct cdmac_bd *cur_p; |
| 651 | int tail; |
| 652 | |
| 653 | tail = lp->tx_bd_tail; |
| 654 | cur_p = &lp->tx_bd_v[tail]; |
| 655 | |
| 656 | do { |
| 657 | if (cur_p->app0) |
| 658 | return NETDEV_TX_BUSY; |
| 659 | |
| 660 | tail++; |
| 661 | if (tail >= TX_BD_NUM) |
| 662 | tail = 0; |
| 663 | |
| 664 | cur_p = &lp->tx_bd_v[tail]; |
| 665 | num_frag--; |
| 666 | } while (num_frag >= 0); |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 671 | static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 672 | { |
| 673 | struct temac_local *lp = netdev_priv(ndev); |
| 674 | struct cdmac_bd *cur_p; |
| 675 | dma_addr_t start_p, tail_p; |
| 676 | int ii; |
| 677 | unsigned long num_frag; |
| 678 | skb_frag_t *frag; |
| 679 | |
| 680 | num_frag = skb_shinfo(skb)->nr_frags; |
| 681 | frag = &skb_shinfo(skb)->frags[0]; |
| 682 | start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail; |
| 683 | cur_p = &lp->tx_bd_v[lp->tx_bd_tail]; |
| 684 | |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 685 | if (temac_check_tx_bd_space(lp, num_frag)) { |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 686 | if (!netif_queue_stopped(ndev)) { |
| 687 | netif_stop_queue(ndev); |
| 688 | return NETDEV_TX_BUSY; |
| 689 | } |
| 690 | return NETDEV_TX_BUSY; |
| 691 | } |
| 692 | |
| 693 | cur_p->app0 = 0; |
| 694 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Michał Mirosław | 0d0b167 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 695 | unsigned int csum_start_off = skb_checksum_start_offset(skb); |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 696 | unsigned int csum_index_off = csum_start_off + skb->csum_offset; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 697 | |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 698 | cur_p->app0 |= 1; /* TX Checksum Enabled */ |
| 699 | cur_p->app1 = (csum_start_off << 16) | csum_index_off; |
| 700 | cur_p->app2 = 0; /* initial checksum seed */ |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 701 | } |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 702 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 703 | cur_p->app0 |= STS_CTRL_APP0_SOP; |
| 704 | cur_p->len = skb_headlen(skb); |
| 705 | cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len, |
| 706 | DMA_TO_DEVICE); |
| 707 | cur_p->app4 = (unsigned long)skb; |
| 708 | |
| 709 | for (ii = 0; ii < num_frag; ii++) { |
| 710 | lp->tx_bd_tail++; |
| 711 | if (lp->tx_bd_tail >= TX_BD_NUM) |
| 712 | lp->tx_bd_tail = 0; |
| 713 | |
| 714 | cur_p = &lp->tx_bd_v[lp->tx_bd_tail]; |
| 715 | cur_p->phys = dma_map_single(ndev->dev.parent, |
| 716 | (void *)page_address(frag->page) + |
| 717 | frag->page_offset, |
| 718 | frag->size, DMA_TO_DEVICE); |
| 719 | cur_p->len = frag->size; |
| 720 | cur_p->app0 = 0; |
| 721 | frag++; |
| 722 | } |
| 723 | cur_p->app0 |= STS_CTRL_APP0_EOP; |
| 724 | |
| 725 | tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail; |
| 726 | lp->tx_bd_tail++; |
| 727 | if (lp->tx_bd_tail >= TX_BD_NUM) |
| 728 | lp->tx_bd_tail = 0; |
| 729 | |
| 730 | /* Kick off the transfer */ |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 731 | lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */ |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 732 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 733 | return NETDEV_TX_OK; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
| 737 | static void ll_temac_recv(struct net_device *ndev) |
| 738 | { |
| 739 | struct temac_local *lp = netdev_priv(ndev); |
| 740 | struct sk_buff *skb, *new_skb; |
| 741 | unsigned int bdstat; |
| 742 | struct cdmac_bd *cur_p; |
| 743 | dma_addr_t tail_p; |
| 744 | int length; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 745 | unsigned long flags; |
| 746 | |
| 747 | spin_lock_irqsave(&lp->rx_lock, flags); |
| 748 | |
| 749 | tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci; |
| 750 | cur_p = &lp->rx_bd_v[lp->rx_bd_ci]; |
| 751 | |
| 752 | bdstat = cur_p->app0; |
| 753 | while ((bdstat & STS_CTRL_APP0_CMPLT)) { |
| 754 | |
| 755 | skb = lp->rx_skb[lp->rx_bd_ci]; |
Steven J. Magnani | c3b7c12 | 2010-02-17 07:14:20 +0000 | [diff] [blame] | 756 | length = cur_p->app4 & 0x3FFF; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 757 | |
John Linn | 33646d7 | 2010-04-08 07:08:01 +0000 | [diff] [blame] | 758 | dma_unmap_single(ndev->dev.parent, cur_p->phys, length, |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 759 | DMA_FROM_DEVICE); |
| 760 | |
| 761 | skb_put(skb, length); |
| 762 | skb->dev = ndev; |
| 763 | skb->protocol = eth_type_trans(skb, ndev); |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 764 | skb_checksum_none_assert(skb); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 765 | |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 766 | /* if we're doing rx csum offload, set it up */ |
| 767 | if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) && |
| 768 | (skb->protocol == __constant_htons(ETH_P_IP)) && |
| 769 | (skb->len > 64)) { |
| 770 | |
| 771 | skb->csum = cur_p->app3 & 0xFFFF; |
| 772 | skb->ip_summed = CHECKSUM_COMPLETE; |
| 773 | } |
| 774 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 775 | netif_rx(skb); |
| 776 | |
| 777 | ndev->stats.rx_packets++; |
| 778 | ndev->stats.rx_bytes += length; |
| 779 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 780 | new_skb = netdev_alloc_skb_ip_align(ndev, |
| 781 | XTE_MAX_JUMBO_FRAME_SIZE); |
| 782 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 783 | if (new_skb == 0) { |
| 784 | dev_err(&ndev->dev, "no memory for new sk_buff\n"); |
| 785 | spin_unlock_irqrestore(&lp->rx_lock, flags); |
| 786 | return; |
| 787 | } |
| 788 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 789 | cur_p->app0 = STS_CTRL_APP0_IRQONEND; |
| 790 | cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data, |
| 791 | XTE_MAX_JUMBO_FRAME_SIZE, |
| 792 | DMA_FROM_DEVICE); |
| 793 | cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE; |
| 794 | lp->rx_skb[lp->rx_bd_ci] = new_skb; |
| 795 | |
| 796 | lp->rx_bd_ci++; |
| 797 | if (lp->rx_bd_ci >= RX_BD_NUM) |
| 798 | lp->rx_bd_ci = 0; |
| 799 | |
| 800 | cur_p = &lp->rx_bd_v[lp->rx_bd_ci]; |
| 801 | bdstat = cur_p->app0; |
| 802 | } |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 803 | lp->dma_out(lp, RX_TAILDESC_PTR, tail_p); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 804 | |
| 805 | spin_unlock_irqrestore(&lp->rx_lock, flags); |
| 806 | } |
| 807 | |
| 808 | static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev) |
| 809 | { |
| 810 | struct net_device *ndev = _ndev; |
| 811 | struct temac_local *lp = netdev_priv(ndev); |
| 812 | unsigned int status; |
| 813 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 814 | status = lp->dma_in(lp, TX_IRQ_REG); |
| 815 | lp->dma_out(lp, TX_IRQ_REG, status); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 816 | |
| 817 | if (status & (IRQ_COAL | IRQ_DLY)) |
| 818 | temac_start_xmit_done(lp->ndev); |
| 819 | if (status & 0x080) |
| 820 | dev_err(&ndev->dev, "DMA error 0x%x\n", status); |
| 821 | |
| 822 | return IRQ_HANDLED; |
| 823 | } |
| 824 | |
| 825 | static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev) |
| 826 | { |
| 827 | struct net_device *ndev = _ndev; |
| 828 | struct temac_local *lp = netdev_priv(ndev); |
| 829 | unsigned int status; |
| 830 | |
| 831 | /* Read and clear the status registers */ |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 832 | status = lp->dma_in(lp, RX_IRQ_REG); |
| 833 | lp->dma_out(lp, RX_IRQ_REG, status); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 834 | |
| 835 | if (status & (IRQ_COAL | IRQ_DLY)) |
| 836 | ll_temac_recv(lp->ndev); |
| 837 | |
| 838 | return IRQ_HANDLED; |
| 839 | } |
| 840 | |
| 841 | static int temac_open(struct net_device *ndev) |
| 842 | { |
| 843 | struct temac_local *lp = netdev_priv(ndev); |
| 844 | int rc; |
| 845 | |
| 846 | dev_dbg(&ndev->dev, "temac_open()\n"); |
| 847 | |
| 848 | if (lp->phy_node) { |
| 849 | lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node, |
| 850 | temac_adjust_link, 0, 0); |
| 851 | if (!lp->phy_dev) { |
| 852 | dev_err(lp->dev, "of_phy_connect() failed\n"); |
| 853 | return -ENODEV; |
| 854 | } |
| 855 | |
| 856 | phy_start(lp->phy_dev); |
| 857 | } |
| 858 | |
| 859 | rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev); |
| 860 | if (rc) |
| 861 | goto err_tx_irq; |
| 862 | rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev); |
| 863 | if (rc) |
| 864 | goto err_rx_irq; |
| 865 | |
| 866 | temac_device_reset(ndev); |
| 867 | return 0; |
| 868 | |
| 869 | err_rx_irq: |
| 870 | free_irq(lp->tx_irq, ndev); |
| 871 | err_tx_irq: |
| 872 | if (lp->phy_dev) |
| 873 | phy_disconnect(lp->phy_dev); |
| 874 | lp->phy_dev = NULL; |
| 875 | dev_err(lp->dev, "request_irq() failed\n"); |
| 876 | return rc; |
| 877 | } |
| 878 | |
| 879 | static int temac_stop(struct net_device *ndev) |
| 880 | { |
| 881 | struct temac_local *lp = netdev_priv(ndev); |
| 882 | |
| 883 | dev_dbg(&ndev->dev, "temac_close()\n"); |
| 884 | |
| 885 | free_irq(lp->tx_irq, ndev); |
| 886 | free_irq(lp->rx_irq, ndev); |
| 887 | |
| 888 | if (lp->phy_dev) |
| 889 | phy_disconnect(lp->phy_dev); |
| 890 | lp->phy_dev = NULL; |
| 891 | |
Denis Kirjanov | 301e9d9 | 2010-07-08 10:24:51 +0000 | [diff] [blame] | 892 | temac_dma_bd_release(ndev); |
| 893 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 898 | static void |
| 899 | temac_poll_controller(struct net_device *ndev) |
| 900 | { |
| 901 | struct temac_local *lp = netdev_priv(ndev); |
| 902 | |
| 903 | disable_irq(lp->tx_irq); |
| 904 | disable_irq(lp->rx_irq); |
| 905 | |
Michal Simek | 8539992 | 2010-08-18 00:26:34 +0000 | [diff] [blame] | 906 | ll_temac_rx_irq(lp->tx_irq, ndev); |
| 907 | ll_temac_tx_irq(lp->rx_irq, ndev); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 908 | |
| 909 | enable_irq(lp->tx_irq); |
| 910 | enable_irq(lp->rx_irq); |
| 911 | } |
| 912 | #endif |
| 913 | |
| 914 | static const struct net_device_ops temac_netdev_ops = { |
| 915 | .ndo_open = temac_open, |
| 916 | .ndo_stop = temac_stop, |
| 917 | .ndo_start_xmit = temac_start_xmit, |
Steven J. Magnani | 8ea7a37 | 2010-02-17 07:55:07 +0000 | [diff] [blame] | 918 | .ndo_set_mac_address = netdev_set_mac_address, |
Denis Kirjanov | 60eb5fd | 2010-07-10 11:10:44 +0000 | [diff] [blame] | 919 | .ndo_validate_addr = eth_validate_addr, |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 920 | //.ndo_set_multicast_list = temac_set_multicast_list, |
| 921 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 922 | .ndo_poll_controller = temac_poll_controller, |
| 923 | #endif |
| 924 | }; |
| 925 | |
| 926 | /* --------------------------------------------------------------------- |
| 927 | * SYSFS device attributes |
| 928 | */ |
| 929 | static ssize_t temac_show_llink_regs(struct device *dev, |
| 930 | struct device_attribute *attr, char *buf) |
| 931 | { |
| 932 | struct net_device *ndev = dev_get_drvdata(dev); |
| 933 | struct temac_local *lp = netdev_priv(ndev); |
| 934 | int i, len = 0; |
| 935 | |
| 936 | for (i = 0; i < 0x11; i++) |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 937 | len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i), |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 938 | (i % 8) == 7 ? "\n" : " "); |
| 939 | len += sprintf(buf + len, "\n"); |
| 940 | |
| 941 | return len; |
| 942 | } |
| 943 | |
| 944 | static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL); |
| 945 | |
| 946 | static struct attribute *temac_device_attrs[] = { |
| 947 | &dev_attr_llink_regs.attr, |
| 948 | NULL, |
| 949 | }; |
| 950 | |
| 951 | static const struct attribute_group temac_attr_group = { |
| 952 | .attrs = temac_device_attrs, |
| 953 | }; |
| 954 | |
Michal Simek | 87e6097 | 2011-01-03 00:32:36 +0000 | [diff] [blame] | 955 | static int __devinit |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 956 | temac_of_probe(struct platform_device *op, const struct of_device_id *match) |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 957 | { |
| 958 | struct device_node *np; |
| 959 | struct temac_local *lp; |
| 960 | struct net_device *ndev; |
| 961 | const void *addr; |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 962 | __be32 *p; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 963 | int size, rc = 0; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 964 | |
| 965 | /* Init network device structure */ |
| 966 | ndev = alloc_etherdev(sizeof(*lp)); |
| 967 | if (!ndev) { |
| 968 | dev_err(&op->dev, "could not allocate device.\n"); |
| 969 | return -ENOMEM; |
| 970 | } |
| 971 | ether_setup(ndev); |
| 972 | dev_set_drvdata(&op->dev, ndev); |
| 973 | SET_NETDEV_DEV(ndev, &op->dev); |
| 974 | ndev->flags &= ~IFF_MULTICAST; /* clear multicast */ |
| 975 | ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST; |
| 976 | ndev->netdev_ops = &temac_netdev_ops; |
| 977 | #if 0 |
| 978 | ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */ |
| 979 | ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */ |
| 980 | ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */ |
| 981 | ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */ |
| 982 | ndev->features |= NETIF_F_HW_VLAN_TX; /* Transmit VLAN hw accel */ |
| 983 | ndev->features |= NETIF_F_HW_VLAN_RX; /* Receive VLAN hw acceleration */ |
| 984 | ndev->features |= NETIF_F_HW_VLAN_FILTER; /* Receive VLAN filtering */ |
| 985 | ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */ |
| 986 | ndev->features |= NETIF_F_GSO; /* Enable software GSO. */ |
| 987 | ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */ |
| 988 | ndev->features |= NETIF_F_LRO; /* large receive offload */ |
| 989 | #endif |
| 990 | |
| 991 | /* setup temac private info structure */ |
| 992 | lp = netdev_priv(ndev); |
| 993 | lp->ndev = ndev; |
| 994 | lp->dev = &op->dev; |
| 995 | lp->options = XTE_OPTION_DEFAULTS; |
| 996 | spin_lock_init(&lp->rx_lock); |
| 997 | mutex_init(&lp->indirect_mutex); |
| 998 | |
| 999 | /* map device registers */ |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 1000 | lp->regs = of_iomap(op->dev.of_node, 0); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1001 | if (!lp->regs) { |
| 1002 | dev_err(&op->dev, "could not map temac regs.\n"); |
| 1003 | goto nodev; |
| 1004 | } |
| 1005 | |
Brian Hill | 23ecc4b | 2010-05-26 20:44:30 -0700 | [diff] [blame] | 1006 | /* Setup checksum offload, but default to off if not specified */ |
| 1007 | lp->temac_features = 0; |
| 1008 | p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL); |
| 1009 | if (p && be32_to_cpu(*p)) { |
| 1010 | lp->temac_features |= TEMAC_FEATURE_TX_CSUM; |
| 1011 | /* Can checksum TCP/UDP over IPv4. */ |
| 1012 | ndev->features |= NETIF_F_IP_CSUM; |
| 1013 | } |
| 1014 | p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL); |
| 1015 | if (p && be32_to_cpu(*p)) |
| 1016 | lp->temac_features |= TEMAC_FEATURE_RX_CSUM; |
| 1017 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1018 | /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */ |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 1019 | np = of_parse_phandle(op->dev.of_node, "llink-connected", 0); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1020 | if (!np) { |
| 1021 | dev_err(&op->dev, "could not find DMA node\n"); |
Denis Kirjanov | dfe1e8e | 2010-07-05 21:44:20 +0000 | [diff] [blame] | 1022 | goto err_iounmap; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 1025 | /* Setup the DMA register accesses, could be DCR or memory mapped */ |
| 1026 | if (temac_dcr_setup(lp, op, np)) { |
| 1027 | |
| 1028 | /* no DCR in the device tree, try non-DCR */ |
| 1029 | lp->sdma_regs = of_iomap(np, 0); |
| 1030 | if (lp->sdma_regs) { |
| 1031 | lp->dma_in = temac_dma_in32; |
| 1032 | lp->dma_out = temac_dma_out32; |
| 1033 | dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs); |
| 1034 | } else { |
| 1035 | dev_err(&op->dev, "unable to map DMA registers\n"); |
Kulikov Vasiliy | 7cc36f6 | 2010-07-08 23:43:20 -0700 | [diff] [blame] | 1036 | of_node_put(np); |
Denis Kirjanov | dfe1e8e | 2010-07-05 21:44:20 +0000 | [diff] [blame] | 1037 | goto err_iounmap; |
John Linn | e44171f | 2010-04-08 07:08:02 +0000 | [diff] [blame] | 1038 | } |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1039 | } |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1040 | |
| 1041 | lp->rx_irq = irq_of_parse_and_map(np, 0); |
| 1042 | lp->tx_irq = irq_of_parse_and_map(np, 1); |
Kulikov Vasiliy | 7cc36f6 | 2010-07-08 23:43:20 -0700 | [diff] [blame] | 1043 | |
| 1044 | of_node_put(np); /* Finished with the DMA node; drop the reference */ |
| 1045 | |
Brian Hill | 755fae0 | 2010-05-26 20:42:18 -0700 | [diff] [blame] | 1046 | if ((lp->rx_irq == NO_IRQ) || (lp->tx_irq == NO_IRQ)) { |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1047 | dev_err(&op->dev, "could not determine irqs\n"); |
| 1048 | rc = -ENOMEM; |
Denis Kirjanov | dfe1e8e | 2010-07-05 21:44:20 +0000 | [diff] [blame] | 1049 | goto err_iounmap_2; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1052 | |
| 1053 | /* Retrieve the MAC address */ |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 1054 | addr = of_get_property(op->dev.of_node, "local-mac-address", &size); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1055 | if ((!addr) || (size != 6)) { |
| 1056 | dev_err(&op->dev, "could not find MAC address\n"); |
| 1057 | rc = -ENODEV; |
Denis Kirjanov | dfe1e8e | 2010-07-05 21:44:20 +0000 | [diff] [blame] | 1058 | goto err_iounmap_2; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1059 | } |
| 1060 | temac_set_mac_address(ndev, (void *)addr); |
| 1061 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 1062 | rc = temac_mdio_setup(lp, op->dev.of_node); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1063 | if (rc) |
| 1064 | dev_warn(&op->dev, "error registering MDIO bus\n"); |
| 1065 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 1066 | lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1067 | if (lp->phy_node) |
| 1068 | dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np); |
| 1069 | |
| 1070 | /* Add the device attributes */ |
| 1071 | rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group); |
| 1072 | if (rc) { |
| 1073 | dev_err(lp->dev, "Error creating sysfs files\n"); |
Denis Kirjanov | dfe1e8e | 2010-07-05 21:44:20 +0000 | [diff] [blame] | 1074 | goto err_iounmap_2; |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | rc = register_netdev(lp->ndev); |
| 1078 | if (rc) { |
| 1079 | dev_err(lp->dev, "register_netdev() error (%i)\n", rc); |
| 1080 | goto err_register_ndev; |
| 1081 | } |
| 1082 | |
| 1083 | return 0; |
| 1084 | |
| 1085 | err_register_ndev: |
| 1086 | sysfs_remove_group(&lp->dev->kobj, &temac_attr_group); |
Denis Kirjanov | dfe1e8e | 2010-07-05 21:44:20 +0000 | [diff] [blame] | 1087 | err_iounmap_2: |
| 1088 | if (lp->sdma_regs) |
| 1089 | iounmap(lp->sdma_regs); |
| 1090 | err_iounmap: |
| 1091 | iounmap(lp->regs); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1092 | nodev: |
| 1093 | free_netdev(ndev); |
| 1094 | ndev = NULL; |
| 1095 | return rc; |
| 1096 | } |
| 1097 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 1098 | static int __devexit temac_of_remove(struct platform_device *op) |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1099 | { |
| 1100 | struct net_device *ndev = dev_get_drvdata(&op->dev); |
| 1101 | struct temac_local *lp = netdev_priv(ndev); |
| 1102 | |
| 1103 | temac_mdio_teardown(lp); |
| 1104 | unregister_netdev(ndev); |
| 1105 | sysfs_remove_group(&lp->dev->kobj, &temac_attr_group); |
| 1106 | if (lp->phy_node) |
| 1107 | of_node_put(lp->phy_node); |
| 1108 | lp->phy_node = NULL; |
| 1109 | dev_set_drvdata(&op->dev, NULL); |
Denis Kirjanov | dfe1e8e | 2010-07-05 21:44:20 +0000 | [diff] [blame] | 1110 | iounmap(lp->regs); |
| 1111 | if (lp->sdma_regs) |
| 1112 | iounmap(lp->sdma_regs); |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1113 | free_netdev(ndev); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | static struct of_device_id temac_of_match[] __devinitdata = { |
| 1118 | { .compatible = "xlnx,xps-ll-temac-1.01.b", }, |
Steven J. Magnani | c3b7c12 | 2010-02-17 07:14:20 +0000 | [diff] [blame] | 1119 | { .compatible = "xlnx,xps-ll-temac-2.00.a", }, |
| 1120 | { .compatible = "xlnx,xps-ll-temac-2.02.a", }, |
| 1121 | { .compatible = "xlnx,xps-ll-temac-2.03.a", }, |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1122 | {}, |
| 1123 | }; |
| 1124 | MODULE_DEVICE_TABLE(of, temac_of_match); |
| 1125 | |
| 1126 | static struct of_platform_driver temac_of_driver = { |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1127 | .probe = temac_of_probe, |
| 1128 | .remove = __devexit_p(temac_of_remove), |
| 1129 | .driver = { |
| 1130 | .owner = THIS_MODULE, |
| 1131 | .name = "xilinx_temac", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 1132 | .of_match_table = temac_of_match, |
Grant Likely | 9274498 | 2009-04-25 12:53:39 +0000 | [diff] [blame] | 1133 | }, |
| 1134 | }; |
| 1135 | |
| 1136 | static int __init temac_init(void) |
| 1137 | { |
| 1138 | return of_register_platform_driver(&temac_of_driver); |
| 1139 | } |
| 1140 | module_init(temac_init); |
| 1141 | |
| 1142 | static void __exit temac_exit(void) |
| 1143 | { |
| 1144 | of_unregister_platform_driver(&temac_of_driver); |
| 1145 | } |
| 1146 | module_exit(temac_exit); |
| 1147 | |
| 1148 | MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver"); |
| 1149 | MODULE_AUTHOR("Yoshio Kashiwagi"); |
| 1150 | MODULE_LICENSE("GPL"); |