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