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
);
205 /* allocate the tx and rx ring buffer descriptors. */
206 /* returns a virtual addres and a physical address. */
207 lp
->tx_bd_v
= dma_alloc_coherent(ndev
->dev
.parent
,
208 sizeof(*lp
->tx_bd_v
) * TX_BD_NUM
,
209 &lp
->tx_bd_p
, GFP_KERNEL
);
210 lp
->rx_bd_v
= dma_alloc_coherent(ndev
->dev
.parent
,
211 sizeof(*lp
->rx_bd_v
) * RX_BD_NUM
,
212 &lp
->rx_bd_p
, GFP_KERNEL
);
214 memset(lp
->tx_bd_v
, 0, sizeof(*lp
->tx_bd_v
) * TX_BD_NUM
);
215 for (i
= 0; i
< TX_BD_NUM
; i
++) {
216 lp
->tx_bd_v
[i
].next
= lp
->tx_bd_p
+
217 sizeof(*lp
->tx_bd_v
) * ((i
+ 1) % TX_BD_NUM
);
220 memset(lp
->rx_bd_v
, 0, sizeof(*lp
->rx_bd_v
) * RX_BD_NUM
);
221 for (i
= 0; i
< RX_BD_NUM
; i
++) {
222 lp
->rx_bd_v
[i
].next
= lp
->rx_bd_p
+
223 sizeof(*lp
->rx_bd_v
) * ((i
+ 1) % RX_BD_NUM
);
225 skb
= netdev_alloc_skb_ip_align(ndev
,
226 XTE_MAX_JUMBO_FRAME_SIZE
);
229 dev_err(&ndev
->dev
, "alloc_skb error %d\n", i
);
233 /* returns physical address of skb->data */
234 lp
->rx_bd_v
[i
].phys
= dma_map_single(ndev
->dev
.parent
,
236 XTE_MAX_JUMBO_FRAME_SIZE
,
238 lp
->rx_bd_v
[i
].len
= XTE_MAX_JUMBO_FRAME_SIZE
;
239 lp
->rx_bd_v
[i
].app0
= STS_CTRL_APP0_IRQONEND
;
242 lp
->dma_out(lp
, TX_CHNL_CTRL
, 0x10220400 |
244 CHNL_CTRL_IRQ_DLY_EN
|
245 CHNL_CTRL_IRQ_COAL_EN
);
248 lp
->dma_out(lp
, RX_CHNL_CTRL
, 0xff070000 |
250 CHNL_CTRL_IRQ_DLY_EN
|
251 CHNL_CTRL_IRQ_COAL_EN
|
255 lp
->dma_out(lp
, RX_CURDESC_PTR
, lp
->rx_bd_p
);
256 lp
->dma_out(lp
, RX_TAILDESC_PTR
,
257 lp
->rx_bd_p
+ (sizeof(*lp
->rx_bd_v
) * (RX_BD_NUM
- 1)));
258 lp
->dma_out(lp
, TX_CURDESC_PTR
, lp
->tx_bd_p
);
263 /* ---------------------------------------------------------------------
267 static int temac_set_mac_address(struct net_device
*ndev
, void *address
)
269 struct temac_local
*lp
= netdev_priv(ndev
);
272 memcpy(ndev
->dev_addr
, address
, ETH_ALEN
);
274 if (!is_valid_ether_addr(ndev
->dev_addr
))
275 random_ether_addr(ndev
->dev_addr
);
277 /* set up unicast MAC address filter set its mac address */
278 mutex_lock(&lp
->indirect_mutex
);
279 temac_indirect_out32(lp
, XTE_UAW0_OFFSET
,
280 (ndev
->dev_addr
[0]) |
281 (ndev
->dev_addr
[1] << 8) |
282 (ndev
->dev_addr
[2] << 16) |
283 (ndev
->dev_addr
[3] << 24));
284 /* There are reserved bits in EUAW1
285 * so don't affect them Set MAC bits [47:32] in EUAW1 */
286 temac_indirect_out32(lp
, XTE_UAW1_OFFSET
,
287 (ndev
->dev_addr
[4] & 0x000000ff) |
288 (ndev
->dev_addr
[5] << 8));
289 mutex_unlock(&lp
->indirect_mutex
);
294 static int netdev_set_mac_address(struct net_device
*ndev
, void *p
)
296 struct sockaddr
*addr
= p
;
298 return temac_set_mac_address(ndev
, addr
->sa_data
);
301 static void temac_set_multicast_list(struct net_device
*ndev
)
303 struct temac_local
*lp
= netdev_priv(ndev
);
304 u32 multi_addr_msw
, multi_addr_lsw
, val
;
307 mutex_lock(&lp
->indirect_mutex
);
308 if (ndev
->flags
& (IFF_ALLMULTI
| IFF_PROMISC
) ||
309 netdev_mc_count(ndev
) > MULTICAST_CAM_TABLE_NUM
) {
311 * We must make the kernel realise we had to move
312 * into promisc mode or we start all out war on
313 * the cable. If it was a promisc request the
314 * flag is already set. If not we assert it.
316 ndev
->flags
|= IFF_PROMISC
;
317 temac_indirect_out32(lp
, XTE_AFM_OFFSET
, XTE_AFM_EPPRM_MASK
);
318 dev_info(&ndev
->dev
, "Promiscuous mode enabled.\n");
319 } else if (!netdev_mc_empty(ndev
)) {
320 struct netdev_hw_addr
*ha
;
323 netdev_for_each_mc_addr(ha
, ndev
) {
324 if (i
>= MULTICAST_CAM_TABLE_NUM
)
326 multi_addr_msw
= ((ha
->addr
[3] << 24) |
327 (ha
->addr
[2] << 16) |
330 temac_indirect_out32(lp
, XTE_MAW0_OFFSET
,
332 multi_addr_lsw
= ((ha
->addr
[5] << 8) |
333 (ha
->addr
[4]) | (i
<< 16));
334 temac_indirect_out32(lp
, XTE_MAW1_OFFSET
,
339 val
= temac_indirect_in32(lp
, XTE_AFM_OFFSET
);
340 temac_indirect_out32(lp
, XTE_AFM_OFFSET
,
341 val
& ~XTE_AFM_EPPRM_MASK
);
342 temac_indirect_out32(lp
, XTE_MAW0_OFFSET
, 0);
343 temac_indirect_out32(lp
, XTE_MAW1_OFFSET
, 0);
344 dev_info(&ndev
->dev
, "Promiscuous mode disabled.\n");
346 mutex_unlock(&lp
->indirect_mutex
);
349 struct temac_option
{
355 } temac_options
[] = {
356 /* Turn on jumbo packet support for both Rx and Tx */
358 .opt
= XTE_OPTION_JUMBO
,
359 .reg
= XTE_TXC_OFFSET
,
360 .m_or
= XTE_TXC_TXJMBO_MASK
,
363 .opt
= XTE_OPTION_JUMBO
,
364 .reg
= XTE_RXC1_OFFSET
,
365 .m_or
=XTE_RXC1_RXJMBO_MASK
,
367 /* Turn on VLAN packet support for both Rx and Tx */
369 .opt
= XTE_OPTION_VLAN
,
370 .reg
= XTE_TXC_OFFSET
,
371 .m_or
=XTE_TXC_TXVLAN_MASK
,
374 .opt
= XTE_OPTION_VLAN
,
375 .reg
= XTE_RXC1_OFFSET
,
376 .m_or
=XTE_RXC1_RXVLAN_MASK
,
378 /* Turn on FCS stripping on receive packets */
380 .opt
= XTE_OPTION_FCS_STRIP
,
381 .reg
= XTE_RXC1_OFFSET
,
382 .m_or
=XTE_RXC1_RXFCS_MASK
,
384 /* Turn on FCS insertion on transmit packets */
386 .opt
= XTE_OPTION_FCS_INSERT
,
387 .reg
= XTE_TXC_OFFSET
,
388 .m_or
=XTE_TXC_TXFCS_MASK
,
390 /* Turn on length/type field checking on receive packets */
392 .opt
= XTE_OPTION_LENTYPE_ERR
,
393 .reg
= XTE_RXC1_OFFSET
,
394 .m_or
=XTE_RXC1_RXLT_MASK
,
396 /* Turn on flow control */
398 .opt
= XTE_OPTION_FLOW_CONTROL
,
399 .reg
= XTE_FCC_OFFSET
,
400 .m_or
=XTE_FCC_RXFLO_MASK
,
402 /* Turn on flow control */
404 .opt
= XTE_OPTION_FLOW_CONTROL
,
405 .reg
= XTE_FCC_OFFSET
,
406 .m_or
=XTE_FCC_TXFLO_MASK
,
408 /* Turn on promiscuous frame filtering (all frames are received ) */
410 .opt
= XTE_OPTION_PROMISC
,
411 .reg
= XTE_AFM_OFFSET
,
412 .m_or
=XTE_AFM_EPPRM_MASK
,
414 /* Enable transmitter if not already enabled */
416 .opt
= XTE_OPTION_TXEN
,
417 .reg
= XTE_TXC_OFFSET
,
418 .m_or
=XTE_TXC_TXEN_MASK
,
420 /* Enable receiver? */
422 .opt
= XTE_OPTION_RXEN
,
423 .reg
= XTE_RXC1_OFFSET
,
424 .m_or
=XTE_RXC1_RXEN_MASK
,
432 static u32
temac_setoptions(struct net_device
*ndev
, u32 options
)
434 struct temac_local
*lp
= netdev_priv(ndev
);
435 struct temac_option
*tp
= &temac_options
[0];
438 mutex_lock(&lp
->indirect_mutex
);
440 reg
= temac_indirect_in32(lp
, tp
->reg
) & ~tp
->m_or
;
441 if (options
& tp
->opt
)
443 temac_indirect_out32(lp
, tp
->reg
, reg
);
446 lp
->options
|= options
;
447 mutex_unlock(&lp
->indirect_mutex
);
452 /* Initilize temac */
453 static void temac_device_reset(struct net_device
*ndev
)
455 struct temac_local
*lp
= netdev_priv(ndev
);
459 /* Perform a software reset */
461 /* 0x300 host enable bit ? */
462 /* reset PHY through control register ?:1 */
464 dev_dbg(&ndev
->dev
, "%s()\n", __func__
);
466 mutex_lock(&lp
->indirect_mutex
);
467 /* Reset the receiver and wait for it to finish reset */
468 temac_indirect_out32(lp
, XTE_RXC1_OFFSET
, XTE_RXC1_RXRST_MASK
);
470 while (temac_indirect_in32(lp
, XTE_RXC1_OFFSET
) & XTE_RXC1_RXRST_MASK
) {
472 if (--timeout
== 0) {
474 "temac_device_reset RX reset timeout!!\n");
479 /* Reset the transmitter and wait for it to finish reset */
480 temac_indirect_out32(lp
, XTE_TXC_OFFSET
, XTE_TXC_TXRST_MASK
);
482 while (temac_indirect_in32(lp
, XTE_TXC_OFFSET
) & XTE_TXC_TXRST_MASK
) {
484 if (--timeout
== 0) {
486 "temac_device_reset TX reset timeout!!\n");
491 /* Disable the receiver */
492 val
= temac_indirect_in32(lp
, XTE_RXC1_OFFSET
);
493 temac_indirect_out32(lp
, XTE_RXC1_OFFSET
, val
& ~XTE_RXC1_RXEN_MASK
);
495 /* Reset Local Link (DMA) */
496 lp
->dma_out(lp
, DMA_CONTROL_REG
, DMA_CONTROL_RST
);
498 while (lp
->dma_in(lp
, DMA_CONTROL_REG
) & DMA_CONTROL_RST
) {
500 if (--timeout
== 0) {
502 "temac_device_reset DMA reset timeout!!\n");
506 lp
->dma_out(lp
, DMA_CONTROL_REG
, DMA_TAIL_ENABLE
);
508 temac_dma_bd_init(ndev
);
510 temac_indirect_out32(lp
, XTE_RXC0_OFFSET
, 0);
511 temac_indirect_out32(lp
, XTE_RXC1_OFFSET
, 0);
512 temac_indirect_out32(lp
, XTE_TXC_OFFSET
, 0);
513 temac_indirect_out32(lp
, XTE_FCC_OFFSET
, XTE_FCC_RXFLO_MASK
);
515 mutex_unlock(&lp
->indirect_mutex
);
517 /* Sync default options with HW
518 * but leave receiver and transmitter disabled. */
519 temac_setoptions(ndev
,
520 lp
->options
& ~(XTE_OPTION_TXEN
| XTE_OPTION_RXEN
));
522 temac_set_mac_address(ndev
, NULL
);
524 /* Set address filter table */
525 temac_set_multicast_list(ndev
);
526 if (temac_setoptions(ndev
, lp
->options
))
527 dev_err(&ndev
->dev
, "Error setting TEMAC options\n");
529 /* Init Driver variable */
530 ndev
->trans_start
= jiffies
; /* prevent tx timeout */
533 void temac_adjust_link(struct net_device
*ndev
)
535 struct temac_local
*lp
= netdev_priv(ndev
);
536 struct phy_device
*phy
= lp
->phy_dev
;
540 /* hash together the state values to decide if something has changed */
541 link_state
= phy
->speed
| (phy
->duplex
<< 1) | phy
->link
;
543 mutex_lock(&lp
->indirect_mutex
);
544 if (lp
->last_link
!= link_state
) {
545 mii_speed
= temac_indirect_in32(lp
, XTE_EMCFG_OFFSET
);
546 mii_speed
&= ~XTE_EMCFG_LINKSPD_MASK
;
548 switch (phy
->speed
) {
549 case SPEED_1000
: mii_speed
|= XTE_EMCFG_LINKSPD_1000
; break;
550 case SPEED_100
: mii_speed
|= XTE_EMCFG_LINKSPD_100
; break;
551 case SPEED_10
: mii_speed
|= XTE_EMCFG_LINKSPD_10
; break;
554 /* Write new speed setting out to TEMAC */
555 temac_indirect_out32(lp
, XTE_EMCFG_OFFSET
, mii_speed
);
556 lp
->last_link
= link_state
;
557 phy_print_status(phy
);
559 mutex_unlock(&lp
->indirect_mutex
);
562 static void temac_start_xmit_done(struct net_device
*ndev
)
564 struct temac_local
*lp
= netdev_priv(ndev
);
565 struct cdmac_bd
*cur_p
;
566 unsigned int stat
= 0;
568 cur_p
= &lp
->tx_bd_v
[lp
->tx_bd_ci
];
571 while (stat
& STS_CTRL_APP0_CMPLT
) {
572 dma_unmap_single(ndev
->dev
.parent
, cur_p
->phys
, cur_p
->len
,
575 dev_kfree_skb_irq((struct sk_buff
*)cur_p
->app4
);
582 ndev
->stats
.tx_packets
++;
583 ndev
->stats
.tx_bytes
+= cur_p
->len
;
586 if (lp
->tx_bd_ci
>= TX_BD_NUM
)
589 cur_p
= &lp
->tx_bd_v
[lp
->tx_bd_ci
];
593 netif_wake_queue(ndev
);
596 static inline int temac_check_tx_bd_space(struct temac_local
*lp
, int num_frag
)
598 struct cdmac_bd
*cur_p
;
601 tail
= lp
->tx_bd_tail
;
602 cur_p
= &lp
->tx_bd_v
[tail
];
606 return NETDEV_TX_BUSY
;
609 if (tail
>= TX_BD_NUM
)
612 cur_p
= &lp
->tx_bd_v
[tail
];
614 } while (num_frag
>= 0);
619 static int temac_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
621 struct temac_local
*lp
= netdev_priv(ndev
);
622 struct cdmac_bd
*cur_p
;
623 dma_addr_t start_p
, tail_p
;
625 unsigned long num_frag
;
628 num_frag
= skb_shinfo(skb
)->nr_frags
;
629 frag
= &skb_shinfo(skb
)->frags
[0];
630 start_p
= lp
->tx_bd_p
+ sizeof(*lp
->tx_bd_v
) * lp
->tx_bd_tail
;
631 cur_p
= &lp
->tx_bd_v
[lp
->tx_bd_tail
];
633 if (temac_check_tx_bd_space(lp
, num_frag
)) {
634 if (!netif_queue_stopped(ndev
)) {
635 netif_stop_queue(ndev
);
636 return NETDEV_TX_BUSY
;
638 return NETDEV_TX_BUSY
;
642 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
643 unsigned int csum_start_off
= skb_transport_offset(skb
);
644 unsigned int csum_index_off
= csum_start_off
+ skb
->csum_offset
;
646 cur_p
->app0
|= 1; /* TX Checksum Enabled */
647 cur_p
->app1
= (csum_start_off
<< 16) | csum_index_off
;
648 cur_p
->app2
= 0; /* initial checksum seed */
651 cur_p
->app0
|= STS_CTRL_APP0_SOP
;
652 cur_p
->len
= skb_headlen(skb
);
653 cur_p
->phys
= dma_map_single(ndev
->dev
.parent
, skb
->data
, skb
->len
,
655 cur_p
->app4
= (unsigned long)skb
;
657 for (ii
= 0; ii
< num_frag
; ii
++) {
659 if (lp
->tx_bd_tail
>= TX_BD_NUM
)
662 cur_p
= &lp
->tx_bd_v
[lp
->tx_bd_tail
];
663 cur_p
->phys
= dma_map_single(ndev
->dev
.parent
,
664 (void *)page_address(frag
->page
) +
666 frag
->size
, DMA_TO_DEVICE
);
667 cur_p
->len
= frag
->size
;
671 cur_p
->app0
|= STS_CTRL_APP0_EOP
;
673 tail_p
= lp
->tx_bd_p
+ sizeof(*lp
->tx_bd_v
) * lp
->tx_bd_tail
;
675 if (lp
->tx_bd_tail
>= TX_BD_NUM
)
678 /* Kick off the transfer */
679 lp
->dma_out(lp
, TX_TAILDESC_PTR
, tail_p
); /* DMA start */
685 static void ll_temac_recv(struct net_device
*ndev
)
687 struct temac_local
*lp
= netdev_priv(ndev
);
688 struct sk_buff
*skb
, *new_skb
;
690 struct cdmac_bd
*cur_p
;
695 spin_lock_irqsave(&lp
->rx_lock
, flags
);
697 tail_p
= lp
->rx_bd_p
+ sizeof(*lp
->rx_bd_v
) * lp
->rx_bd_ci
;
698 cur_p
= &lp
->rx_bd_v
[lp
->rx_bd_ci
];
700 bdstat
= cur_p
->app0
;
701 while ((bdstat
& STS_CTRL_APP0_CMPLT
)) {
703 skb
= lp
->rx_skb
[lp
->rx_bd_ci
];
704 length
= cur_p
->app4
& 0x3FFF;
706 dma_unmap_single(ndev
->dev
.parent
, cur_p
->phys
, length
,
709 skb_put(skb
, length
);
711 skb
->protocol
= eth_type_trans(skb
, ndev
);
712 skb
->ip_summed
= CHECKSUM_NONE
;
714 /* if we're doing rx csum offload, set it up */
715 if (((lp
->temac_features
& TEMAC_FEATURE_RX_CSUM
) != 0) &&
716 (skb
->protocol
== __constant_htons(ETH_P_IP
)) &&
719 skb
->csum
= cur_p
->app3
& 0xFFFF;
720 skb
->ip_summed
= CHECKSUM_COMPLETE
;
725 ndev
->stats
.rx_packets
++;
726 ndev
->stats
.rx_bytes
+= length
;
728 new_skb
= netdev_alloc_skb_ip_align(ndev
,
729 XTE_MAX_JUMBO_FRAME_SIZE
);
732 dev_err(&ndev
->dev
, "no memory for new sk_buff\n");
733 spin_unlock_irqrestore(&lp
->rx_lock
, flags
);
737 cur_p
->app0
= STS_CTRL_APP0_IRQONEND
;
738 cur_p
->phys
= dma_map_single(ndev
->dev
.parent
, new_skb
->data
,
739 XTE_MAX_JUMBO_FRAME_SIZE
,
741 cur_p
->len
= XTE_MAX_JUMBO_FRAME_SIZE
;
742 lp
->rx_skb
[lp
->rx_bd_ci
] = new_skb
;
745 if (lp
->rx_bd_ci
>= RX_BD_NUM
)
748 cur_p
= &lp
->rx_bd_v
[lp
->rx_bd_ci
];
749 bdstat
= cur_p
->app0
;
751 lp
->dma_out(lp
, RX_TAILDESC_PTR
, tail_p
);
753 spin_unlock_irqrestore(&lp
->rx_lock
, flags
);
756 static irqreturn_t
ll_temac_tx_irq(int irq
, void *_ndev
)
758 struct net_device
*ndev
= _ndev
;
759 struct temac_local
*lp
= netdev_priv(ndev
);
762 status
= lp
->dma_in(lp
, TX_IRQ_REG
);
763 lp
->dma_out(lp
, TX_IRQ_REG
, status
);
765 if (status
& (IRQ_COAL
| IRQ_DLY
))
766 temac_start_xmit_done(lp
->ndev
);
768 dev_err(&ndev
->dev
, "DMA error 0x%x\n", status
);
773 static irqreturn_t
ll_temac_rx_irq(int irq
, void *_ndev
)
775 struct net_device
*ndev
= _ndev
;
776 struct temac_local
*lp
= netdev_priv(ndev
);
779 /* Read and clear the status registers */
780 status
= lp
->dma_in(lp
, RX_IRQ_REG
);
781 lp
->dma_out(lp
, RX_IRQ_REG
, status
);
783 if (status
& (IRQ_COAL
| IRQ_DLY
))
784 ll_temac_recv(lp
->ndev
);
789 static int temac_open(struct net_device
*ndev
)
791 struct temac_local
*lp
= netdev_priv(ndev
);
794 dev_dbg(&ndev
->dev
, "temac_open()\n");
797 lp
->phy_dev
= of_phy_connect(lp
->ndev
, lp
->phy_node
,
798 temac_adjust_link
, 0, 0);
800 dev_err(lp
->dev
, "of_phy_connect() failed\n");
804 phy_start(lp
->phy_dev
);
807 rc
= request_irq(lp
->tx_irq
, ll_temac_tx_irq
, 0, ndev
->name
, ndev
);
810 rc
= request_irq(lp
->rx_irq
, ll_temac_rx_irq
, 0, ndev
->name
, ndev
);
814 temac_device_reset(ndev
);
818 free_irq(lp
->tx_irq
, ndev
);
821 phy_disconnect(lp
->phy_dev
);
823 dev_err(lp
->dev
, "request_irq() failed\n");
827 static int temac_stop(struct net_device
*ndev
)
829 struct temac_local
*lp
= netdev_priv(ndev
);
831 dev_dbg(&ndev
->dev
, "temac_close()\n");
833 free_irq(lp
->tx_irq
, ndev
);
834 free_irq(lp
->rx_irq
, ndev
);
837 phy_disconnect(lp
->phy_dev
);
843 #ifdef CONFIG_NET_POLL_CONTROLLER
845 temac_poll_controller(struct net_device
*ndev
)
847 struct temac_local
*lp
= netdev_priv(ndev
);
849 disable_irq(lp
->tx_irq
);
850 disable_irq(lp
->rx_irq
);
852 ll_temac_rx_irq(lp
->tx_irq
, lp
);
853 ll_temac_tx_irq(lp
->rx_irq
, lp
);
855 enable_irq(lp
->tx_irq
);
856 enable_irq(lp
->rx_irq
);
860 static const struct net_device_ops temac_netdev_ops
= {
861 .ndo_open
= temac_open
,
862 .ndo_stop
= temac_stop
,
863 .ndo_start_xmit
= temac_start_xmit
,
864 .ndo_set_mac_address
= netdev_set_mac_address
,
865 //.ndo_set_multicast_list = temac_set_multicast_list,
866 #ifdef CONFIG_NET_POLL_CONTROLLER
867 .ndo_poll_controller
= temac_poll_controller
,
871 /* ---------------------------------------------------------------------
872 * SYSFS device attributes
874 static ssize_t
temac_show_llink_regs(struct device
*dev
,
875 struct device_attribute
*attr
, char *buf
)
877 struct net_device
*ndev
= dev_get_drvdata(dev
);
878 struct temac_local
*lp
= netdev_priv(ndev
);
881 for (i
= 0; i
< 0x11; i
++)
882 len
+= sprintf(buf
+ len
, "%.8x%s", lp
->dma_in(lp
, i
),
883 (i
% 8) == 7 ? "\n" : " ");
884 len
+= sprintf(buf
+ len
, "\n");
889 static DEVICE_ATTR(llink_regs
, 0440, temac_show_llink_regs
, NULL
);
891 static struct attribute
*temac_device_attrs
[] = {
892 &dev_attr_llink_regs
.attr
,
896 static const struct attribute_group temac_attr_group
= {
897 .attrs
= temac_device_attrs
,
901 temac_of_probe(struct of_device
*op
, const struct of_device_id
*match
)
903 struct device_node
*np
;
904 struct temac_local
*lp
;
905 struct net_device
*ndev
;
910 /* Init network device structure */
911 ndev
= alloc_etherdev(sizeof(*lp
));
913 dev_err(&op
->dev
, "could not allocate device.\n");
917 dev_set_drvdata(&op
->dev
, ndev
);
918 SET_NETDEV_DEV(ndev
, &op
->dev
);
919 ndev
->flags
&= ~IFF_MULTICAST
; /* clear multicast */
920 ndev
->features
= NETIF_F_SG
| NETIF_F_FRAGLIST
;
921 ndev
->netdev_ops
= &temac_netdev_ops
;
923 ndev
->features
|= NETIF_F_IP_CSUM
; /* Can checksum TCP/UDP over IPv4. */
924 ndev
->features
|= NETIF_F_HW_CSUM
; /* Can checksum all the packets. */
925 ndev
->features
|= NETIF_F_IPV6_CSUM
; /* Can checksum IPV6 TCP/UDP */
926 ndev
->features
|= NETIF_F_HIGHDMA
; /* Can DMA to high memory. */
927 ndev
->features
|= NETIF_F_HW_VLAN_TX
; /* Transmit VLAN hw accel */
928 ndev
->features
|= NETIF_F_HW_VLAN_RX
; /* Receive VLAN hw acceleration */
929 ndev
->features
|= NETIF_F_HW_VLAN_FILTER
; /* Receive VLAN filtering */
930 ndev
->features
|= NETIF_F_VLAN_CHALLENGED
; /* cannot handle VLAN pkts */
931 ndev
->features
|= NETIF_F_GSO
; /* Enable software GSO. */
932 ndev
->features
|= NETIF_F_MULTI_QUEUE
; /* Has multiple TX/RX queues */
933 ndev
->features
|= NETIF_F_LRO
; /* large receive offload */
936 /* setup temac private info structure */
937 lp
= netdev_priv(ndev
);
940 lp
->options
= XTE_OPTION_DEFAULTS
;
941 spin_lock_init(&lp
->rx_lock
);
942 mutex_init(&lp
->indirect_mutex
);
944 /* map device registers */
945 lp
->regs
= of_iomap(op
->dev
.of_node
, 0);
947 dev_err(&op
->dev
, "could not map temac regs.\n");
951 /* Setup checksum offload, but default to off if not specified */
952 lp
->temac_features
= 0;
953 p
= (__be32
*)of_get_property(op
->dev
.of_node
, "xlnx,txcsum", NULL
);
954 if (p
&& be32_to_cpu(*p
)) {
955 lp
->temac_features
|= TEMAC_FEATURE_TX_CSUM
;
956 /* Can checksum TCP/UDP over IPv4. */
957 ndev
->features
|= NETIF_F_IP_CSUM
;
959 p
= (__be32
*)of_get_property(op
->dev
.of_node
, "xlnx,rxcsum", NULL
);
960 if (p
&& be32_to_cpu(*p
))
961 lp
->temac_features
|= TEMAC_FEATURE_RX_CSUM
;
963 /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
964 np
= of_parse_phandle(op
->dev
.of_node
, "llink-connected", 0);
966 dev_err(&op
->dev
, "could not find DMA node\n");
970 /* Setup the DMA register accesses, could be DCR or memory mapped */
971 if (temac_dcr_setup(lp
, op
, np
)) {
973 /* no DCR in the device tree, try non-DCR */
974 lp
->sdma_regs
= of_iomap(np
, 0);
976 lp
->dma_in
= temac_dma_in32
;
977 lp
->dma_out
= temac_dma_out32
;
978 dev_dbg(&op
->dev
, "MEM base: %p\n", lp
->sdma_regs
);
980 dev_err(&op
->dev
, "unable to map DMA registers\n");
985 lp
->rx_irq
= irq_of_parse_and_map(np
, 0);
986 lp
->tx_irq
= irq_of_parse_and_map(np
, 1);
987 if ((lp
->rx_irq
== NO_IRQ
) || (lp
->tx_irq
== NO_IRQ
)) {
988 dev_err(&op
->dev
, "could not determine irqs\n");
993 of_node_put(np
); /* Finished with the DMA node; drop the reference */
995 /* Retrieve the MAC address */
996 addr
= of_get_property(op
->dev
.of_node
, "local-mac-address", &size
);
997 if ((!addr
) || (size
!= 6)) {
998 dev_err(&op
->dev
, "could not find MAC address\n");
1002 temac_set_mac_address(ndev
, (void *)addr
);
1004 rc
= temac_mdio_setup(lp
, op
->dev
.of_node
);
1006 dev_warn(&op
->dev
, "error registering MDIO bus\n");
1008 lp
->phy_node
= of_parse_phandle(op
->dev
.of_node
, "phy-handle", 0);
1010 dev_dbg(lp
->dev
, "using PHY node %s (%p)\n", np
->full_name
, np
);
1012 /* Add the device attributes */
1013 rc
= sysfs_create_group(&lp
->dev
->kobj
, &temac_attr_group
);
1015 dev_err(lp
->dev
, "Error creating sysfs files\n");
1019 rc
= register_netdev(lp
->ndev
);
1021 dev_err(lp
->dev
, "register_netdev() error (%i)\n", rc
);
1022 goto err_register_ndev
;
1028 sysfs_remove_group(&lp
->dev
->kobj
, &temac_attr_group
);
1035 static int __devexit
temac_of_remove(struct of_device
*op
)
1037 struct net_device
*ndev
= dev_get_drvdata(&op
->dev
);
1038 struct temac_local
*lp
= netdev_priv(ndev
);
1040 temac_mdio_teardown(lp
);
1041 unregister_netdev(ndev
);
1042 sysfs_remove_group(&lp
->dev
->kobj
, &temac_attr_group
);
1044 of_node_put(lp
->phy_node
);
1045 lp
->phy_node
= NULL
;
1046 dev_set_drvdata(&op
->dev
, NULL
);
1051 static struct of_device_id temac_of_match
[] __devinitdata
= {
1052 { .compatible
= "xlnx,xps-ll-temac-1.01.b", },
1053 { .compatible
= "xlnx,xps-ll-temac-2.00.a", },
1054 { .compatible
= "xlnx,xps-ll-temac-2.02.a", },
1055 { .compatible
= "xlnx,xps-ll-temac-2.03.a", },
1058 MODULE_DEVICE_TABLE(of
, temac_of_match
);
1060 static struct of_platform_driver temac_of_driver
= {
1061 .probe
= temac_of_probe
,
1062 .remove
= __devexit_p(temac_of_remove
),
1064 .owner
= THIS_MODULE
,
1065 .name
= "xilinx_temac",
1066 .of_match_table
= temac_of_match
,
1070 static int __init
temac_init(void)
1072 return of_register_platform_driver(&temac_of_driver
);
1074 module_init(temac_init
);
1076 static void __exit
temac_exit(void)
1078 of_unregister_platform_driver(&temac_of_driver
);
1080 module_exit(temac_exit
);
1082 MODULE_DESCRIPTION("Xilinx LL_TEMAC Ethernet driver");
1083 MODULE_AUTHOR("Yoshio Kashiwagi");
1084 MODULE_LICENSE("GPL");