2 * Driver for Xilinx TEMAC Ethernet device
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.
8 * This is a driver for the Xilinx ll_temac ipcore which is often used
9 * in the Virtex and Spartan series of chips.
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.
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.
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>
38 #include <linux/of_device.h>
39 #include <linux/of_mdio.h>
40 #include <linux/of_platform.h>
41 #include <linux/skbuff.h>
42 #include <linux/spinlock.h>
43 #include <linux/tcp.h> /* needed for sizeof(tcphdr) */
44 #include <linux/udp.h> /* needed for sizeof(udphdr) */
45 #include <linux/phy.h>
49 #include <linux/slab.h>
56 /* ---------------------------------------------------------------------
57 * Low level register access functions
60 u32 temac_ior(struct temac_local *lp, int offset)
62 return in_be32((u32 *)(lp->regs + offset));
65 void temac_iow(struct temac_local *lp, int offset, u32 value)
67 out_be32((u32 *) (lp->regs + offset), value);
70 int temac_indirect_busywait(struct temac_local *lp)
72 long end = jiffies + 2;
74 while (!(temac_ior(lp, XTE_RDY0_OFFSET) & XTE_RDY0_HARD_ACS_RDY_MASK)) {
75 if (end - jiffies <= 0) {
87 * lp->indirect_mutex must be held when calling this function
89 u32 temac_indirect_in32(struct temac_local *lp, int reg)
93 if (temac_indirect_busywait(lp))
95 temac_iow(lp, XTE_CTL0_OFFSET, reg);
96 if (temac_indirect_busywait(lp))
98 val = temac_ior(lp, XTE_LSW0_OFFSET);
104 * temac_indirect_out32
106 * lp->indirect_mutex must be held when calling this function
108 void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
110 if (temac_indirect_busywait(lp))
112 temac_iow(lp, XTE_LSW0_OFFSET, value);
113 temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
117 * temac_dma_in32 - Memory mapped DMA read, this function expects a
118 * register input that is based on DCR word addresses which
119 * are then converted to memory mapped byte addresses
121 static u32 temac_dma_in32(struct temac_local *lp, int reg)
123 return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
127 * temac_dma_out32 - Memory mapped DMA read, this function expects a
128 * register input that is based on DCR word addresses which
129 * are then converted to memory mapped byte addresses
131 static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
133 out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
136 /* DMA register access functions can be DCR based or memory mapped.
137 * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
140 #ifdef CONFIG_PPC_DCR
143 * temac_dma_dcr_in32 - DCR based DMA read
145 static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
147 return dcr_read(lp->sdma_dcrs, reg);
151 * temac_dma_dcr_out32 - DCR based DMA write
153 static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
155 dcr_write(lp->sdma_dcrs, reg, value);
159 * temac_dcr_setup - If the DMA is DCR based, then setup the address and
162 static int temac_dcr_setup(struct temac_local *lp, struct of_device *op,
163 struct device_node *np)
167 /* setup the dcr address mapping if it's in the device tree */
169 dcrs = dcr_resource_start(np, 0);
171 lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
172 lp->dma_in = temac_dma_dcr_in;
173 lp->dma_out = temac_dma_dcr_out;
174 dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
177 /* no DCR in the device tree, indicate a failure */
184 * temac_dcr_setup - This is a stub for when DCR is not supported,
185 * such as with MicroBlaze
187 static int temac_dcr_setup(struct temac_local *lp, struct of_device *op,
188 struct device_node *np)
196 * temac_dma_bd_init - Setup buffer descriptor rings
198 static int temac_dma_bd_init(struct net_device *ndev)
200 struct temac_local *lp = netdev_priv(ndev);
204 lp->rx_skb = kzalloc(sizeof(*lp->rx_skb) * RX_BD_NUM, GFP_KERNEL);
207 "can't allocate memory for DMA RX buffer\n");
210 /* allocate the tx and rx ring buffer descriptors. */
211 /* returns a virtual addres and a physical address. */
212 lp->tx_bd_v = dma_alloc_coherent(ndev->dev.parent,
213 sizeof(*lp->tx_bd_v) * TX_BD_NUM,
214 &lp->tx_bd_p, GFP_KERNEL);
217 "unable to allocate DMA TX buffer descriptors");
220 lp->rx_bd_v = dma_alloc_coherent(ndev->dev.parent,
221 sizeof(*lp->rx_bd_v) * RX_BD_NUM,
222 &lp->rx_bd_p, GFP_KERNEL);
225 "unable to allocate DMA RX buffer descriptors");
229 memset(lp->tx_bd_v, 0, sizeof(*lp->tx_bd_v) * TX_BD_NUM);
230 for (i = 0; i < TX_BD_NUM; i++) {
231 lp->tx_bd_v[i].next = lp->tx_bd_p +
232 sizeof(*lp->tx_bd_v) * ((i + 1) % TX_BD_NUM);
235 memset(lp->rx_bd_v, 0, sizeof(*lp->rx_bd_v) * RX_BD_NUM);
236 for (i = 0; i < RX_BD_NUM; i++) {
237 lp->rx_bd_v[i].next = lp->rx_bd_p +
238 sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
240 skb = netdev_alloc_skb_ip_align(ndev,
241 XTE_MAX_JUMBO_FRAME_SIZE);
244 dev_err(&ndev->dev, "alloc_skb error %d\n", i);
248 /* returns physical address of skb->data */
249 lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
251 XTE_MAX_JUMBO_FRAME_SIZE,
253 lp->rx_bd_v[i].len = XTE_MAX_JUMBO_FRAME_SIZE;
254 lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
257 lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
259 CHNL_CTRL_IRQ_DLY_EN |
260 CHNL_CTRL_IRQ_COAL_EN);
263 lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
265 CHNL_CTRL_IRQ_DLY_EN |
266 CHNL_CTRL_IRQ_COAL_EN |
270 lp->dma_out(lp, RX_CURDESC_PTR, lp->rx_bd_p);
271 lp->dma_out(lp, RX_TAILDESC_PTR,
272 lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
273 lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
281 /* ---------------------------------------------------------------------
285 static int temac_set_mac_address(struct net_device *ndev, void *address)
287 struct temac_local *lp = netdev_priv(ndev);
290 memcpy(ndev->dev_addr, address, ETH_ALEN);
292 if (!is_valid_ether_addr(ndev->dev_addr))
293 random_ether_addr(ndev->dev_addr);
295 /* set up unicast MAC address filter set its mac address */
296 mutex_lock(&lp->indirect_mutex);
297 temac_indirect_out32(lp, XTE_UAW0_OFFSET,
298 (ndev->dev_addr[0]) |
299 (ndev->dev_addr[1] << 8) |
300 (ndev->dev_addr[2] << 16) |
301 (ndev->dev_addr[3] << 24));
302 /* There are reserved bits in EUAW1
303 * so don't affect them Set MAC bits [47:32] in EUAW1 */
304 temac_indirect_out32(lp, XTE_UAW1_OFFSET,
305 (ndev->dev_addr[4] & 0x000000ff) |
306 (ndev->dev_addr[5] << 8));
307 mutex_unlock(&lp->indirect_mutex);
312 static int netdev_set_mac_address(struct net_device *ndev, void *p)
314 struct sockaddr *addr = p;
316 return temac_set_mac_address(ndev, addr->sa_data);
319 static void temac_set_multicast_list(struct net_device *ndev)
321 struct temac_local *lp = netdev_priv(ndev);
322 u32 multi_addr_msw, multi_addr_lsw, val;
325 mutex_lock(&lp->indirect_mutex);
326 if (ndev->flags & (IFF_ALLMULTI | IFF_PROMISC) ||
327 netdev_mc_count(ndev) > MULTICAST_CAM_TABLE_NUM) {
329 * We must make the kernel realise we had to move
330 * into promisc mode or we start all out war on
331 * the cable. If it was a promisc request the
332 * flag is already set. If not we assert it.
334 ndev->flags |= IFF_PROMISC;
335 temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
336 dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
337 } else if (!netdev_mc_empty(ndev)) {
338 struct netdev_hw_addr *ha;
341 netdev_for_each_mc_addr(ha, ndev) {
342 if (i >= MULTICAST_CAM_TABLE_NUM)
344 multi_addr_msw = ((ha->addr[3] << 24) |
345 (ha->addr[2] << 16) |
348 temac_indirect_out32(lp, XTE_MAW0_OFFSET,
350 multi_addr_lsw = ((ha->addr[5] << 8) |
351 (ha->addr[4]) | (i << 16));
352 temac_indirect_out32(lp, XTE_MAW1_OFFSET,
357 val = temac_indirect_in32(lp, XTE_AFM_OFFSET);
358 temac_indirect_out32(lp, XTE_AFM_OFFSET,
359 val & ~XTE_AFM_EPPRM_MASK);
360 temac_indirect_out32(lp, XTE_MAW0_OFFSET, 0);
361 temac_indirect_out32(lp, XTE_MAW1_OFFSET, 0);
362 dev_info(&ndev->dev, "Promiscuous mode disabled.\n");
364 mutex_unlock(&lp->indirect_mutex);
367 struct temac_option {
373 } temac_options[] = {
374 /* Turn on jumbo packet support for both Rx and Tx */
376 .opt = XTE_OPTION_JUMBO,
377 .reg = XTE_TXC_OFFSET,
378 .m_or = XTE_TXC_TXJMBO_MASK,
381 .opt = XTE_OPTION_JUMBO,
382 .reg = XTE_RXC1_OFFSET,
383 .m_or =XTE_RXC1_RXJMBO_MASK,
385 /* Turn on VLAN packet support for both Rx and Tx */
387 .opt = XTE_OPTION_VLAN,
388 .reg = XTE_TXC_OFFSET,
389 .m_or =XTE_TXC_TXVLAN_MASK,
392 .opt = XTE_OPTION_VLAN,
393 .reg = XTE_RXC1_OFFSET,
394 .m_or =XTE_RXC1_RXVLAN_MASK,
396 /* Turn on FCS stripping on receive packets */
398 .opt = XTE_OPTION_FCS_STRIP,
399 .reg = XTE_RXC1_OFFSET,
400 .m_or =XTE_RXC1_RXFCS_MASK,
402 /* Turn on FCS insertion on transmit packets */
404 .opt = XTE_OPTION_FCS_INSERT,
405 .reg = XTE_TXC_OFFSET,
406 .m_or =XTE_TXC_TXFCS_MASK,
408 /* Turn on length/type field checking on receive packets */
410 .opt = XTE_OPTION_LENTYPE_ERR,
411 .reg = XTE_RXC1_OFFSET,
412 .m_or =XTE_RXC1_RXLT_MASK,
414 /* Turn on flow control */
416 .opt = XTE_OPTION_FLOW_CONTROL,
417 .reg = XTE_FCC_OFFSET,
418 .m_or =XTE_FCC_RXFLO_MASK,
420 /* Turn on flow control */
422 .opt = XTE_OPTION_FLOW_CONTROL,
423 .reg = XTE_FCC_OFFSET,
424 .m_or =XTE_FCC_TXFLO_MASK,
426 /* Turn on promiscuous frame filtering (all frames are received ) */
428 .opt = XTE_OPTION_PROMISC,
429 .reg = XTE_AFM_OFFSET,
430 .m_or =XTE_AFM_EPPRM_MASK,
432 /* Enable transmitter if not already enabled */
434 .opt = XTE_OPTION_TXEN,
435 .reg = XTE_TXC_OFFSET,
436 .m_or =XTE_TXC_TXEN_MASK,
438 /* Enable receiver? */
440 .opt = XTE_OPTION_RXEN,
441 .reg = XTE_RXC1_OFFSET,
442 .m_or =XTE_RXC1_RXEN_MASK,
450 static u32 temac_setoptions(struct net_device *ndev, u32 options)
452 struct temac_local *lp = netdev_priv(ndev);
453 struct temac_option *tp = &temac_options[0];
456 mutex_lock(&lp->indirect_mutex);
458 reg = temac_indirect_in32(lp, tp->reg) & ~tp->m_or;
459 if (options & tp->opt)
461 temac_indirect_out32(lp, tp->reg, reg);
464 lp->options |= options;
465 mutex_unlock(&lp->indirect_mutex);
470 /* Initilize temac */
471 static void temac_device_reset(struct net_device *ndev)
473 struct temac_local *lp = netdev_priv(ndev);
477 /* Perform a software reset */
479 /* 0x300 host enable bit ? */
480 /* reset PHY through control register ?:1 */
482 dev_dbg(&ndev->dev, "%s()\n", __func__);
484 mutex_lock(&lp->indirect_mutex);
485 /* Reset the receiver and wait for it to finish reset */
486 temac_indirect_out32(lp, XTE_RXC1_OFFSET, XTE_RXC1_RXRST_MASK);
488 while (temac_indirect_in32(lp, XTE_RXC1_OFFSET) & XTE_RXC1_RXRST_MASK) {
490 if (--timeout == 0) {
492 "temac_device_reset RX reset timeout!!\n");
497 /* Reset the transmitter and wait for it to finish reset */
498 temac_indirect_out32(lp, XTE_TXC_OFFSET, XTE_TXC_TXRST_MASK);
500 while (temac_indirect_in32(lp, XTE_TXC_OFFSET) & XTE_TXC_TXRST_MASK) {
502 if (--timeout == 0) {
504 "temac_device_reset TX reset timeout!!\n");
509 /* Disable the receiver */
510 val = temac_indirect_in32(lp, XTE_RXC1_OFFSET);
511 temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
513 /* Reset Local Link (DMA) */
514 lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
516 while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
518 if (--timeout == 0) {
520 "temac_device_reset DMA reset timeout!!\n");
524 lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
526 if (temac_dma_bd_init(ndev)) {
528 "temac_device_reset descriptor allocation failed\n");
531 temac_indirect_out32(lp, XTE_RXC0_OFFSET, 0);
532 temac_indirect_out32(lp, XTE_RXC1_OFFSET, 0);
533 temac_indirect_out32(lp, XTE_TXC_OFFSET, 0);
534 temac_indirect_out32(lp, XTE_FCC_OFFSET, XTE_FCC_RXFLO_MASK);
536 mutex_unlock(&lp->indirect_mutex);
538 /* Sync default options with HW
539 * but leave receiver and transmitter disabled. */
540 temac_setoptions(ndev,
541 lp->options & ~(XTE_OPTION_TXEN | XTE_OPTION_RXEN));
543 temac_set_mac_address(ndev, NULL);
545 /* Set address filter table */
546 temac_set_multicast_list(ndev);
547 if (temac_setoptions(ndev, lp->options))
548 dev_err(&ndev->dev, "Error setting TEMAC options\n");
550 /* Init Driver variable */
551 ndev->trans_start = jiffies; /* prevent tx timeout */
554 void temac_adjust_link(struct net_device *ndev)
556 struct temac_local *lp = netdev_priv(ndev);
557 struct phy_device *phy = lp->phy_dev;
561 /* hash together the state values to decide if something has changed */
562 link_state = phy->speed | (phy->duplex << 1) | phy->link;
564 mutex_lock(&lp->indirect_mutex);
565 if (lp->last_link != link_state) {
566 mii_speed = temac_indirect_in32(lp, XTE_EMCFG_OFFSET);
567 mii_speed &= ~XTE_EMCFG_LINKSPD_MASK;
569 switch (phy->speed) {
570 case SPEED_1000: mii_speed |= XTE_EMCFG_LINKSPD_1000; break;
571 case SPEED_100: mii_speed |= XTE_EMCFG_LINKSPD_100; break;
572 case SPEED_10: mii_speed |= XTE_EMCFG_LINKSPD_10; break;
575 /* Write new speed setting out to TEMAC */
576 temac_indirect_out32(lp, XTE_EMCFG_OFFSET, mii_speed);
577 lp->last_link = link_state;
578 phy_print_status(phy);
580 mutex_unlock(&lp->indirect_mutex);
583 static void temac_start_xmit_done(struct net_device *ndev)
585 struct temac_local *lp = netdev_priv(ndev);
586 struct cdmac_bd *cur_p;
587 unsigned int stat = 0;
589 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
592 while (stat & STS_CTRL_APP0_CMPLT) {
593 dma_unmap_single(ndev->dev.parent, cur_p->phys, cur_p->len,
596 dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
603 ndev->stats.tx_packets++;
604 ndev->stats.tx_bytes += cur_p->len;
607 if (lp->tx_bd_ci >= TX_BD_NUM)
610 cur_p = &lp->tx_bd_v[lp->tx_bd_ci];
614 netif_wake_queue(ndev);
617 static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
619 struct cdmac_bd *cur_p;
622 tail = lp->tx_bd_tail;
623 cur_p = &lp->tx_bd_v[tail];
627 return NETDEV_TX_BUSY;
630 if (tail >= TX_BD_NUM)
633 cur_p = &lp->tx_bd_v[tail];
635 } while (num_frag >= 0);
640 static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
642 struct temac_local *lp = netdev_priv(ndev);
643 struct cdmac_bd *cur_p;
644 dma_addr_t start_p, tail_p;
646 unsigned long num_frag;
649 num_frag = skb_shinfo(skb)->nr_frags;
650 frag = &skb_shinfo(skb)->frags[0];
651 start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
652 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
654 if (temac_check_tx_bd_space(lp, num_frag)) {
655 if (!netif_queue_stopped(ndev)) {
656 netif_stop_queue(ndev);
657 return NETDEV_TX_BUSY;
659 return NETDEV_TX_BUSY;
663 if (skb->ip_summed == CHECKSUM_PARTIAL) {
664 unsigned int csum_start_off = skb_transport_offset(skb);
665 unsigned int csum_index_off = csum_start_off + skb->csum_offset;
667 cur_p->app0 |= 1; /* TX Checksum Enabled */
668 cur_p->app1 = (csum_start_off << 16) | csum_index_off;
669 cur_p->app2 = 0; /* initial checksum seed */
672 cur_p->app0 |= STS_CTRL_APP0_SOP;
673 cur_p->len = skb_headlen(skb);
674 cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
676 cur_p->app4 = (unsigned long)skb;
678 for (ii = 0; ii < num_frag; ii++) {
680 if (lp->tx_bd_tail >= TX_BD_NUM)
683 cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
684 cur_p->phys = dma_map_single(ndev->dev.parent,
685 (void *)page_address(frag->page) +
687 frag->size, DMA_TO_DEVICE);
688 cur_p->len = frag->size;
692 cur_p->app0 |= STS_CTRL_APP0_EOP;
694 tail_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
696 if (lp->tx_bd_tail >= TX_BD_NUM)
699 /* Kick off the transfer */
700 lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
706 static void ll_temac_recv(struct net_device *ndev)
708 struct temac_local *lp = netdev_priv(ndev);
709 struct sk_buff *skb, *new_skb;
711 struct cdmac_bd *cur_p;
716 spin_lock_irqsave(&lp->rx_lock, flags);
718 tail_p = lp->rx_bd_p + sizeof(*lp->rx_bd_v) * lp->rx_bd_ci;
719 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
721 bdstat = cur_p->app0;
722 while ((bdstat & STS_CTRL_APP0_CMPLT)) {
724 skb = lp->rx_skb[lp->rx_bd_ci];
725 length = cur_p->app4 & 0x3FFF;
727 dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
730 skb_put(skb, length);
732 skb->protocol = eth_type_trans(skb, ndev);
733 skb->ip_summed = CHECKSUM_NONE;
735 /* if we're doing rx csum offload, set it up */
736 if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
737 (skb->protocol == __constant_htons(ETH_P_IP)) &&
740 skb->csum = cur_p->app3 & 0xFFFF;
741 skb->ip_summed = CHECKSUM_COMPLETE;
746 ndev->stats.rx_packets++;
747 ndev->stats.rx_bytes += length;
749 new_skb = netdev_alloc_skb_ip_align(ndev,
750 XTE_MAX_JUMBO_FRAME_SIZE);
753 dev_err(&ndev->dev, "no memory for new sk_buff\n");
754 spin_unlock_irqrestore(&lp->rx_lock, flags);
758 cur_p->app0 = STS_CTRL_APP0_IRQONEND;
759 cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
760 XTE_MAX_JUMBO_FRAME_SIZE,
762 cur_p->len = XTE_MAX_JUMBO_FRAME_SIZE;
763 lp->rx_skb[lp->rx_bd_ci] = new_skb;
766 if (lp->rx_bd_ci >= RX_BD_NUM)
769 cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
770 bdstat = cur_p->app0;
772 lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
774 spin_unlock_irqrestore(&lp->rx_lock, flags);
777 static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
779 struct net_device *ndev = _ndev;
780 struct temac_local *lp = netdev_priv(ndev);
783 status = lp->dma_in(lp, TX_IRQ_REG);
784 lp->dma_out(lp, TX_IRQ_REG, status);
786 if (status & (IRQ_COAL | IRQ_DLY))
787 temac_start_xmit_done(lp->ndev);
789 dev_err(&ndev->dev, "DMA error 0x%x\n", status);
794 static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
796 struct net_device *ndev = _ndev;
797 struct temac_local *lp = netdev_priv(ndev);
800 /* Read and clear the status registers */
801 status = lp->dma_in(lp, RX_IRQ_REG);
802 lp->dma_out(lp, RX_IRQ_REG, status);
804 if (status & (IRQ_COAL | IRQ_DLY))
805 ll_temac_recv(lp->ndev);
810 static int temac_open(struct net_device *ndev)
812 struct temac_local *lp = netdev_priv(ndev);
815 dev_dbg(&ndev->dev, "temac_open()\n");
818 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
819 temac_adjust_link, 0, 0);
821 dev_err(lp->dev, "of_phy_connect() failed\n");
825 phy_start(lp->phy_dev);
828 rc = request_irq(lp->tx_irq, ll_temac_tx_irq, 0, ndev->name, ndev);
831 rc = request_irq(lp->rx_irq, ll_temac_rx_irq, 0, ndev->name, ndev);
835 temac_device_reset(ndev);
839 free_irq(lp->tx_irq, ndev);
842 phy_disconnect(lp->phy_dev);
844 dev_err(lp->dev, "request_irq() failed\n");
848 static int temac_stop(struct net_device *ndev)
850 struct temac_local *lp = netdev_priv(ndev);
852 dev_dbg(&ndev->dev, "temac_close()\n");
854 free_irq(lp->tx_irq, ndev);
855 free_irq(lp->rx_irq, ndev);
858 phy_disconnect(lp->phy_dev);
864 #ifdef CONFIG_NET_POLL_CONTROLLER
866 temac_poll_controller(struct net_device *ndev)
868 struct temac_local *lp = netdev_priv(ndev);
870 disable_irq(lp->tx_irq);
871 disable_irq(lp->rx_irq);
873 ll_temac_rx_irq(lp->tx_irq, lp);
874 ll_temac_tx_irq(lp->rx_irq, lp);
876 enable_irq(lp->tx_irq);
877 enable_irq(lp->rx_irq);
881 static const struct net_device_ops temac_netdev_ops = {
882 .ndo_open = temac_open,
883 .ndo_stop = temac_stop,
884 .ndo_start_xmit = temac_start_xmit,
885 .ndo_set_mac_address = netdev_set_mac_address,
886 //.ndo_set_multicast_list = temac_set_multicast_list,
887 #ifdef CONFIG_NET_POLL_CONTROLLER
888 .ndo_poll_controller = temac_poll_controller,
892 /* ---------------------------------------------------------------------
893 * SYSFS device attributes
895 static ssize_t temac_show_llink_regs(struct device *dev,
896 struct device_attribute *attr, char *buf)
898 struct net_device *ndev = dev_get_drvdata(dev);
899 struct temac_local *lp = netdev_priv(ndev);
902 for (i = 0; i < 0x11; i++)
903 len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
904 (i % 8) == 7 ? "\n" : " ");
905 len += sprintf(buf + len, "\n");
910 static DEVICE_ATTR(llink_regs, 0440, temac_show_llink_regs, NULL);
912 static struct attribute *temac_device_attrs[] = {
913 &dev_attr_llink_regs.attr,
917 static const struct attribute_group temac_attr_group = {
918 .attrs = temac_device_attrs,
922 temac_of_probe(struct of_device *op, const struct of_device_id *match)
924 struct device_node *np;
925 struct temac_local *lp;
926 struct net_device *ndev;
931 /* Init network device structure */
932 ndev = alloc_etherdev(sizeof(*lp));
934 dev_err(&op->dev, "could not allocate device.\n");
938 dev_set_drvdata(&op->dev, ndev);
939 SET_NETDEV_DEV(ndev, &op->dev);
940 ndev->flags &= ~IFF_MULTICAST; /* clear multicast */
941 ndev->features = NETIF_F_SG | NETIF_F_FRAGLIST;
942 ndev->netdev_ops = &temac_netdev_ops;
944 ndev->features |= NETIF_F_IP_CSUM; /* Can checksum TCP/UDP over IPv4. */
945 ndev->features |= NETIF_F_HW_CSUM; /* Can checksum all the packets. */
946 ndev->features |= NETIF_F_IPV6_CSUM; /* Can checksum IPV6 TCP/UDP */
947 ndev->features |= NETIF_F_HIGHDMA; /* Can DMA to high memory. */
948 ndev->features |= NETIF_F_HW_VLAN_TX; /* Transmit VLAN hw accel */
949 ndev->features |= NETIF_F_HW_VLAN_RX; /* Receive VLAN hw acceleration */
950 ndev->features |= NETIF_F_HW_VLAN_FILTER; /* Receive VLAN filtering */
951 ndev->features |= NETIF_F_VLAN_CHALLENGED; /* cannot handle VLAN pkts */
952 ndev->features |= NETIF_F_GSO; /* Enable software GSO. */
953 ndev->features |= NETIF_F_MULTI_QUEUE; /* Has multiple TX/RX queues */
954 ndev->features |= NETIF_F_LRO; /* large receive offload */
957 /* setup temac private info structure */
958 lp = netdev_priv(ndev);
961 lp->options = XTE_OPTION_DEFAULTS;
962 spin_lock_init(&lp->rx_lock);
963 mutex_init(&lp->indirect_mutex);
965 /* map device registers */
966 lp->regs = of_iomap(op->dev.of_node, 0);
968 dev_err(&op->dev, "could not map temac regs.\n");
972 /* Setup checksum offload, but default to off if not specified */
973 lp->temac_features = 0;
974 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
975 if (p && be32_to_cpu(*p)) {
976 lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
977 /* Can checksum TCP/UDP over IPv4. */
978 ndev->features |= NETIF_F_IP_CSUM;
980 p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
981 if (p && be32_to_cpu(*p))
982 lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
984 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
985 np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
987 dev_err(&op->dev, "could not find DMA node\n");
991 /* Setup the DMA register accesses, could be DCR or memory mapped */
992 if (temac_dcr_setup(lp, op, np)) {
994 /* no DCR in the device tree, try non-DCR */
995 lp->sdma_regs = of_iomap(np, 0);
997 lp->dma_in = temac_dma_in32;
998 lp->dma_out = temac_dma_out32;
999 dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
1001 dev_err(&op->dev, "unable to map DMA registers\n");
1006 lp->rx_irq = irq_of_parse_and_map(np, 0);
1007 lp->tx_irq = irq_of_parse_and_map(np, 1);
1008 if ((lp->rx_irq == NO_IRQ) || (lp->tx_irq == NO_IRQ)) {
1009 dev_err(&op->dev, "could not determine irqs\n");
1014 of_node_put(np); /* Finished with the DMA node; drop the reference */
1016 /* Retrieve the MAC address */
1017 addr = of_get_property(op->dev.of_node, "local-mac-address", &size);
1018 if ((!addr) || (size != 6)) {
1019 dev_err(&op->dev, "could not find MAC address\n");
1023 temac_set_mac_address(ndev, (void *)addr);
1025 rc = temac_mdio_setup(lp, op->dev.of_node);
1027 dev_warn(&op->dev, "error registering MDIO bus\n");
1029 lp->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
1031 dev_dbg(lp->dev, "using PHY node %s (%p)\n", np->full_name, np);
1033 /* Add the device attributes */
1034 rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
1036 dev_err(lp->dev, "Error creating sysfs files\n");
1040 rc = register_netdev(lp->ndev);
1042 dev_err(lp->dev, "register_netdev() error (%i)\n", rc);
1043 goto err_register_ndev;
1049 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1052 iounmap(lp->sdma_regs);
1061 static int __devexit temac_of_remove(struct of_device *op)
1063 struct net_device *ndev = dev_get_drvdata(&op->dev);
1064 struct temac_local *lp = netdev_priv(ndev);
1066 temac_mdio_teardown(lp);
1067 unregister_netdev(ndev);
1068 sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
1070 of_node_put(lp->phy_node);
1071 lp->phy_node = NULL;
1072 dev_set_drvdata(&op->dev, NULL);
1075 iounmap(lp->sdma_regs);
1080 static struct of_device_id temac_of_match[] __devinitdata = {
1081 { .compatible = "xlnx,xps-ll-temac-1.01.b", },
1082 { .compatible = "xlnx,xps-ll-temac-2.00.a", },
1083 { .compatible = "xlnx,xps-ll-temac-2.02.a", },
1084 { .compatible = "xlnx,xps-ll-temac-2.03.a", },
1087 MODULE_DEVICE_TABLE(of, temac_of_match);
1089 static struct of_platform_driver temac_of_driver = {
1090 .probe = temac_of_probe,
1091 .remove = __devexit_p(temac_of_remove),
1093 .owner = THIS_MODULE,
1094 .name = "xilinx_temac",
1095 .of_match_table = temac_of_match,
1099 static int __init temac_init(void)
1101 return of_register_platform_driver(&temac_of_driver);
1103 module_init(temac_init);
1105 static void __exit temac_exit(void)
1107 of_unregister_platform_driver(&temac_of_driver);
1109 module_exit(temac_exit);
1111 MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1112 MODULE_AUTHOR("Yoshio Kashiwagi");
1113 MODULE_LICENSE("GPL");