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