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