1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/mii.h>
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/skbuff.h>
26#include <linux/dma-mapping.h>
27#include <linux/ethtool.h>
28#include <linux/platform_device.h>
29#include <linux/clk.h>
30#include <linux/gfp.h>
31
32#include <asm/io.h>
33#include <asm/uaccess.h>
34#include <asm/mach-types.h>
35
36#include <mach/at91rm9200_emac.h>
37#include <mach/gpio.h>
38#include <mach/board.h>
39
40#include "at91_ether.h"
41
42#define DRV_NAME "at91_ether"
43#define DRV_VERSION "1.0"
44
45#define LINK_POLL_INTERVAL (HZ)
46
47
48
49
50
51
52static inline unsigned long at91_emac_read(unsigned int reg)
53{
54 void __iomem *emac_base = (void __iomem *)AT91_VA_BASE_EMAC;
55
56 return __raw_readl(emac_base + reg);
57}
58
59
60
61
62static inline void at91_emac_write(unsigned int reg, unsigned long value)
63{
64 void __iomem *emac_base = (void __iomem *)AT91_VA_BASE_EMAC;
65
66 __raw_writel(value, emac_base + reg);
67}
68
69
70
71
72
73
74
75
76static void enable_mdi(void)
77{
78 unsigned long ctl;
79
80 ctl = at91_emac_read(AT91_EMAC_CTL);
81 at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_MPE);
82}
83
84
85
86
87static void disable_mdi(void)
88{
89 unsigned long ctl;
90
91 ctl = at91_emac_read(AT91_EMAC_CTL);
92 at91_emac_write(AT91_EMAC_CTL, ctl & ~AT91_EMAC_MPE);
93}
94
95
96
97
98static inline void at91_phy_wait(void) {
99 unsigned long timeout = jiffies + 2;
100
101 while (!(at91_emac_read(AT91_EMAC_SR) & AT91_EMAC_SR_IDLE)) {
102 if (time_after(jiffies, timeout)) {
103 printk("at91_ether: MIO timeout\n");
104 break;
105 }
106 cpu_relax();
107 }
108}
109
110
111
112
113
114static void write_phy(unsigned char phy_addr, unsigned char address, unsigned int value)
115{
116 at91_emac_write(AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_W
117 | ((phy_addr & 0x1f) << 23) | (address << 18) | (value & AT91_EMAC_DATA));
118
119
120 at91_phy_wait();
121}
122
123
124
125
126
127static void read_phy(unsigned char phy_addr, unsigned char address, unsigned int *value)
128{
129 at91_emac_write(AT91_EMAC_MAN, AT91_EMAC_MAN_802_3 | AT91_EMAC_RW_R
130 | ((phy_addr & 0x1f) << 23) | (address << 18));
131
132
133 at91_phy_wait();
134
135 *value = at91_emac_read(AT91_EMAC_MAN) & AT91_EMAC_DATA;
136}
137
138
139
140
141
142
143
144
145static void update_linkspeed(struct net_device *dev, int silent)
146{
147 struct at91_private *lp = netdev_priv(dev);
148 unsigned int bmsr, bmcr, lpa, mac_cfg;
149 unsigned int speed, duplex;
150
151 if (!mii_link_ok(&lp->mii)) {
152 netif_carrier_off(dev);
153 if (!silent)
154 printk(KERN_INFO "%s: Link down.\n", dev->name);
155 return;
156 }
157
158
159 read_phy(lp->phy_address, MII_BMSR, &bmsr);
160 read_phy(lp->phy_address, MII_BMCR, &bmcr);
161 if (bmcr & BMCR_ANENABLE) {
162 if (!(bmsr & BMSR_ANEGCOMPLETE))
163 return;
164
165 read_phy(lp->phy_address, MII_LPA, &lpa);
166 if ((lpa & LPA_100FULL) || (lpa & LPA_100HALF)) speed = SPEED_100;
167 else speed = SPEED_10;
168 if ((lpa & LPA_100FULL) || (lpa & LPA_10FULL)) duplex = DUPLEX_FULL;
169 else duplex = DUPLEX_HALF;
170 } else {
171 speed = (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;
172 duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
173 }
174
175
176 mac_cfg = at91_emac_read(AT91_EMAC_CFG) & ~(AT91_EMAC_SPD | AT91_EMAC_FD);
177 if (speed == SPEED_100) {
178 if (duplex == DUPLEX_FULL)
179 mac_cfg |= AT91_EMAC_SPD | AT91_EMAC_FD;
180 else
181 mac_cfg |= AT91_EMAC_SPD;
182 } else {
183 if (duplex == DUPLEX_FULL)
184 mac_cfg |= AT91_EMAC_FD;
185 else {}
186 }
187 at91_emac_write(AT91_EMAC_CFG, mac_cfg);
188
189 if (!silent)
190 printk(KERN_INFO "%s: Link now %i-%s\n", dev->name, speed, (duplex == DUPLEX_FULL) ? "FullDuplex" : "HalfDuplex");
191 netif_carrier_on(dev);
192}
193
194
195
196
197static irqreturn_t at91ether_phy_interrupt(int irq, void *dev_id)
198{
199 struct net_device *dev = (struct net_device *) dev_id;
200 struct at91_private *lp = netdev_priv(dev);
201 unsigned int phy;
202
203
204
205
206
207
208 enable_mdi();
209 if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
210 read_phy(lp->phy_address, MII_DSINTR_REG, &phy);
211 if (!(phy & (1 << 0)))
212 goto done;
213 }
214 else if (lp->phy_type == MII_LXT971A_ID) {
215 read_phy(lp->phy_address, MII_ISINTS_REG, &phy);
216 if (!(phy & (1 << 2)))
217 goto done;
218 }
219 else if (lp->phy_type == MII_BCM5221_ID) {
220 read_phy(lp->phy_address, MII_BCMINTR_REG, &phy);
221 if (!(phy & (1 << 0)))
222 goto done;
223 }
224 else if (lp->phy_type == MII_KS8721_ID) {
225 read_phy(lp->phy_address, MII_TPISTATUS, &phy);
226 if (!(phy & ((1 << 2) | 1)))
227 goto done;
228 }
229 else if (lp->phy_type == MII_T78Q21x3_ID) {
230 read_phy(lp->phy_address, MII_T78Q21INT_REG, &phy);
231 if (!(phy & ((1 << 2) | 1)))
232 goto done;
233 }
234 else if (lp->phy_type == MII_DP83848_ID) {
235 read_phy(lp->phy_address, MII_DPPHYSTS_REG, &phy);
236 if (!(phy & (1 << 7)))
237 goto done;
238 }
239
240 update_linkspeed(dev, 0);
241
242done:
243 disable_mdi();
244
245 return IRQ_HANDLED;
246}
247
248
249
250
251static void enable_phyirq(struct net_device *dev)
252{
253 struct at91_private *lp = netdev_priv(dev);
254 unsigned int dsintr, irq_number;
255 int status;
256
257 irq_number = lp->board_data.phy_irq_pin;
258 if (!irq_number) {
259
260
261
262
263 mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
264 return;
265 }
266
267 status = request_irq(irq_number, at91ether_phy_interrupt, 0, dev->name, dev);
268 if (status) {
269 printk(KERN_ERR "at91_ether: PHY IRQ %d request failed - status %d!\n", irq_number, status);
270 return;
271 }
272
273 spin_lock_irq(&lp->lock);
274 enable_mdi();
275
276 if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
277 read_phy(lp->phy_address, MII_DSINTR_REG, &dsintr);
278 dsintr = dsintr & ~0xf00;
279 write_phy(lp->phy_address, MII_DSINTR_REG, dsintr);
280 }
281 else if (lp->phy_type == MII_LXT971A_ID) {
282 read_phy(lp->phy_address, MII_ISINTE_REG, &dsintr);
283 dsintr = dsintr | 0xf2;
284 write_phy(lp->phy_address, MII_ISINTE_REG, dsintr);
285 }
286 else if (lp->phy_type == MII_BCM5221_ID) {
287 dsintr = (1 << 15) | ( 1 << 14);
288 write_phy(lp->phy_address, MII_BCMINTR_REG, dsintr);
289 }
290 else if (lp->phy_type == MII_KS8721_ID) {
291 dsintr = (1 << 10) | ( 1 << 8);
292 write_phy(lp->phy_address, MII_TPISTATUS, dsintr);
293 }
294 else if (lp->phy_type == MII_T78Q21x3_ID) {
295 read_phy(lp->phy_address, MII_T78Q21INT_REG, &dsintr);
296 dsintr = dsintr | 0x500;
297 write_phy(lp->phy_address, MII_T78Q21INT_REG, dsintr);
298 }
299 else if (lp->phy_type == MII_DP83848_ID) {
300 read_phy(lp->phy_address, MII_DPMISR_REG, &dsintr);
301 dsintr = dsintr | 0x3c;
302 write_phy(lp->phy_address, MII_DPMISR_REG, dsintr);
303 read_phy(lp->phy_address, MII_DPMICR_REG, &dsintr);
304 dsintr = dsintr | 0x3;
305 write_phy(lp->phy_address, MII_DPMICR_REG, dsintr);
306 }
307
308 disable_mdi();
309 spin_unlock_irq(&lp->lock);
310}
311
312
313
314
315static void disable_phyirq(struct net_device *dev)
316{
317 struct at91_private *lp = netdev_priv(dev);
318 unsigned int dsintr;
319 unsigned int irq_number;
320
321 irq_number = lp->board_data.phy_irq_pin;
322 if (!irq_number) {
323 del_timer_sync(&lp->check_timer);
324 return;
325 }
326
327 spin_lock_irq(&lp->lock);
328 enable_mdi();
329
330 if ((lp->phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
331 read_phy(lp->phy_address, MII_DSINTR_REG, &dsintr);
332 dsintr = dsintr | 0xf00;
333 write_phy(lp->phy_address, MII_DSINTR_REG, dsintr);
334 }
335 else if (lp->phy_type == MII_LXT971A_ID) {
336 read_phy(lp->phy_address, MII_ISINTE_REG, &dsintr);
337 dsintr = dsintr & ~0xf2;
338 write_phy(lp->phy_address, MII_ISINTE_REG, dsintr);
339 }
340 else if (lp->phy_type == MII_BCM5221_ID) {
341 read_phy(lp->phy_address, MII_BCMINTR_REG, &dsintr);
342 dsintr = ~(1 << 14);
343 write_phy(lp->phy_address, MII_BCMINTR_REG, dsintr);
344 }
345 else if (lp->phy_type == MII_KS8721_ID) {
346 read_phy(lp->phy_address, MII_TPISTATUS, &dsintr);
347 dsintr = ~((1 << 10) | (1 << 8));
348 write_phy(lp->phy_address, MII_TPISTATUS, dsintr);
349 }
350 else if (lp->phy_type == MII_T78Q21x3_ID) {
351 read_phy(lp->phy_address, MII_T78Q21INT_REG, &dsintr);
352 dsintr = dsintr & ~0x500;
353 write_phy(lp->phy_address, MII_T78Q21INT_REG, dsintr);
354 }
355 else if (lp->phy_type == MII_DP83848_ID) {
356 read_phy(lp->phy_address, MII_DPMICR_REG, &dsintr);
357 dsintr = dsintr & ~0x3;
358 write_phy(lp->phy_address, MII_DPMICR_REG, dsintr);
359 read_phy(lp->phy_address, MII_DPMISR_REG, &dsintr);
360 dsintr = dsintr & ~0x3c;
361 write_phy(lp->phy_address, MII_DPMISR_REG, dsintr);
362 }
363
364 disable_mdi();
365 spin_unlock_irq(&lp->lock);
366
367 free_irq(irq_number, dev);
368}
369
370
371
372
373#if 0
374static void reset_phy(struct net_device *dev)
375{
376 struct at91_private *lp = netdev_priv(dev);
377 unsigned int bmcr;
378
379 spin_lock_irq(&lp->lock);
380 enable_mdi();
381
382
383 write_phy(lp->phy_address, MII_BMCR, BMCR_RESET);
384
385
386 do {
387 read_phy(lp->phy_address, MII_BMCR, &bmcr);
388 } while (!(bmcr & BMCR_RESET));
389
390 disable_mdi();
391 spin_unlock_irq(&lp->lock);
392}
393#endif
394
395static void at91ether_check_link(unsigned long dev_id)
396{
397 struct net_device *dev = (struct net_device *) dev_id;
398 struct at91_private *lp = netdev_priv(dev);
399
400 enable_mdi();
401 update_linkspeed(dev, 1);
402 disable_mdi();
403
404 mod_timer(&lp->check_timer, jiffies + LINK_POLL_INTERVAL);
405}
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422static short __init unpack_mac_address(struct net_device *dev, unsigned int hi, unsigned int lo)
423{
424 char addr[6];
425
426 if (machine_is_csb337()) {
427 addr[5] = (lo & 0xff);
428 addr[4] = (lo & 0xff00) >> 8;
429 addr[3] = (lo & 0xff0000) >> 16;
430 addr[2] = (lo & 0xff000000) >> 24;
431 addr[1] = (hi & 0xff);
432 addr[0] = (hi & 0xff00) >> 8;
433 }
434 else {
435 addr[0] = (lo & 0xff);
436 addr[1] = (lo & 0xff00) >> 8;
437 addr[2] = (lo & 0xff0000) >> 16;
438 addr[3] = (lo & 0xff000000) >> 24;
439 addr[4] = (hi & 0xff);
440 addr[5] = (hi & 0xff00) >> 8;
441 }
442
443 if (is_valid_ether_addr(addr)) {
444 memcpy(dev->dev_addr, &addr, 6);
445 return 1;
446 }
447 return 0;
448}
449
450
451
452
453static void __init get_mac_address(struct net_device *dev)
454{
455
456 if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA1H), at91_emac_read(AT91_EMAC_SA1L)))
457 return;
458
459 if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA2H), at91_emac_read(AT91_EMAC_SA2L)))
460 return;
461
462 if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA3H), at91_emac_read(AT91_EMAC_SA3L)))
463 return;
464
465 if (unpack_mac_address(dev, at91_emac_read(AT91_EMAC_SA4H), at91_emac_read(AT91_EMAC_SA4L)))
466 return;
467
468 printk(KERN_ERR "at91_ether: Your bootloader did not configure a MAC address.\n");
469}
470
471
472
473
474static void update_mac_address(struct net_device *dev)
475{
476 at91_emac_write(AT91_EMAC_SA1L, (dev->dev_addr[3] << 24) | (dev->dev_addr[2] << 16) | (dev->dev_addr[1] << 8) | (dev->dev_addr[0]));
477 at91_emac_write(AT91_EMAC_SA1H, (dev->dev_addr[5] << 8) | (dev->dev_addr[4]));
478
479 at91_emac_write(AT91_EMAC_SA2L, 0);
480 at91_emac_write(AT91_EMAC_SA2H, 0);
481}
482
483
484
485
486static int set_mac_address(struct net_device *dev, void* addr)
487{
488 struct sockaddr *address = addr;
489
490 if (!is_valid_ether_addr(address->sa_data))
491 return -EADDRNOTAVAIL;
492
493 memcpy(dev->dev_addr, address->sa_data, dev->addr_len);
494 update_mac_address(dev);
495
496 printk("%s: Setting MAC address to %pM\n", dev->name,
497 dev->dev_addr);
498
499 return 0;
500}
501
502static int inline hash_bit_value(int bitnr, __u8 *addr)
503{
504 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
505 return 1;
506 return 0;
507}
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540static int hash_get_index(__u8 *addr)
541{
542 int i, j, bitval;
543 int hash_index = 0;
544
545 for (j = 0; j < 6; j++) {
546 for (i = 0, bitval = 0; i < 8; i++)
547 bitval ^= hash_bit_value(i*6 + j, addr);
548
549 hash_index |= (bitval << j);
550 }
551
552 return hash_index;
553}
554
555
556
557
558static void at91ether_sethashtable(struct net_device *dev)
559{
560 struct dev_mc_list *curr;
561 unsigned long mc_filter[2];
562 unsigned int bitnr;
563
564 mc_filter[0] = mc_filter[1] = 0;
565
566 netdev_for_each_mc_addr(curr, dev) {
567 bitnr = hash_get_index(curr->dmi_addr);
568 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
569 }
570
571 at91_emac_write(AT91_EMAC_HSL, mc_filter[0]);
572 at91_emac_write(AT91_EMAC_HSH, mc_filter[1]);
573}
574
575
576
577
578static void at91ether_set_multicast_list(struct net_device *dev)
579{
580 unsigned long cfg;
581
582 cfg = at91_emac_read(AT91_EMAC_CFG);
583
584 if (dev->flags & IFF_PROMISC)
585 cfg |= AT91_EMAC_CAF;
586 else if (dev->flags & (~IFF_PROMISC))
587 cfg &= ~AT91_EMAC_CAF;
588
589 if (dev->flags & IFF_ALLMULTI) {
590 at91_emac_write(AT91_EMAC_HSH, -1);
591 at91_emac_write(AT91_EMAC_HSL, -1);
592 cfg |= AT91_EMAC_MTI;
593 } else if (!netdev_mc_empty(dev)) {
594 at91ether_sethashtable(dev);
595 cfg |= AT91_EMAC_MTI;
596 } else if (dev->flags & (~IFF_ALLMULTI)) {
597 at91_emac_write(AT91_EMAC_HSH, 0);
598 at91_emac_write(AT91_EMAC_HSL, 0);
599 cfg &= ~AT91_EMAC_MTI;
600 }
601
602 at91_emac_write(AT91_EMAC_CFG, cfg);
603}
604
605
606
607static int mdio_read(struct net_device *dev, int phy_id, int location)
608{
609 unsigned int value;
610
611 read_phy(phy_id, location, &value);
612 return value;
613}
614
615static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
616{
617 write_phy(phy_id, location, value);
618}
619
620static int at91ether_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
621{
622 struct at91_private *lp = netdev_priv(dev);
623 int ret;
624
625 spin_lock_irq(&lp->lock);
626 enable_mdi();
627
628 ret = mii_ethtool_gset(&lp->mii, cmd);
629
630 disable_mdi();
631 spin_unlock_irq(&lp->lock);
632
633 if (lp->phy_media == PORT_FIBRE) {
634 cmd->supported = SUPPORTED_FIBRE;
635 cmd->port = PORT_FIBRE;
636 }
637
638 return ret;
639}
640
641static int at91ether_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
642{
643 struct at91_private *lp = netdev_priv(dev);
644 int ret;
645
646 spin_lock_irq(&lp->lock);
647 enable_mdi();
648
649 ret = mii_ethtool_sset(&lp->mii, cmd);
650
651 disable_mdi();
652 spin_unlock_irq(&lp->lock);
653
654 return ret;
655}
656
657static int at91ether_nwayreset(struct net_device *dev)
658{
659 struct at91_private *lp = netdev_priv(dev);
660 int ret;
661
662 spin_lock_irq(&lp->lock);
663 enable_mdi();
664
665 ret = mii_nway_restart(&lp->mii);
666
667 disable_mdi();
668 spin_unlock_irq(&lp->lock);
669
670 return ret;
671}
672
673static void at91ether_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
674{
675 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
676 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
677 strlcpy(info->bus_info, dev_name(dev->dev.parent), sizeof(info->bus_info));
678}
679
680static const struct ethtool_ops at91ether_ethtool_ops = {
681 .get_settings = at91ether_get_settings,
682 .set_settings = at91ether_set_settings,
683 .get_drvinfo = at91ether_get_drvinfo,
684 .nway_reset = at91ether_nwayreset,
685 .get_link = ethtool_op_get_link,
686};
687
688static int at91ether_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
689{
690 struct at91_private *lp = netdev_priv(dev);
691 int res;
692
693 if (!netif_running(dev))
694 return -EINVAL;
695
696 spin_lock_irq(&lp->lock);
697 enable_mdi();
698 res = generic_mii_ioctl(&lp->mii, if_mii(rq), cmd, NULL);
699 disable_mdi();
700 spin_unlock_irq(&lp->lock);
701
702 return res;
703}
704
705
706
707
708
709
710static void at91ether_start(struct net_device *dev)
711{
712 struct at91_private *lp = netdev_priv(dev);
713 struct recv_desc_bufs *dlist, *dlist_phys;
714 int i;
715 unsigned long ctl;
716
717 dlist = lp->dlist;
718 dlist_phys = lp->dlist_phys;
719
720 for (i = 0; i < MAX_RX_DESCR; i++) {
721 dlist->descriptors[i].addr = (unsigned int) &dlist_phys->recv_buf[i][0];
722 dlist->descriptors[i].size = 0;
723 }
724
725
726 dlist->descriptors[i-1].addr |= EMAC_DESC_WRAP;
727
728
729 lp->rxBuffIndex = 0;
730
731
732 at91_emac_write(AT91_EMAC_RBQP, (unsigned long) dlist_phys);
733
734
735 ctl = at91_emac_read(AT91_EMAC_CTL);
736 at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_RE | AT91_EMAC_TE);
737}
738
739
740
741
742static int at91ether_open(struct net_device *dev)
743{
744 struct at91_private *lp = netdev_priv(dev);
745 unsigned long ctl;
746
747 if (!is_valid_ether_addr(dev->dev_addr))
748 return -EADDRNOTAVAIL;
749
750 clk_enable(lp->ether_clk);
751
752
753 ctl = at91_emac_read(AT91_EMAC_CTL);
754 at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_CSR);
755
756
757 update_mac_address(dev);
758
759
760 enable_phyirq(dev);
761
762
763 at91_emac_write(AT91_EMAC_IER, AT91_EMAC_RCOM | AT91_EMAC_RBNA
764 | AT91_EMAC_TUND | AT91_EMAC_RTRY | AT91_EMAC_TCOM
765 | AT91_EMAC_ROVR | AT91_EMAC_ABT);
766
767
768 spin_lock_irq(&lp->lock);
769 enable_mdi();
770 update_linkspeed(dev, 0);
771 disable_mdi();
772 spin_unlock_irq(&lp->lock);
773
774 at91ether_start(dev);
775 netif_start_queue(dev);
776 return 0;
777}
778
779
780
781
782static int at91ether_close(struct net_device *dev)
783{
784 struct at91_private *lp = netdev_priv(dev);
785 unsigned long ctl;
786
787
788 ctl = at91_emac_read(AT91_EMAC_CTL);
789 at91_emac_write(AT91_EMAC_CTL, ctl & ~(AT91_EMAC_TE | AT91_EMAC_RE));
790
791
792 disable_phyirq(dev);
793
794
795 at91_emac_write(AT91_EMAC_IDR, AT91_EMAC_RCOM | AT91_EMAC_RBNA
796 | AT91_EMAC_TUND | AT91_EMAC_RTRY | AT91_EMAC_TCOM
797 | AT91_EMAC_ROVR | AT91_EMAC_ABT);
798
799 netif_stop_queue(dev);
800
801 clk_disable(lp->ether_clk);
802
803 return 0;
804}
805
806
807
808
809static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
810{
811 struct at91_private *lp = netdev_priv(dev);
812
813 if (at91_emac_read(AT91_EMAC_TSR) & AT91_EMAC_TSR_BNQ) {
814 netif_stop_queue(dev);
815
816
817 lp->skb = skb;
818 lp->skb_length = skb->len;
819 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
820 dev->stats.tx_bytes += skb->len;
821
822
823 at91_emac_write(AT91_EMAC_TAR, lp->skb_physaddr);
824
825 at91_emac_write(AT91_EMAC_TCR, skb->len);
826
827 dev->trans_start = jiffies;
828 } else {
829 printk(KERN_ERR "at91_ether.c: at91ether_start_xmit() called, but device is busy!\n");
830 return NETDEV_TX_BUSY;
831
832
833 }
834
835 return NETDEV_TX_OK;
836}
837
838
839
840
841static struct net_device_stats *at91ether_stats(struct net_device *dev)
842{
843 int ale, lenerr, seqe, lcol, ecol;
844
845 if (netif_running(dev)) {
846 dev->stats.rx_packets += at91_emac_read(AT91_EMAC_OK);
847 ale = at91_emac_read(AT91_EMAC_ALE);
848 dev->stats.rx_frame_errors += ale;
849 lenerr = at91_emac_read(AT91_EMAC_ELR) + at91_emac_read(AT91_EMAC_USF);
850 dev->stats.rx_length_errors += lenerr;
851 seqe = at91_emac_read(AT91_EMAC_SEQE);
852 dev->stats.rx_crc_errors += seqe;
853 dev->stats.rx_fifo_errors += at91_emac_read(AT91_EMAC_DRFC);
854 dev->stats.rx_errors += (ale + lenerr + seqe
855 + at91_emac_read(AT91_EMAC_CDE) + at91_emac_read(AT91_EMAC_RJB));
856
857 dev->stats.tx_packets += at91_emac_read(AT91_EMAC_FRA);
858 dev->stats.tx_fifo_errors += at91_emac_read(AT91_EMAC_TUE);
859 dev->stats.tx_carrier_errors += at91_emac_read(AT91_EMAC_CSE);
860 dev->stats.tx_heartbeat_errors += at91_emac_read(AT91_EMAC_SQEE);
861
862 lcol = at91_emac_read(AT91_EMAC_LCOL);
863 ecol = at91_emac_read(AT91_EMAC_ECOL);
864 dev->stats.tx_window_errors += lcol;
865 dev->stats.tx_aborted_errors += ecol;
866
867 dev->stats.collisions += (at91_emac_read(AT91_EMAC_SCOL) + at91_emac_read(AT91_EMAC_MCOL) + lcol + ecol);
868 }
869 return &dev->stats;
870}
871
872
873
874
875
876static void at91ether_rx(struct net_device *dev)
877{
878 struct at91_private *lp = netdev_priv(dev);
879 struct recv_desc_bufs *dlist;
880 unsigned char *p_recv;
881 struct sk_buff *skb;
882 unsigned int pktlen;
883
884 dlist = lp->dlist;
885 while (dlist->descriptors[lp->rxBuffIndex].addr & EMAC_DESC_DONE) {
886 p_recv = dlist->recv_buf[lp->rxBuffIndex];
887 pktlen = dlist->descriptors[lp->rxBuffIndex].size & 0x7ff;
888 skb = dev_alloc_skb(pktlen + 2);
889 if (skb != NULL) {
890 skb_reserve(skb, 2);
891 memcpy(skb_put(skb, pktlen), p_recv, pktlen);
892
893 skb->protocol = eth_type_trans(skb, dev);
894 dev->stats.rx_bytes += pktlen;
895 netif_rx(skb);
896 }
897 else {
898 dev->stats.rx_dropped += 1;
899 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
900 }
901
902 if (dlist->descriptors[lp->rxBuffIndex].size & EMAC_MULTICAST)
903 dev->stats.multicast++;
904
905 dlist->descriptors[lp->rxBuffIndex].addr &= ~EMAC_DESC_DONE;
906 if (lp->rxBuffIndex == MAX_RX_DESCR-1)
907 lp->rxBuffIndex = 0;
908 else
909 lp->rxBuffIndex++;
910 }
911}
912
913
914
915
916static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
917{
918 struct net_device *dev = (struct net_device *) dev_id;
919 struct at91_private *lp = netdev_priv(dev);
920 unsigned long intstatus, ctl;
921
922
923
924 intstatus = at91_emac_read(AT91_EMAC_ISR);
925
926 if (intstatus & AT91_EMAC_RCOM)
927 at91ether_rx(dev);
928
929 if (intstatus & AT91_EMAC_TCOM) {
930
931 if (intstatus & (AT91_EMAC_TUND | AT91_EMAC_RTRY))
932 dev->stats.tx_errors += 1;
933
934 if (lp->skb) {
935 dev_kfree_skb_irq(lp->skb);
936 lp->skb = NULL;
937 dma_unmap_single(NULL, lp->skb_physaddr, lp->skb_length, DMA_TO_DEVICE);
938 }
939 netif_wake_queue(dev);
940 }
941
942
943 if (intstatus & AT91_EMAC_RBNA) {
944 ctl = at91_emac_read(AT91_EMAC_CTL);
945 at91_emac_write(AT91_EMAC_CTL, ctl & ~AT91_EMAC_RE);
946 at91_emac_write(AT91_EMAC_CTL, ctl | AT91_EMAC_RE);
947 }
948
949 if (intstatus & AT91_EMAC_ROVR)
950 printk("%s: ROVR error\n", dev->name);
951
952 return IRQ_HANDLED;
953}
954
955#ifdef CONFIG_NET_POLL_CONTROLLER
956static void at91ether_poll_controller(struct net_device *dev)
957{
958 unsigned long flags;
959
960 local_irq_save(flags);
961 at91ether_interrupt(dev->irq, dev);
962 local_irq_restore(flags);
963}
964#endif
965
966static const struct net_device_ops at91ether_netdev_ops = {
967 .ndo_open = at91ether_open,
968 .ndo_stop = at91ether_close,
969 .ndo_start_xmit = at91ether_start_xmit,
970 .ndo_get_stats = at91ether_stats,
971 .ndo_set_multicast_list = at91ether_set_multicast_list,
972 .ndo_set_mac_address = set_mac_address,
973 .ndo_do_ioctl = at91ether_ioctl,
974 .ndo_validate_addr = eth_validate_addr,
975 .ndo_change_mtu = eth_change_mtu,
976#ifdef CONFIG_NET_POLL_CONTROLLER
977 .ndo_poll_controller = at91ether_poll_controller,
978#endif
979};
980
981
982
983
984static int __init at91ether_setup(unsigned long phy_type, unsigned short phy_address,
985 struct platform_device *pdev, struct clk *ether_clk)
986{
987 struct at91_eth_data *board_data = pdev->dev.platform_data;
988 struct net_device *dev;
989 struct at91_private *lp;
990 unsigned int val;
991 int res;
992
993 dev = alloc_etherdev(sizeof(struct at91_private));
994 if (!dev)
995 return -ENOMEM;
996
997 dev->base_addr = AT91_VA_BASE_EMAC;
998 dev->irq = AT91RM9200_ID_EMAC;
999
1000
1001 if (request_irq(dev->irq, at91ether_interrupt, 0, dev->name, dev)) {
1002 free_netdev(dev);
1003 return -EBUSY;
1004 }
1005
1006
1007 lp = netdev_priv(dev);
1008 lp->dlist = (struct recv_desc_bufs *) dma_alloc_coherent(NULL, sizeof(struct recv_desc_bufs), (dma_addr_t *) &lp->dlist_phys, GFP_KERNEL);
1009 if (lp->dlist == NULL) {
1010 free_irq(dev->irq, dev);
1011 free_netdev(dev);
1012 return -ENOMEM;
1013 }
1014 lp->board_data = *board_data;
1015 lp->ether_clk = ether_clk;
1016 platform_set_drvdata(pdev, dev);
1017
1018 spin_lock_init(&lp->lock);
1019
1020 ether_setup(dev);
1021 dev->netdev_ops = &at91ether_netdev_ops;
1022 dev->ethtool_ops = &at91ether_ethtool_ops;
1023
1024 SET_NETDEV_DEV(dev, &pdev->dev);
1025
1026 get_mac_address(dev);
1027 update_mac_address(dev);
1028
1029 at91_emac_write(AT91_EMAC_CTL, 0);
1030
1031 if (lp->board_data.is_rmii)
1032 at91_emac_write(AT91_EMAC_CFG, AT91_EMAC_CLK_DIV32 | AT91_EMAC_BIG | AT91_EMAC_RMII);
1033 else
1034 at91_emac_write(AT91_EMAC_CFG, AT91_EMAC_CLK_DIV32 | AT91_EMAC_BIG);
1035
1036
1037 spin_lock_irq(&lp->lock);
1038 enable_mdi();
1039 if ((phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID)) {
1040 read_phy(phy_address, MII_DSCR_REG, &val);
1041 if ((val & (1 << 10)) == 0)
1042 lp->phy_media = PORT_FIBRE;
1043 } else if (machine_is_csb337()) {
1044
1045 write_phy(phy_address, MII_LEDCTRL_REG, 0x0d22);
1046 } else if (machine_is_ecbat91())
1047 write_phy(phy_address, MII_LEDCTRL_REG, 0x156A);
1048
1049 disable_mdi();
1050 spin_unlock_irq(&lp->lock);
1051
1052 lp->mii.dev = dev;
1053 lp->mii.mdio_read = mdio_read;
1054 lp->mii.mdio_write = mdio_write;
1055 lp->mii.phy_id = phy_address;
1056 lp->mii.phy_id_mask = 0x1f;
1057 lp->mii.reg_num_mask = 0x1f;
1058
1059 lp->phy_type = phy_type;
1060 lp->phy_address = phy_address;
1061
1062
1063 res = register_netdev(dev);
1064 if (res) {
1065 free_irq(dev->irq, dev);
1066 free_netdev(dev);
1067 dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
1068 return res;
1069 }
1070
1071
1072 spin_lock_irq(&lp->lock);
1073 enable_mdi();
1074 update_linkspeed(dev, 0);
1075 disable_mdi();
1076 spin_unlock_irq(&lp->lock);
1077 netif_carrier_off(dev);
1078
1079
1080 if (!lp->board_data.phy_irq_pin) {
1081 init_timer(&lp->check_timer);
1082 lp->check_timer.data = (unsigned long)dev;
1083 lp->check_timer.function = at91ether_check_link;
1084 } else if (lp->board_data.phy_irq_pin >= 32)
1085 gpio_request(lp->board_data.phy_irq_pin, "ethernet_phy");
1086
1087
1088 printk(KERN_INFO "%s: AT91 ethernet at 0x%08x int=%d %s%s (%pM)\n",
1089 dev->name, (uint) dev->base_addr, dev->irq,
1090 at91_emac_read(AT91_EMAC_CFG) & AT91_EMAC_SPD ? "100-" : "10-",
1091 at91_emac_read(AT91_EMAC_CFG) & AT91_EMAC_FD ? "FullDuplex" : "HalfDuplex",
1092 dev->dev_addr);
1093 if ((phy_type == MII_DM9161_ID) || (lp->phy_type == MII_DM9161A_ID))
1094 printk(KERN_INFO "%s: Davicom 9161 PHY %s\n", dev->name, (lp->phy_media == PORT_FIBRE) ? "(Fiber)" : "(Copper)");
1095 else if (phy_type == MII_LXT971A_ID)
1096 printk(KERN_INFO "%s: Intel LXT971A PHY\n", dev->name);
1097 else if (phy_type == MII_RTL8201_ID)
1098 printk(KERN_INFO "%s: Realtek RTL8201(B)L PHY\n", dev->name);
1099 else if (phy_type == MII_BCM5221_ID)
1100 printk(KERN_INFO "%s: Broadcom BCM5221 PHY\n", dev->name);
1101 else if (phy_type == MII_DP83847_ID)
1102 printk(KERN_INFO "%s: National Semiconductor DP83847 PHY\n", dev->name);
1103 else if (phy_type == MII_DP83848_ID)
1104 printk(KERN_INFO "%s: National Semiconductor DP83848 PHY\n", dev->name);
1105 else if (phy_type == MII_AC101L_ID)
1106 printk(KERN_INFO "%s: Altima AC101L PHY\n", dev->name);
1107 else if (phy_type == MII_KS8721_ID)
1108 printk(KERN_INFO "%s: Micrel KS8721 PHY\n", dev->name);
1109 else if (phy_type == MII_T78Q21x3_ID)
1110 printk(KERN_INFO "%s: Teridian 78Q21x3 PHY\n", dev->name);
1111 else if (phy_type == MII_LAN83C185_ID)
1112 printk(KERN_INFO "%s: SMSC LAN83C185 PHY\n", dev->name);
1113
1114 return 0;
1115}
1116
1117
1118
1119
1120static int __init at91ether_probe(struct platform_device *pdev)
1121{
1122 unsigned int phyid1, phyid2;
1123 int detected = -1;
1124 unsigned long phy_id;
1125 unsigned short phy_address = 0;
1126 struct clk *ether_clk;
1127
1128 ether_clk = clk_get(&pdev->dev, "ether_clk");
1129 if (IS_ERR(ether_clk)) {
1130 printk(KERN_ERR "at91_ether: no clock defined\n");
1131 return -ENODEV;
1132 }
1133 clk_enable(ether_clk);
1134
1135 while ((detected != 0) && (phy_address < 32)) {
1136
1137 enable_mdi();
1138 read_phy(phy_address, MII_PHYSID1, &phyid1);
1139 read_phy(phy_address, MII_PHYSID2, &phyid2);
1140 disable_mdi();
1141
1142 phy_id = (phyid1 << 16) | (phyid2 & 0xfff0);
1143 switch (phy_id) {
1144 case MII_DM9161_ID:
1145 case MII_DM9161A_ID:
1146 case MII_LXT971A_ID:
1147 case MII_RTL8201_ID:
1148 case MII_BCM5221_ID:
1149 case MII_DP83847_ID:
1150 case MII_DP83848_ID:
1151 case MII_AC101L_ID:
1152 case MII_KS8721_ID:
1153 case MII_T78Q21x3_ID:
1154 case MII_LAN83C185_ID:
1155 detected = at91ether_setup(phy_id, phy_address, pdev, ether_clk);
1156 break;
1157 }
1158
1159 phy_address++;
1160 }
1161
1162 clk_disable(ether_clk);
1163
1164 return detected;
1165}
1166
1167static int __devexit at91ether_remove(struct platform_device *pdev)
1168{
1169 struct net_device *dev = platform_get_drvdata(pdev);
1170 struct at91_private *lp = netdev_priv(dev);
1171
1172 if (lp->board_data.phy_irq_pin >= 32)
1173 gpio_free(lp->board_data.phy_irq_pin);
1174
1175 unregister_netdev(dev);
1176 free_irq(dev->irq, dev);
1177 dma_free_coherent(NULL, sizeof(struct recv_desc_bufs), lp->dlist, (dma_addr_t)lp->dlist_phys);
1178 clk_put(lp->ether_clk);
1179
1180 platform_set_drvdata(pdev, NULL);
1181 free_netdev(dev);
1182 return 0;
1183}
1184
1185#ifdef CONFIG_PM
1186
1187static int at91ether_suspend(struct platform_device *pdev, pm_message_t mesg)
1188{
1189 struct net_device *net_dev = platform_get_drvdata(pdev);
1190 struct at91_private *lp = netdev_priv(net_dev);
1191 int phy_irq = lp->board_data.phy_irq_pin;
1192
1193 if (netif_running(net_dev)) {
1194 if (phy_irq)
1195 disable_irq(phy_irq);
1196
1197 netif_stop_queue(net_dev);
1198 netif_device_detach(net_dev);
1199
1200 clk_disable(lp->ether_clk);
1201 }
1202 return 0;
1203}
1204
1205static int at91ether_resume(struct platform_device *pdev)
1206{
1207 struct net_device *net_dev = platform_get_drvdata(pdev);
1208 struct at91_private *lp = netdev_priv(net_dev);
1209 int phy_irq = lp->board_data.phy_irq_pin;
1210
1211 if (netif_running(net_dev)) {
1212 clk_enable(lp->ether_clk);
1213
1214 netif_device_attach(net_dev);
1215 netif_start_queue(net_dev);
1216
1217 if (phy_irq)
1218 enable_irq(phy_irq);
1219 }
1220 return 0;
1221}
1222
1223#else
1224#define at91ether_suspend NULL
1225#define at91ether_resume NULL
1226#endif
1227
1228static struct platform_driver at91ether_driver = {
1229 .remove = __devexit_p(at91ether_remove),
1230 .suspend = at91ether_suspend,
1231 .resume = at91ether_resume,
1232 .driver = {
1233 .name = DRV_NAME,
1234 .owner = THIS_MODULE,
1235 },
1236};
1237
1238static int __init at91ether_init(void)
1239{
1240 return platform_driver_probe(&at91ether_driver, at91ether_probe);
1241}
1242
1243static void __exit at91ether_exit(void)
1244{
1245 platform_driver_unregister(&at91ether_driver);
1246}
1247
1248module_init(at91ether_init)
1249module_exit(at91ether_exit)
1250
1251MODULE_LICENSE("GPL");
1252MODULE_DESCRIPTION("AT91RM9200 EMAC Ethernet driver");
1253MODULE_AUTHOR("Andrew Victor");
1254MODULE_ALIAS("platform:" DRV_NAME);
1255