1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/uaccess.h>
17#include <linux/netdevice.h>
18#include <linux/etherdevice.h>
19#include <linux/skbuff.h>
20#include <linux/io.h>
21#include <linux/slab.h>
22#include <linux/of_address.h>
23#include <linux/of_device.h>
24#include <linux/of_platform.h>
25#include <linux/of_mdio.h>
26#include <linux/of_net.h>
27#include <linux/phy.h>
28#include <linux/interrupt.h>
29
30#define DRIVER_NAME "xilinx_emaclite"
31
32
33#define XEL_TXBUFF_OFFSET 0x0
34#define XEL_MDIOADDR_OFFSET 0x07E4
35#define XEL_MDIOWR_OFFSET 0x07E8
36#define XEL_MDIORD_OFFSET 0x07EC
37#define XEL_MDIOCTRL_OFFSET 0x07F0
38#define XEL_GIER_OFFSET 0x07F8
39#define XEL_TSR_OFFSET 0x07FC
40#define XEL_TPLR_OFFSET 0x07F4
41
42#define XEL_RXBUFF_OFFSET 0x1000
43#define XEL_RPLR_OFFSET 0x100C
44#define XEL_RSR_OFFSET 0x17FC
45
46#define XEL_BUFFER_OFFSET 0x0800
47
48
49#define XEL_MDIOADDR_REGADR_MASK 0x0000001F
50#define XEL_MDIOADDR_PHYADR_MASK 0x000003E0
51#define XEL_MDIOADDR_PHYADR_SHIFT 5
52#define XEL_MDIOADDR_OP_MASK 0x00000400
53
54
55#define XEL_MDIOWR_WRDATA_MASK 0x0000FFFF
56
57
58#define XEL_MDIORD_RDDATA_MASK 0x0000FFFF
59
60
61#define XEL_MDIOCTRL_MDIOSTS_MASK 0x00000001
62#define XEL_MDIOCTRL_MDIOEN_MASK 0x00000008
63
64
65#define XEL_GIER_GIE_MASK 0x80000000
66
67
68#define XEL_TSR_XMIT_BUSY_MASK 0x00000001
69#define XEL_TSR_PROGRAM_MASK 0x00000002
70#define XEL_TSR_XMIT_IE_MASK 0x00000008
71#define XEL_TSR_XMIT_ACTIVE_MASK 0x80000000
72
73
74
75
76#define XEL_TSR_PROG_MAC_ADDR (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_PROGRAM_MASK)
77
78
79#define XEL_RSR_RECV_DONE_MASK 0x00000001
80#define XEL_RSR_RECV_IE_MASK 0x00000008
81
82
83#define XEL_TPLR_LENGTH_MASK 0x0000FFFF
84
85
86#define XEL_RPLR_LENGTH_MASK 0x0000FFFF
87
88#define XEL_HEADER_OFFSET 12
89#define XEL_HEADER_SHIFT 16
90
91
92#define XEL_ARP_PACKET_SIZE 28
93#define XEL_HEADER_IP_LENGTH_OFFSET 16
94
95
96
97#define TX_TIMEOUT (60*HZ)
98#define ALIGNMENT 4
99
100
101#define BUFFER_ALIGN(adr) ((ALIGNMENT - ((u32) adr)) % ALIGNMENT)
102
103#ifdef __BIG_ENDIAN
104#define xemaclite_readl ioread32be
105#define xemaclite_writel iowrite32be
106#else
107#define xemaclite_readl ioread32
108#define xemaclite_writel iowrite32
109#endif
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127struct net_local {
128
129 struct net_device *ndev;
130
131 bool tx_ping_pong;
132 bool rx_ping_pong;
133 u32 next_tx_buf_to_use;
134 u32 next_rx_buf_to_use;
135 void __iomem *base_addr;
136
137 spinlock_t reset_lock;
138 struct sk_buff *deferred_skb;
139
140 struct phy_device *phy_dev;
141 struct device_node *phy_node;
142
143 struct mii_bus *mii_bus;
144
145 int last_link;
146};
147
148
149
150
151
152
153
154
155
156
157
158
159
160static void xemaclite_enable_interrupts(struct net_local *drvdata)
161{
162 u32 reg_data;
163
164
165 reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
166 xemaclite_writel(reg_data | XEL_TSR_XMIT_IE_MASK,
167 drvdata->base_addr + XEL_TSR_OFFSET);
168
169
170 xemaclite_writel(XEL_RSR_RECV_IE_MASK, drvdata->base_addr + XEL_RSR_OFFSET);
171
172
173 xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
174}
175
176
177
178
179
180
181
182
183static void xemaclite_disable_interrupts(struct net_local *drvdata)
184{
185 u32 reg_data;
186
187
188 xemaclite_writel(XEL_GIER_GIE_MASK, drvdata->base_addr + XEL_GIER_OFFSET);
189
190
191 reg_data = xemaclite_readl(drvdata->base_addr + XEL_TSR_OFFSET);
192 xemaclite_writel(reg_data & (~XEL_TSR_XMIT_IE_MASK),
193 drvdata->base_addr + XEL_TSR_OFFSET);
194
195
196 reg_data = xemaclite_readl(drvdata->base_addr + XEL_RSR_OFFSET);
197 xemaclite_writel(reg_data & (~XEL_RSR_RECV_IE_MASK),
198 drvdata->base_addr + XEL_RSR_OFFSET);
199}
200
201
202
203
204
205
206
207
208
209
210static void xemaclite_aligned_write(void *src_ptr, u32 *dest_ptr,
211 unsigned length)
212{
213 u32 align_buffer;
214 u32 *to_u32_ptr;
215 u16 *from_u16_ptr, *to_u16_ptr;
216
217 to_u32_ptr = dest_ptr;
218 from_u16_ptr = src_ptr;
219 align_buffer = 0;
220
221 for (; length > 3; length -= 4) {
222 to_u16_ptr = (u16 *)&align_buffer;
223 *to_u16_ptr++ = *from_u16_ptr++;
224 *to_u16_ptr++ = *from_u16_ptr++;
225
226
227
228
229
230
231 wmb();
232
233
234 *to_u32_ptr++ = align_buffer;
235 }
236 if (length) {
237 u8 *from_u8_ptr, *to_u8_ptr;
238
239
240 align_buffer = 0;
241 to_u8_ptr = (u8 *) &align_buffer;
242 from_u8_ptr = (u8 *) from_u16_ptr;
243
244
245 for (; length > 0; length--)
246 *to_u8_ptr++ = *from_u8_ptr++;
247
248
249
250
251
252
253 wmb();
254 *to_u32_ptr = align_buffer;
255 }
256}
257
258
259
260
261
262
263
264
265
266
267static void xemaclite_aligned_read(u32 *src_ptr, u8 *dest_ptr,
268 unsigned length)
269{
270 u16 *to_u16_ptr, *from_u16_ptr;
271 u32 *from_u32_ptr;
272 u32 align_buffer;
273
274 from_u32_ptr = src_ptr;
275 to_u16_ptr = (u16 *) dest_ptr;
276
277 for (; length > 3; length -= 4) {
278
279 align_buffer = *from_u32_ptr++;
280 from_u16_ptr = (u16 *)&align_buffer;
281
282
283 *to_u16_ptr++ = *from_u16_ptr++;
284 *to_u16_ptr++ = *from_u16_ptr++;
285 }
286
287 if (length) {
288 u8 *to_u8_ptr, *from_u8_ptr;
289
290
291 to_u8_ptr = (u8 *) to_u16_ptr;
292 align_buffer = *from_u32_ptr++;
293 from_u8_ptr = (u8 *) &align_buffer;
294
295
296 for (; length > 0; length--)
297 *to_u8_ptr = *from_u8_ptr;
298 }
299}
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316static int xemaclite_send_data(struct net_local *drvdata, u8 *data,
317 unsigned int byte_count)
318{
319 u32 reg_data;
320 void __iomem *addr;
321
322
323 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
324
325
326 if (byte_count > ETH_FRAME_LEN)
327 byte_count = ETH_FRAME_LEN;
328
329
330 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
331 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
332 XEL_TSR_XMIT_ACTIVE_MASK)) == 0) {
333
334
335 if (drvdata->tx_ping_pong != 0)
336 drvdata->next_tx_buf_to_use ^= XEL_BUFFER_OFFSET;
337 } else if (drvdata->tx_ping_pong != 0) {
338
339
340
341 addr = (void __iomem __force *)((u32 __force)addr ^
342 XEL_BUFFER_OFFSET);
343 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
344
345 if ((reg_data & (XEL_TSR_XMIT_BUSY_MASK |
346 XEL_TSR_XMIT_ACTIVE_MASK)) != 0)
347 return -1;
348 } else
349 return -1;
350
351
352 xemaclite_aligned_write(data, (u32 __force *) addr, byte_count);
353
354 xemaclite_writel((byte_count & XEL_TPLR_LENGTH_MASK),
355 addr + XEL_TPLR_OFFSET);
356
357
358
359
360
361 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
362 reg_data |= (XEL_TSR_XMIT_BUSY_MASK | XEL_TSR_XMIT_ACTIVE_MASK);
363 xemaclite_writel(reg_data, addr + XEL_TSR_OFFSET);
364
365 return 0;
366}
367
368
369
370
371
372
373
374
375
376
377
378static u16 xemaclite_recv_data(struct net_local *drvdata, u8 *data, int maxlen)
379{
380 void __iomem *addr;
381 u16 length, proto_type;
382 u32 reg_data;
383
384
385 addr = (drvdata->base_addr + drvdata->next_rx_buf_to_use);
386
387
388 reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
389
390 if ((reg_data & XEL_RSR_RECV_DONE_MASK) == XEL_RSR_RECV_DONE_MASK) {
391 if (drvdata->rx_ping_pong != 0)
392 drvdata->next_rx_buf_to_use ^= XEL_BUFFER_OFFSET;
393 } else {
394
395
396
397
398 if (drvdata->rx_ping_pong != 0)
399 addr = (void __iomem __force *)((u32 __force)addr ^
400 XEL_BUFFER_OFFSET);
401 else
402 return 0;
403
404
405 reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
406 if ((reg_data & XEL_RSR_RECV_DONE_MASK) !=
407 XEL_RSR_RECV_DONE_MASK)
408 return 0;
409 }
410
411
412 proto_type = ((ntohl(xemaclite_readl(addr + XEL_HEADER_OFFSET +
413 XEL_RXBUFF_OFFSET)) >> XEL_HEADER_SHIFT) &
414 XEL_RPLR_LENGTH_MASK);
415
416
417
418 if (proto_type > ETH_DATA_LEN) {
419
420 if (proto_type == ETH_P_IP) {
421 length = ((ntohl(xemaclite_readl(addr +
422 XEL_HEADER_IP_LENGTH_OFFSET +
423 XEL_RXBUFF_OFFSET)) >>
424 XEL_HEADER_SHIFT) &
425 XEL_RPLR_LENGTH_MASK);
426 length = min_t(u16, length, ETH_DATA_LEN);
427 length += ETH_HLEN + ETH_FCS_LEN;
428
429 } else if (proto_type == ETH_P_ARP)
430 length = XEL_ARP_PACKET_SIZE + ETH_HLEN + ETH_FCS_LEN;
431 else
432
433
434 length = ETH_FRAME_LEN + ETH_FCS_LEN;
435 } else
436
437 length = proto_type + ETH_HLEN + ETH_FCS_LEN;
438
439 if (WARN_ON(length > maxlen))
440 length = maxlen;
441
442
443 xemaclite_aligned_read((u32 __force *) (addr + XEL_RXBUFF_OFFSET),
444 data, length);
445
446
447 reg_data = xemaclite_readl(addr + XEL_RSR_OFFSET);
448 reg_data &= ~XEL_RSR_RECV_DONE_MASK;
449 xemaclite_writel(reg_data, addr + XEL_RSR_OFFSET);
450
451 return length;
452}
453
454
455
456
457
458
459
460
461
462
463
464
465static void xemaclite_update_address(struct net_local *drvdata,
466 u8 *address_ptr)
467{
468 void __iomem *addr;
469 u32 reg_data;
470
471
472 addr = drvdata->base_addr + drvdata->next_tx_buf_to_use;
473
474 xemaclite_aligned_write(address_ptr, (u32 __force *) addr, ETH_ALEN);
475
476 xemaclite_writel(ETH_ALEN, addr + XEL_TPLR_OFFSET);
477
478
479 reg_data = xemaclite_readl(addr + XEL_TSR_OFFSET);
480 xemaclite_writel(reg_data | XEL_TSR_PROG_MAC_ADDR, addr + XEL_TSR_OFFSET);
481
482
483 while ((xemaclite_readl(addr + XEL_TSR_OFFSET) &
484 XEL_TSR_PROG_MAC_ADDR) != 0)
485 ;
486}
487
488
489
490
491
492
493
494
495
496
497
498
499static int xemaclite_set_mac_address(struct net_device *dev, void *address)
500{
501 struct net_local *lp = netdev_priv(dev);
502 struct sockaddr *addr = address;
503
504 if (netif_running(dev))
505 return -EBUSY;
506
507 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
508 xemaclite_update_address(lp, dev->dev_addr);
509 return 0;
510}
511
512
513
514
515
516
517
518static void xemaclite_tx_timeout(struct net_device *dev, unsigned int txqueue)
519{
520 struct net_local *lp = netdev_priv(dev);
521 unsigned long flags;
522
523 dev_err(&lp->ndev->dev, "Exceeded transmit timeout of %lu ms\n",
524 TX_TIMEOUT * 1000UL / HZ);
525
526 dev->stats.tx_errors++;
527
528
529 spin_lock_irqsave(&lp->reset_lock, flags);
530
531
532 netif_stop_queue(dev);
533
534 xemaclite_disable_interrupts(lp);
535 xemaclite_enable_interrupts(lp);
536
537 if (lp->deferred_skb) {
538 dev_kfree_skb(lp->deferred_skb);
539 lp->deferred_skb = NULL;
540 dev->stats.tx_errors++;
541 }
542
543
544 netif_trans_update(dev);
545
546
547 netif_wake_queue(dev);
548 spin_unlock_irqrestore(&lp->reset_lock, flags);
549}
550
551
552
553
554
555
556
557
558
559
560
561
562static void xemaclite_tx_handler(struct net_device *dev)
563{
564 struct net_local *lp = netdev_priv(dev);
565
566 dev->stats.tx_packets++;
567 if (lp->deferred_skb) {
568 if (xemaclite_send_data(lp,
569 (u8 *) lp->deferred_skb->data,
570 lp->deferred_skb->len) != 0)
571 return;
572 else {
573 dev->stats.tx_bytes += lp->deferred_skb->len;
574 dev_kfree_skb_irq(lp->deferred_skb);
575 lp->deferred_skb = NULL;
576 netif_trans_update(dev);
577 netif_wake_queue(dev);
578 }
579 }
580}
581
582
583
584
585
586
587
588
589static void xemaclite_rx_handler(struct net_device *dev)
590{
591 struct net_local *lp = netdev_priv(dev);
592 struct sk_buff *skb;
593 unsigned int align;
594 u32 len;
595
596 len = ETH_FRAME_LEN + ETH_FCS_LEN;
597 skb = netdev_alloc_skb(dev, len + ALIGNMENT);
598 if (!skb) {
599
600 dev->stats.rx_dropped++;
601 dev_err(&lp->ndev->dev, "Could not allocate receive buffer\n");
602 return;
603 }
604
605
606
607
608
609
610 align = BUFFER_ALIGN(skb->data);
611 if (align)
612 skb_reserve(skb, align);
613
614 skb_reserve(skb, 2);
615
616 len = xemaclite_recv_data(lp, (u8 *) skb->data, len);
617
618 if (!len) {
619 dev->stats.rx_errors++;
620 dev_kfree_skb_irq(skb);
621 return;
622 }
623
624 skb_put(skb, len);
625
626 skb->protocol = eth_type_trans(skb, dev);
627 skb_checksum_none_assert(skb);
628
629 dev->stats.rx_packets++;
630 dev->stats.rx_bytes += len;
631
632 if (!skb_defer_rx_timestamp(skb))
633 netif_rx(skb);
634}
635
636
637
638
639
640
641
642
643
644static irqreturn_t xemaclite_interrupt(int irq, void *dev_id)
645{
646 bool tx_complete = false;
647 struct net_device *dev = dev_id;
648 struct net_local *lp = netdev_priv(dev);
649 void __iomem *base_addr = lp->base_addr;
650 u32 tx_status;
651
652
653 if ((xemaclite_readl(base_addr + XEL_RSR_OFFSET) &
654 XEL_RSR_RECV_DONE_MASK) ||
655 (xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_RSR_OFFSET)
656 & XEL_RSR_RECV_DONE_MASK))
657
658 xemaclite_rx_handler(dev);
659
660
661 tx_status = xemaclite_readl(base_addr + XEL_TSR_OFFSET);
662 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
663 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
664
665 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
666 xemaclite_writel(tx_status, base_addr + XEL_TSR_OFFSET);
667
668 tx_complete = true;
669 }
670
671
672 tx_status = xemaclite_readl(base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
673 if (((tx_status & XEL_TSR_XMIT_BUSY_MASK) == 0) &&
674 (tx_status & XEL_TSR_XMIT_ACTIVE_MASK) != 0) {
675
676 tx_status &= ~XEL_TSR_XMIT_ACTIVE_MASK;
677 xemaclite_writel(tx_status, base_addr + XEL_BUFFER_OFFSET +
678 XEL_TSR_OFFSET);
679
680 tx_complete = true;
681 }
682
683
684 if (tx_complete != 0)
685 xemaclite_tx_handler(dev);
686
687 return IRQ_HANDLED;
688}
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704static int xemaclite_mdio_wait(struct net_local *lp)
705{
706 unsigned long end = jiffies + 2;
707
708
709
710
711 while (xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET) &
712 XEL_MDIOCTRL_MDIOSTS_MASK) {
713 if (time_before_eq(end, jiffies)) {
714 WARN_ON(1);
715 return -ETIMEDOUT;
716 }
717 msleep(1);
718 }
719 return 0;
720}
721
722
723
724
725
726
727
728
729
730
731
732
733
734static int xemaclite_mdio_read(struct mii_bus *bus, int phy_id, int reg)
735{
736 struct net_local *lp = bus->priv;
737 u32 ctrl_reg;
738 u32 rc;
739
740 if (xemaclite_mdio_wait(lp))
741 return -ETIMEDOUT;
742
743
744
745
746
747 ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
748 xemaclite_writel(XEL_MDIOADDR_OP_MASK |
749 ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
750 lp->base_addr + XEL_MDIOADDR_OFFSET);
751 xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
752 lp->base_addr + XEL_MDIOCTRL_OFFSET);
753
754 if (xemaclite_mdio_wait(lp))
755 return -ETIMEDOUT;
756
757 rc = xemaclite_readl(lp->base_addr + XEL_MDIORD_OFFSET);
758
759 dev_dbg(&lp->ndev->dev,
760 "xemaclite_mdio_read(phy_id=%i, reg=%x) == %x\n",
761 phy_id, reg, rc);
762
763 return rc;
764}
765
766
767
768
769
770
771
772
773
774
775
776static int xemaclite_mdio_write(struct mii_bus *bus, int phy_id, int reg,
777 u16 val)
778{
779 struct net_local *lp = bus->priv;
780 u32 ctrl_reg;
781
782 dev_dbg(&lp->ndev->dev,
783 "xemaclite_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
784 phy_id, reg, val);
785
786 if (xemaclite_mdio_wait(lp))
787 return -ETIMEDOUT;
788
789
790
791
792
793
794 ctrl_reg = xemaclite_readl(lp->base_addr + XEL_MDIOCTRL_OFFSET);
795 xemaclite_writel(~XEL_MDIOADDR_OP_MASK &
796 ((phy_id << XEL_MDIOADDR_PHYADR_SHIFT) | reg),
797 lp->base_addr + XEL_MDIOADDR_OFFSET);
798 xemaclite_writel(val, lp->base_addr + XEL_MDIOWR_OFFSET);
799 xemaclite_writel(ctrl_reg | XEL_MDIOCTRL_MDIOSTS_MASK,
800 lp->base_addr + XEL_MDIOCTRL_OFFSET);
801
802 return 0;
803}
804
805
806
807
808
809
810
811
812
813
814
815static int xemaclite_mdio_setup(struct net_local *lp, struct device *dev)
816{
817 struct mii_bus *bus;
818 int rc;
819 struct resource res;
820 struct device_node *np = of_get_parent(lp->phy_node);
821 struct device_node *npp;
822
823
824
825
826 if (!np) {
827 dev_err(dev, "Failed to register mdio bus.\n");
828 return -ENODEV;
829 }
830 npp = of_get_parent(np);
831
832 of_address_to_resource(npp, 0, &res);
833 if (lp->ndev->mem_start != res.start) {
834 struct phy_device *phydev;
835 phydev = of_phy_find_device(lp->phy_node);
836 if (!phydev)
837 dev_info(dev,
838 "MDIO of the phy is not registered yet\n");
839 else
840 put_device(&phydev->mdio.dev);
841 return 0;
842 }
843
844
845
846
847 xemaclite_writel(XEL_MDIOCTRL_MDIOEN_MASK,
848 lp->base_addr + XEL_MDIOCTRL_OFFSET);
849
850 bus = mdiobus_alloc();
851 if (!bus) {
852 dev_err(dev, "Failed to allocate mdiobus\n");
853 return -ENOMEM;
854 }
855
856 snprintf(bus->id, MII_BUS_ID_SIZE, "%.8llx",
857 (unsigned long long)res.start);
858 bus->priv = lp;
859 bus->name = "Xilinx Emaclite MDIO";
860 bus->read = xemaclite_mdio_read;
861 bus->write = xemaclite_mdio_write;
862 bus->parent = dev;
863
864 rc = of_mdiobus_register(bus, np);
865 if (rc) {
866 dev_err(dev, "Failed to register mdio bus.\n");
867 goto err_register;
868 }
869
870 lp->mii_bus = bus;
871
872 return 0;
873
874err_register:
875 mdiobus_free(bus);
876 return rc;
877}
878
879
880
881
882
883
884
885
886static void xemaclite_adjust_link(struct net_device *ndev)
887{
888 struct net_local *lp = netdev_priv(ndev);
889 struct phy_device *phy = lp->phy_dev;
890 int link_state;
891
892
893 link_state = phy->speed | (phy->duplex << 1) | phy->link;
894
895 if (lp->last_link != link_state) {
896 lp->last_link = link_state;
897 phy_print_status(phy);
898 }
899}
900
901
902
903
904
905
906
907
908
909static int xemaclite_open(struct net_device *dev)
910{
911 struct net_local *lp = netdev_priv(dev);
912 int retval;
913
914
915 xemaclite_disable_interrupts(lp);
916
917 if (lp->phy_node) {
918 u32 bmcr;
919
920 lp->phy_dev = of_phy_connect(lp->ndev, lp->phy_node,
921 xemaclite_adjust_link, 0,
922 PHY_INTERFACE_MODE_MII);
923 if (!lp->phy_dev) {
924 dev_err(&lp->ndev->dev, "of_phy_connect() failed\n");
925 return -ENODEV;
926 }
927
928
929 phy_set_max_speed(lp->phy_dev, SPEED_100);
930
931
932 phy_write(lp->phy_dev, MII_CTRL1000, 0);
933
934
935 phy_write(lp->phy_dev, MII_ADVERTISE, ADVERTISE_ALL |
936 ADVERTISE_CSMA);
937
938
939 bmcr = phy_read(lp->phy_dev, MII_BMCR);
940 bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
941 phy_write(lp->phy_dev, MII_BMCR, bmcr);
942
943 phy_start(lp->phy_dev);
944 }
945
946
947 xemaclite_update_address(lp, dev->dev_addr);
948
949
950 retval = request_irq(dev->irq, xemaclite_interrupt, 0, dev->name, dev);
951 if (retval) {
952 dev_err(&lp->ndev->dev, "Could not allocate interrupt %d\n",
953 dev->irq);
954 if (lp->phy_dev)
955 phy_disconnect(lp->phy_dev);
956 lp->phy_dev = NULL;
957
958 return retval;
959 }
960
961
962 xemaclite_enable_interrupts(lp);
963
964
965 netif_start_queue(dev);
966
967 return 0;
968}
969
970
971
972
973
974
975
976
977
978static int xemaclite_close(struct net_device *dev)
979{
980 struct net_local *lp = netdev_priv(dev);
981
982 netif_stop_queue(dev);
983 xemaclite_disable_interrupts(lp);
984 free_irq(dev->irq, dev);
985
986 if (lp->phy_dev)
987 phy_disconnect(lp->phy_dev);
988 lp->phy_dev = NULL;
989
990 return 0;
991}
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007static int xemaclite_send(struct sk_buff *orig_skb, struct net_device *dev)
1008{
1009 struct net_local *lp = netdev_priv(dev);
1010 struct sk_buff *new_skb;
1011 unsigned int len;
1012 unsigned long flags;
1013
1014 len = orig_skb->len;
1015
1016 new_skb = orig_skb;
1017
1018 spin_lock_irqsave(&lp->reset_lock, flags);
1019 if (xemaclite_send_data(lp, (u8 *) new_skb->data, len) != 0) {
1020
1021
1022
1023 netif_stop_queue(dev);
1024 lp->deferred_skb = new_skb;
1025
1026 skb_tx_timestamp(new_skb);
1027 spin_unlock_irqrestore(&lp->reset_lock, flags);
1028 return 0;
1029 }
1030 spin_unlock_irqrestore(&lp->reset_lock, flags);
1031
1032 skb_tx_timestamp(new_skb);
1033
1034 dev->stats.tx_bytes += len;
1035 dev_consume_skb_any(new_skb);
1036
1037 return 0;
1038}
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050static bool get_bool(struct platform_device *ofdev, const char *s)
1051{
1052 u32 *p = (u32 *)of_get_property(ofdev->dev.of_node, s, NULL);
1053
1054 if (p) {
1055 return (bool)*p;
1056 } else {
1057 dev_warn(&ofdev->dev, "Parameter %s not found,"
1058 "defaulting to false\n", s);
1059 return false;
1060 }
1061}
1062
1063static const struct net_device_ops xemaclite_netdev_ops;
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079static int xemaclite_of_probe(struct platform_device *ofdev)
1080{
1081 struct resource *res;
1082 struct net_device *ndev = NULL;
1083 struct net_local *lp = NULL;
1084 struct device *dev = &ofdev->dev;
1085 const void *mac_address;
1086
1087 int rc = 0;
1088
1089 dev_info(dev, "Device Tree Probing\n");
1090
1091
1092 ndev = alloc_etherdev(sizeof(struct net_local));
1093 if (!ndev)
1094 return -ENOMEM;
1095
1096 dev_set_drvdata(dev, ndev);
1097 SET_NETDEV_DEV(ndev, &ofdev->dev);
1098
1099 lp = netdev_priv(ndev);
1100 lp->ndev = ndev;
1101
1102
1103 res = platform_get_resource(ofdev, IORESOURCE_IRQ, 0);
1104 if (!res) {
1105 dev_err(dev, "no IRQ found\n");
1106 rc = -ENXIO;
1107 goto error;
1108 }
1109
1110 ndev->irq = res->start;
1111
1112 res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
1113 lp->base_addr = devm_ioremap_resource(&ofdev->dev, res);
1114 if (IS_ERR(lp->base_addr)) {
1115 rc = PTR_ERR(lp->base_addr);
1116 goto error;
1117 }
1118
1119 ndev->mem_start = res->start;
1120 ndev->mem_end = res->end;
1121
1122 spin_lock_init(&lp->reset_lock);
1123 lp->next_tx_buf_to_use = 0x0;
1124 lp->next_rx_buf_to_use = 0x0;
1125 lp->tx_ping_pong = get_bool(ofdev, "xlnx,tx-ping-pong");
1126 lp->rx_ping_pong = get_bool(ofdev, "xlnx,rx-ping-pong");
1127 mac_address = of_get_mac_address(ofdev->dev.of_node);
1128
1129 if (mac_address) {
1130
1131 memcpy(ndev->dev_addr, mac_address, ETH_ALEN);
1132 } else {
1133 dev_warn(dev, "No MAC address found, using random\n");
1134 eth_hw_addr_random(ndev);
1135 }
1136
1137
1138 xemaclite_writel(0, lp->base_addr + XEL_TSR_OFFSET);
1139 xemaclite_writel(0, lp->base_addr + XEL_BUFFER_OFFSET + XEL_TSR_OFFSET);
1140
1141
1142 xemaclite_update_address(lp, ndev->dev_addr);
1143
1144 lp->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
1145 xemaclite_mdio_setup(lp, &ofdev->dev);
1146
1147 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
1148
1149 ndev->netdev_ops = &xemaclite_netdev_ops;
1150 ndev->flags &= ~IFF_MULTICAST;
1151 ndev->watchdog_timeo = TX_TIMEOUT;
1152
1153
1154 rc = register_netdev(ndev);
1155 if (rc) {
1156 dev_err(dev,
1157 "Cannot register network device, aborting\n");
1158 goto error;
1159 }
1160
1161 dev_info(dev,
1162 "Xilinx EmacLite at 0x%08X mapped to 0x%08X, irq=%d\n",
1163 (unsigned int __force)ndev->mem_start,
1164 (unsigned int __force)lp->base_addr, ndev->irq);
1165 return 0;
1166
1167error:
1168 free_netdev(ndev);
1169 return rc;
1170}
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182static int xemaclite_of_remove(struct platform_device *of_dev)
1183{
1184 struct net_device *ndev = platform_get_drvdata(of_dev);
1185
1186 struct net_local *lp = netdev_priv(ndev);
1187
1188
1189 if (lp->mii_bus) {
1190 mdiobus_unregister(lp->mii_bus);
1191 mdiobus_free(lp->mii_bus);
1192 lp->mii_bus = NULL;
1193 }
1194
1195 unregister_netdev(ndev);
1196
1197 of_node_put(lp->phy_node);
1198 lp->phy_node = NULL;
1199
1200 free_netdev(ndev);
1201
1202 return 0;
1203}
1204
1205#ifdef CONFIG_NET_POLL_CONTROLLER
1206static void
1207xemaclite_poll_controller(struct net_device *ndev)
1208{
1209 disable_irq(ndev->irq);
1210 xemaclite_interrupt(ndev->irq, ndev);
1211 enable_irq(ndev->irq);
1212}
1213#endif
1214
1215static const struct net_device_ops xemaclite_netdev_ops = {
1216 .ndo_open = xemaclite_open,
1217 .ndo_stop = xemaclite_close,
1218 .ndo_start_xmit = xemaclite_send,
1219 .ndo_set_mac_address = xemaclite_set_mac_address,
1220 .ndo_tx_timeout = xemaclite_tx_timeout,
1221#ifdef CONFIG_NET_POLL_CONTROLLER
1222 .ndo_poll_controller = xemaclite_poll_controller,
1223#endif
1224};
1225
1226
1227static const struct of_device_id xemaclite_of_match[] = {
1228 { .compatible = "xlnx,opb-ethernetlite-1.01.a", },
1229 { .compatible = "xlnx,opb-ethernetlite-1.01.b", },
1230 { .compatible = "xlnx,xps-ethernetlite-1.00.a", },
1231 { .compatible = "xlnx,xps-ethernetlite-2.00.a", },
1232 { .compatible = "xlnx,xps-ethernetlite-2.01.a", },
1233 { .compatible = "xlnx,xps-ethernetlite-3.00.a", },
1234 { },
1235};
1236MODULE_DEVICE_TABLE(of, xemaclite_of_match);
1237
1238static struct platform_driver xemaclite_of_driver = {
1239 .driver = {
1240 .name = DRIVER_NAME,
1241 .of_match_table = xemaclite_of_match,
1242 },
1243 .probe = xemaclite_of_probe,
1244 .remove = xemaclite_of_remove,
1245};
1246
1247module_platform_driver(xemaclite_of_driver);
1248
1249MODULE_AUTHOR("Xilinx, Inc.");
1250MODULE_DESCRIPTION("Xilinx Ethernet MAC Lite driver");
1251MODULE_LICENSE("GPL");
1252