Restore the gpio.h file, platform.c uses the generic GPIO API
[openwrt.git] / target / linux / aruba-2.6 / files / drivers / net / ar2313 / ar2313.c
1 /*
2 * ar2313.c: Linux driver for the Atheros AR2313 Ethernet device.
3 *
4 * Copyright 2004 by Sameer Dekate, <sdekate@arubanetworks.com>.
5 * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * Thanks to Atheros for providing hardware and documentation
8 * enabling me to write this driver.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * Additional credits:
16 * This code is taken from John Taylor's Sibyte driver and then
17 * modified for the AR2313.
18 */
19
20 #include <linux/autoconf.h>
21 #include <linux/module.h>
22 #include <linux/version.h>
23 #include <linux/types.h>
24 #include <linux/errno.h>
25 #include <linux/ioport.h>
26 #include <linux/pci.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/init.h>
31 #include <linux/delay.h>
32 #include <linux/mm.h>
33 #include <linux/highmem.h>
34 #include <linux/sockios.h>
35 #include <linux/pkt_sched.h>
36 #include <linux/compile.h>
37 #include <linux/mii.h>
38 #include <linux/ethtool.h>
39 #include <linux/ctype.h>
40
41 #include <net/sock.h>
42 #include <net/ip.h>
43
44 #include <asm/system.h>
45 #include <asm/io.h>
46 #include <asm/irq.h>
47 #include <asm/byteorder.h>
48 #include <asm/uaccess.h>
49 #include <asm/bootinfo.h>
50
51 extern char *getenv(char *e);
52
53
54 #undef INDEX_DEBUG
55 #define DEBUG 0
56 #define DEBUG_TX 0
57 #define DEBUG_RX 0
58 #define DEBUG_INT 0
59 #define DEBUG_MC 0
60 #define DEBUG_ERR 1
61
62 #ifndef __exit
63 #define __exit
64 #endif
65
66 #ifndef min
67 #define min(a,b) (((a)<(b))?(a):(b))
68 #endif
69
70 #ifndef SMP_CACHE_BYTES
71 #define SMP_CACHE_BYTES L1_CACHE_BYTES
72 #endif
73
74 #ifndef SET_MODULE_OWNER
75 #define SET_MODULE_OWNER(dev) {do{} while(0);}
76 #define AR2313_MOD_INC_USE_COUNT MOD_INC_USE_COUNT
77 #define AR2313_MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT
78 #else
79 #define AR2313_MOD_INC_USE_COUNT {do{} while(0);}
80 #define AR2313_MOD_DEC_USE_COUNT {do{} while(0);}
81 #endif
82
83 #define PHYSADDR(a) ((_ACAST32_ (a)) & 0x1fffffff)
84
85 static char ethaddr[18] = "00:00:00:00:00:00";
86 static char ifname[5] = "bond";
87
88 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,52)
89 module_param_string(ethaddr, ethaddr, 18, 0);
90 module_param_string(ifname, ifname, 5, 0);
91 #else
92 MODULE_PARM(ethaddr, "c18");
93 MODULE_PARM(ifname, "c5");
94 #endif
95
96 #define AR2313_MBOX_SET_BIT 0x8
97
98 #define BOARD_IDX_STATIC 0
99 #define BOARD_IDX_OVERFLOW -1
100
101 /* margot includes */
102 #include <asm/idt-boards/rc32434/rc32434.h>
103
104 #include "ar2313_msg.h"
105 #include "platform.h"
106 #include "dma.h"
107 #include "ar2313.h"
108
109 /*
110 * New interrupt handler strategy:
111 *
112 * An old interrupt handler worked using the traditional method of
113 * replacing an skbuff with a new one when a packet arrives. However
114 * the rx rings do not need to contain a static number of buffer
115 * descriptors, thus it makes sense to move the memory allocation out
116 * of the main interrupt handler and do it in a bottom half handler
117 * and only allocate new buffers when the number of buffers in the
118 * ring is below a certain threshold. In order to avoid starving the
119 * NIC under heavy load it is however necessary to force allocation
120 * when hitting a minimum threshold. The strategy for alloction is as
121 * follows:
122 *
123 * RX_LOW_BUF_THRES - allocate buffers in the bottom half
124 * RX_PANIC_LOW_THRES - we are very low on buffers, allocate
125 * the buffers in the interrupt handler
126 * RX_RING_THRES - maximum number of buffers in the rx ring
127 *
128 * One advantagous side effect of this allocation approach is that the
129 * entire rx processing can be done without holding any spin lock
130 * since the rx rings and registers are totally independent of the tx
131 * ring and its registers. This of course includes the kmalloc's of
132 * new skb's. Thus start_xmit can run in parallel with rx processing
133 * and the memory allocation on SMP systems.
134 *
135 * Note that running the skb reallocation in a bottom half opens up
136 * another can of races which needs to be handled properly. In
137 * particular it can happen that the interrupt handler tries to run
138 * the reallocation while the bottom half is either running on another
139 * CPU or was interrupted on the same CPU. To get around this the
140 * driver uses bitops to prevent the reallocation routines from being
141 * reentered.
142 *
143 * TX handling can also be done without holding any spin lock, wheee
144 * this is fun! since tx_csm is only written to by the interrupt
145 * handler.
146 */
147
148 /*
149 * Threshold values for RX buffer allocation - the low water marks for
150 * when to start refilling the rings are set to 75% of the ring
151 * sizes. It seems to make sense to refill the rings entirely from the
152 * intrrupt handler once it gets below the panic threshold, that way
153 * we don't risk that the refilling is moved to another CPU when the
154 * one running the interrupt handler just got the slab code hot in its
155 * cache.
156 */
157 #define RX_RING_SIZE AR2313_DESCR_ENTRIES
158 #define RX_PANIC_THRES (RX_RING_SIZE/4)
159 #define RX_LOW_THRES ((3*RX_RING_SIZE)/4)
160 #define CRC_LEN 4
161 #define RX_OFFSET 2
162
163 #define AR2313_BUFSIZE (AR2313_MTU + ETH_HLEN + CRC_LEN + RX_OFFSET)
164
165 #ifdef MODULE
166 MODULE_AUTHOR("Sameer Dekate<sdekate@arubanetworks.com>");
167 MODULE_DESCRIPTION("AR2313 Ethernet driver");
168 #endif
169
170 #if DEBUG
171 static char version[] __initdata =
172 "ar2313.c: v0.02 2006/06/19 sdekate@arubanetworks.com\n";
173 #endif /* DEBUG */
174
175 #define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
176
177 // prototypes
178 static short armiiread(short phy, short reg);
179 static void armiiwrite(short phy, short reg, short data);
180 #ifdef TX_TIMEOUT
181 static void ar2313_tx_timeout(struct net_device *dev);
182 #endif
183 static void ar2313_halt(struct net_device *dev);
184 static void rx_tasklet_func(unsigned long data);
185 static void ar2313_multicast_list(struct net_device *dev);
186
187 static struct net_device *root_dev;
188 static int probed __initdata = 0;
189 static unsigned long ar_eth_base;
190 static unsigned long ar_dma_base;
191 static unsigned long ar_int_base;
192 static unsigned long ar_int_mac_mask;
193 static unsigned long ar_int_phy_mask;
194
195 #ifndef ERR
196 #define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
197 #endif
198
199 static int parse_mac_addr(struct net_device *dev, char* macstr){
200 int i, j;
201 unsigned char result, value;
202
203 for (i=0; i<6; i++) {
204 result = 0;
205 if (i != 5 && *(macstr+2) != ':') {
206 ERR("invalid mac address format: %d %c\n",
207 i, *(macstr+2));
208 return -EINVAL;
209 }
210 for (j=0; j<2; j++) {
211 if (isxdigit(*macstr) && (value = isdigit(*macstr) ? *macstr-'0' :
212 toupper(*macstr)-'A'+10) < 16)
213 {
214 result = result*16 + value;
215 macstr++;
216 }
217 else {
218 ERR("invalid mac address "
219 "character: %c\n", *macstr);
220 return -EINVAL;
221 }
222 }
223
224 macstr++;
225 dev->dev_addr[i] = result;
226 }
227
228 return 0;
229 }
230
231
232 int __init ar2313_probe(void)
233 {
234 struct net_device *dev;
235 struct ar2313_private *sp;
236 int version_disp;
237 char name[64] ;
238
239 if (probed)
240 return -ENODEV;
241 probed++;
242
243 version_disp = 0;
244 sprintf(name, "%s%%d", ifname) ;
245 dev = alloc_etherdev(sizeof(struct ar2313_private));
246
247 if (dev == NULL) {
248 printk(KERN_ERR "ar2313: Unable to allocate net_device structure!\n");
249 return -ENOMEM;
250 }
251
252 SET_MODULE_OWNER(dev);
253
254 sp = dev->priv;
255
256 sp->link = 0;
257 switch (mips_machtype) {
258 case MACH_ARUBA_AP60:
259 ar_eth_base = 0xb8100000;
260 ar_dma_base = ar_eth_base + 0x1000;
261 ar_int_base = 0x1C003020;
262 ar_int_mac_mask = RESET_ENET0|RESET_ENET1;
263 ar_int_phy_mask = RESET_EPHY0|RESET_EPHY1;
264 sp->mac = 1;
265 sp->phy = 1;
266 dev->irq = 4;
267 break;
268
269 case MACH_ARUBA_AP40:
270 ar_eth_base = 0xb0500000;
271 ar_dma_base = ar_eth_base + 0x1000;
272 ar_int_base = 0x11000004;
273 ar_int_mac_mask = 0x800;
274 ar_int_phy_mask = 0x400;
275 sp->mac = 0;
276 sp->phy = 1;
277 dev->irq = 4;
278 break;
279
280 case MACH_ARUBA_AP65:
281 ar_eth_base = 0xb8100000;
282 ar_dma_base = ar_eth_base + 0x1000;
283 ar_int_base = 0x1C003020;
284 ar_int_mac_mask = RESET_ENET0|RESET_ENET1;
285 ar_int_phy_mask = RESET_EPHY0|RESET_EPHY1;
286 sp->mac = 0;
287 #if 0
288 // commented out, for now
289
290 if (mips_machtype == MACH_ARUBA_SAMSUNG) {
291 sp->phy = 0x1f;
292 } else {
293 sp->phy = 1;
294 }
295 #else
296 sp->phy = 1;
297 #endif
298 dev->irq = 3;
299 break;
300
301 default:
302 printk("%s: unsupported mips_machtype=0x%lx\n",
303 __FUNCTION__, mips_machtype) ;
304 return -ENODEV;
305 }
306
307 spin_lock_init(&sp->lock);
308
309 /* initialize func pointers */
310 dev->open = &ar2313_open;
311 dev->stop = &ar2313_close;
312 dev->hard_start_xmit = &ar2313_start_xmit;
313
314 dev->get_stats = &ar2313_get_stats;
315 dev->set_multicast_list = &ar2313_multicast_list;
316 #ifdef TX_TIMEOUT
317 dev->tx_timeout = ar2313_tx_timeout;
318 dev->watchdog_timeo = AR2313_TX_TIMEOUT;
319 #endif
320 dev->do_ioctl = &ar2313_ioctl;
321
322 // SAMEER: do we need this?
323 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA;
324
325 tasklet_init(&sp->rx_tasklet, rx_tasklet_func, (unsigned long) dev);
326 tasklet_disable(&sp->rx_tasklet);
327
328 /* display version info if adapter is found */
329 if (!version_disp) {
330 /* set display flag to TRUE so that */
331 /* we only display this string ONCE */
332 version_disp = 1;
333 #if DEBUG
334 printk(version);
335 #endif /* DEBUG */
336 }
337
338 request_region(PHYSADDR(ETHERNET_BASE), ETHERNET_SIZE*ETHERNET_MACS,
339 "AR2313ENET");
340
341 sp->eth_regs = ioremap_nocache(PHYSADDR(ETHERNET_BASE + ETHERNET_SIZE*sp->mac),
342 sizeof(*sp->eth_regs));
343 if (!sp->eth_regs) {
344 printk("Can't remap eth registers\n");
345 return(-ENXIO);
346 }
347
348 sp->dma_regs = ioremap_nocache(PHYSADDR(DMA_BASE + DMA_SIZE*sp->mac),
349 sizeof(*sp->dma_regs));
350 dev->base_addr = (unsigned int) sp->dma_regs;
351 if (!sp->dma_regs) {
352 printk("Can't remap DMA registers\n");
353 return(-ENXIO);
354 }
355
356 sp->int_regs = ioremap_nocache(PHYSADDR(INTERRUPT_BASE),
357 sizeof(*sp->int_regs));
358 if (!sp->int_regs) {
359 printk("Can't remap INTERRUPT registers\n");
360 return(-ENXIO);
361 }
362
363 strncpy(sp->name, "Atheros AR2313", sizeof (sp->name) - 1);
364 sp->name [sizeof (sp->name) - 1] = '\0';
365
366 {
367 char mac[32];
368 extern char *getenv(char *e);
369 unsigned char def_mac[6] = {0, 0x0b, 0x86, 0xba, 0xdb, 0xad};
370 memset(mac, 0, 32);
371 memcpy(mac, getenv("ethaddr"), 17);
372 if (parse_mac_addr(dev, mac)){
373 printk("%s: MAC address not found, using default\n", __func__);
374 memcpy(dev->dev_addr, def_mac, 6);
375 }
376 }
377
378 sp->board_idx = BOARD_IDX_STATIC;
379
380 if (ar2313_init(dev)) {
381 /*
382 * ar2313_init() calls ar2313_init_cleanup() on error.
383 */
384 kfree(dev);
385 return -ENODEV;
386 }
387
388 if (register_netdev(dev)){
389 printk("%s: register_netdev failed\n", __func__);
390 return -1;
391 }
392
393 printk("%s: %s: %02x:%02x:%02x:%02x:%02x:%02x, irq %d\n",
394 dev->name, sp->name,
395 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
396 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5],
397 dev->irq);
398
399 /* start link poll timer */
400 ar2313_setup_timer(dev);
401
402 /*
403 * Register the device
404 */
405 root_dev = dev;
406
407 return 0;
408 }
409
410 #if 0
411 static void ar2313_dump_regs(struct net_device *dev)
412 {
413 unsigned int *ptr, i;
414 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
415
416 ptr = (unsigned int *)sp->eth_regs;
417 for(i=0; i< (sizeof(ETHERNET_STRUCT)/ sizeof(unsigned int)); i++, ptr++) {
418 printk("ENET: %08x = %08x\n", (int)ptr, *ptr);
419 }
420
421 ptr = (unsigned int *)sp->dma_regs;
422 for(i=0; i< (sizeof(DMA)/ sizeof(unsigned int)); i++, ptr++) {
423 printk("DMA: %08x = %08x\n", (int)ptr, *ptr);
424 }
425
426 ptr = (unsigned int *)sp->int_regs;
427 for(i=0; i< (sizeof(INTERRUPT)/ sizeof(unsigned int)); i++, ptr++){
428 printk("INT: %08x = %08x\n", (int)ptr, *ptr);
429 }
430
431 for (i = 0; i < AR2313_DESCR_ENTRIES; i++) {
432 ar2313_descr_t *td = &sp->tx_ring[i];
433 printk("Tx desc %2d: %08x %08x %08x %08x\n", i,
434 td->status, td->devcs, td->addr, td->descr);
435 }
436 }
437 #endif
438
439 #ifdef TX_TIMEOUT
440 static void
441 ar2313_tx_timeout(struct net_device *dev)
442 {
443 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
444 unsigned long flags;
445
446 #if DEBUG_TX
447 printk("Tx timeout\n");
448 #endif
449 spin_lock_irqsave(&sp->lock, flags);
450 ar2313_restart(dev);
451 spin_unlock_irqrestore(&sp->lock, flags);
452 }
453 #endif
454
455 #if DEBUG_MC
456 static void
457 printMcList(struct net_device *dev)
458 {
459 struct dev_mc_list *list = dev->mc_list;
460 int num=0, i;
461 while(list){
462 printk("%d MC ADDR ", num);
463 for(i=0;i<list->dmi_addrlen;i++) {
464 printk(":%02x", list->dmi_addr[i]);
465 }
466 list = list->next;
467 printk("\n");
468 }
469 }
470 #endif
471
472 /*
473 * Set or clear the multicast filter for this adaptor.
474 * THIS IS ABSOLUTE CRAP, disabled
475 */
476 static void
477 ar2313_multicast_list(struct net_device *dev)
478 {
479 /*
480 * Always listen to broadcasts and
481 * treat IFF bits independently
482 */
483 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
484 unsigned int recognise;
485
486 recognise = sp->eth_regs->mac_control;
487
488 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
489 recognise |= MAC_CONTROL_PR;
490 } else {
491 recognise &= ~MAC_CONTROL_PR;
492 }
493
494 if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
495 #if DEBUG_MC
496 printMcList(dev);
497 printk("%s: all MULTICAST mc_count %d\n", __FUNCTION__, dev->mc_count);
498 #endif
499 recognise |= MAC_CONTROL_PM;/* all multicast */
500 } else if (dev->mc_count > 0) {
501 #if DEBUG_MC
502 printMcList(dev);
503 printk("%s: mc_count %d\n", __FUNCTION__, dev->mc_count);
504 #endif
505 recognise |= MAC_CONTROL_PM; /* for the time being */
506 }
507 #if DEBUG_MC
508 printk("%s: setting %08x to %08x\n", __FUNCTION__, (int)sp->eth_regs, recognise);
509 #endif
510
511 sp->eth_regs->mac_control = recognise;
512 }
513
514 static void rx_tasklet_cleanup(struct net_device *dev)
515 {
516 struct ar2313_private *sp = dev->priv;
517
518 /*
519 * Tasklet may be scheduled. Need to get it removed from the list
520 * since we're about to free the struct.
521 */
522
523 sp->unloading = 1;
524 tasklet_enable(&sp->rx_tasklet);
525 tasklet_kill(&sp->rx_tasklet);
526 }
527
528 static void __exit ar2313_module_cleanup(void)
529 {
530 rx_tasklet_cleanup(root_dev);
531 ar2313_init_cleanup(root_dev);
532 unregister_netdev(root_dev);
533 kfree(root_dev);
534 release_region(PHYSADDR(ETHERNET_BASE), ETHERNET_SIZE*ETHERNET_MACS);
535 }
536
537
538 /*
539 * Restart the AR2313 ethernet controller.
540 */
541 static int ar2313_restart(struct net_device *dev)
542 {
543 /* disable interrupts */
544 disable_irq(dev->irq);
545
546 /* stop mac */
547 ar2313_halt(dev);
548
549 /* initialize */
550 ar2313_init(dev);
551
552 /* enable interrupts */
553 enable_irq(dev->irq);
554
555 return 0;
556 }
557
558 extern unsigned long mips_machtype;
559
560 int __init ar2313_module_init(void)
561 {
562 int status=-1;
563 switch (mips_machtype){
564 case MACH_ARUBA_AP60:
565 case MACH_ARUBA_AP65:
566 case MACH_ARUBA_AP40:
567 root_dev = NULL;
568 status = ar2313_probe();
569 break;
570 }
571 return status;
572 }
573
574
575 module_init(ar2313_module_init);
576 module_exit(ar2313_module_cleanup);
577
578
579 static void ar2313_free_descriptors(struct net_device *dev)
580 {
581 struct ar2313_private *sp = dev->priv;
582 if (sp->rx_ring != NULL) {
583 kfree((void*)KSEG0ADDR(sp->rx_ring));
584 sp->rx_ring = NULL;
585 sp->tx_ring = NULL;
586 }
587 }
588
589
590 static int ar2313_allocate_descriptors(struct net_device *dev)
591 {
592 struct ar2313_private *sp = dev->priv;
593 int size;
594 int j;
595 ar2313_descr_t *space;
596
597 if(sp->rx_ring != NULL){
598 printk("%s: already done.\n", __FUNCTION__);
599 return 0;
600 }
601
602 size = (sizeof(ar2313_descr_t) * (AR2313_DESCR_ENTRIES * AR2313_QUEUES));
603 space = kmalloc(size, GFP_KERNEL);
604 if (space == NULL)
605 return 1;
606
607 /* invalidate caches */
608 dma_cache_inv((unsigned int)space, size);
609
610 /* now convert pointer to KSEG1 */
611 space = (ar2313_descr_t *)KSEG1ADDR(space);
612
613 memset((void *)space, 0, size);
614
615 sp->rx_ring = space;
616 space += AR2313_DESCR_ENTRIES;
617
618 sp->tx_ring = space;
619 space += AR2313_DESCR_ENTRIES;
620
621 /* Initialize the transmit Descriptors */
622 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
623 ar2313_descr_t *td = &sp->tx_ring[j];
624 td->status = 0;
625 td->devcs = DMA_TX1_CHAINED;
626 td->addr = 0;
627 td->descr = K1_TO_PHYS(&sp->tx_ring[(j+1) & (AR2313_DESCR_ENTRIES-1)]);
628 }
629
630 return 0;
631 }
632
633
634 /*
635 * Generic cleanup handling data allocated during init. Used when the
636 * module is unloaded or if an error occurs during initialization
637 */
638 static void ar2313_init_cleanup(struct net_device *dev)
639 {
640 struct ar2313_private *sp = dev->priv;
641 struct sk_buff *skb;
642 int j;
643
644 ar2313_free_descriptors(dev);
645
646 if (sp->eth_regs) iounmap((void*)sp->eth_regs);
647 if (sp->dma_regs) iounmap((void*)sp->dma_regs);
648
649 if (sp->rx_skb) {
650 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
651 skb = sp->rx_skb[j];
652 if (skb) {
653 sp->rx_skb[j] = NULL;
654 dev_kfree_skb(skb);
655 }
656 }
657 kfree(sp->rx_skb);
658 sp->rx_skb = NULL;
659 }
660
661 if (sp->tx_skb) {
662 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
663 skb = sp->tx_skb[j];
664 if (skb) {
665 sp->tx_skb[j] = NULL;
666 dev_kfree_skb(skb);
667 }
668 }
669 kfree(sp->tx_skb);
670 sp->tx_skb = NULL;
671 }
672 }
673
674 static int ar2313_setup_timer(struct net_device *dev)
675 {
676 struct ar2313_private *sp = dev->priv;
677
678 init_timer(&sp->link_timer);
679
680 sp->link_timer.function = ar2313_link_timer_fn;
681 sp->link_timer.data = (int) dev;
682 sp->link_timer.expires = jiffies + HZ;
683
684 add_timer(&sp->link_timer);
685 return 0;
686
687 }
688
689 static void ar2313_link_timer_fn(unsigned long data)
690 {
691 struct net_device *dev = (struct net_device *) data;
692 struct ar2313_private *sp = dev->priv;
693
694 // see if the link status changed
695 // This was needed to make sure we set the PHY to the
696 // autonegotiated value of half or full duplex.
697 ar2313_check_link(dev);
698
699 // Loop faster when we don't have link.
700 // This was needed to speed up the AP bootstrap time.
701 if(sp->link == 0) {
702 mod_timer(&sp->link_timer, jiffies + HZ/2);
703 } else {
704 mod_timer(&sp->link_timer, jiffies + LINK_TIMER);
705 }
706 }
707
708 static void ar2313_check_link(struct net_device *dev)
709 {
710 struct ar2313_private *sp = dev->priv;
711 u16 phyData;
712
713 phyData = armiiread(sp->phy, MII_BMSR);
714 if (sp->phyData != phyData) {
715 if (phyData & BMSR_LSTATUS) {
716 /* link is present, ready link partner ability to deterine duplexity */
717 int duplex = 0;
718 u16 reg;
719
720 sp->link = 1;
721 reg = armiiread(sp->phy, MII_BMCR);
722 if (reg & BMCR_ANENABLE) {
723 /* auto neg enabled */
724 reg = armiiread(sp->phy, MII_LPA);
725 duplex = (reg & (LPA_100FULL|LPA_10FULL))? 1:0;
726 } else {
727 /* no auto neg, just read duplex config */
728 duplex = (reg & BMCR_FULLDPLX)? 1:0;
729 }
730
731 printk(KERN_INFO "%s: Configuring MAC for %s duplex\n", dev->name,
732 (duplex)? "full":"half");
733
734 if (duplex) {
735 /* full duplex */
736 sp->eth_regs->mac_control = ((sp->eth_regs->mac_control | MAC_CONTROL_F) &
737 ~MAC_CONTROL_DRO);
738 } else {
739 /* half duplex */
740 sp->eth_regs->mac_control = ((sp->eth_regs->mac_control | MAC_CONTROL_DRO) &
741 ~MAC_CONTROL_F);
742 }
743 } else {
744 /* no link */
745 sp->link = 0;
746 }
747 sp->phyData = phyData;
748 }
749 }
750
751 static int
752 ar2313_reset_reg(struct net_device *dev)
753 {
754 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
755 unsigned int ethsal, ethsah;
756 unsigned int flags;
757
758 *sp->int_regs |= ar_int_mac_mask;
759 mdelay(10);
760 *sp->int_regs &= ~ar_int_mac_mask;
761 mdelay(10);
762 *sp->int_regs |= ar_int_phy_mask;
763 mdelay(10);
764 *sp->int_regs &= ~ar_int_phy_mask;
765 mdelay(10);
766
767 sp->dma_regs->bus_mode = (DMA_BUS_MODE_SWR);
768 mdelay(10);
769 sp->dma_regs->bus_mode = ((32 << DMA_BUS_MODE_PBL_SHIFT) | DMA_BUS_MODE_BLE);
770
771 /* enable interrupts */
772 sp->dma_regs->intr_ena = (DMA_STATUS_AIS |
773 DMA_STATUS_NIS |
774 DMA_STATUS_RI |
775 DMA_STATUS_TI |
776 DMA_STATUS_FBE);
777 sp->dma_regs->xmt_base = K1_TO_PHYS(sp->tx_ring);
778 sp->dma_regs->rcv_base = K1_TO_PHYS(sp->rx_ring);
779 sp->dma_regs->control = (DMA_CONTROL_SR | DMA_CONTROL_ST | DMA_CONTROL_SF);
780
781 sp->eth_regs->flow_control = (FLOW_CONTROL_FCE);
782 sp->eth_regs->vlan_tag = (0x8100);
783
784 /* Enable Ethernet Interface */
785 flags = (MAC_CONTROL_TE | /* transmit enable */
786 MAC_CONTROL_PM | /* pass mcast */
787 MAC_CONTROL_F | /* full duplex */
788 MAC_CONTROL_HBD); /* heart beat disabled */
789
790 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
791 flags |= MAC_CONTROL_PR;
792 }
793 sp->eth_regs->mac_control = flags;
794
795 /* Set all Ethernet station address registers to their initial values */
796 ethsah = ((((u_int)(dev->dev_addr[5]) << 8) & (u_int)0x0000FF00) |
797 (((u_int)(dev->dev_addr[4]) << 0) & (u_int)0x000000FF));
798
799 ethsal = ((((u_int)(dev->dev_addr[3]) << 24) & (u_int)0xFF000000) |
800 (((u_int)(dev->dev_addr[2]) << 16) & (u_int)0x00FF0000) |
801 (((u_int)(dev->dev_addr[1]) << 8) & (u_int)0x0000FF00) |
802 (((u_int)(dev->dev_addr[0]) << 0) & (u_int)0x000000FF) );
803
804 sp->eth_regs->mac_addr[0] = ethsah;
805 sp->eth_regs->mac_addr[1] = ethsal;
806
807 mdelay(10);
808
809 return(0);
810 }
811
812
813 static int ar2313_init(struct net_device *dev)
814 {
815 struct ar2313_private *sp = dev->priv;
816 int ecode=0;
817
818 /*
819 * Allocate descriptors
820 */
821 if (ar2313_allocate_descriptors(dev)) {
822 printk("%s: %s: ar2313_allocate_descriptors failed\n",
823 dev->name, __FUNCTION__);
824 ecode = -EAGAIN;
825 goto init_error;
826 }
827
828 /*
829 * Get the memory for the skb rings.
830 */
831 if(sp->rx_skb == NULL) {
832 sp->rx_skb = kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES, GFP_KERNEL);
833 if (!(sp->rx_skb)) {
834 printk("%s: %s: rx_skb kmalloc failed\n",
835 dev->name, __FUNCTION__);
836 ecode = -EAGAIN;
837 goto init_error;
838 }
839 }
840 memset(sp->rx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
841
842 if(sp->tx_skb == NULL) {
843 sp->tx_skb = kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES, GFP_KERNEL);
844 if (!(sp->tx_skb)) {
845 printk("%s: %s: tx_skb kmalloc failed\n",
846 dev->name, __FUNCTION__);
847 ecode = -EAGAIN;
848 goto init_error;
849 }
850 }
851 memset(sp->tx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
852
853 /*
854 * Set tx_csm before we start receiving interrupts, otherwise
855 * the interrupt handler might think it is supposed to process
856 * tx ints before we are up and running, which may cause a null
857 * pointer access in the int handler.
858 */
859 sp->rx_skbprd = 0;
860 sp->cur_rx = 0;
861 sp->tx_prd = 0;
862 sp->tx_csm = 0;
863
864 /*
865 * Zero the stats before starting the interface
866 */
867 memset(&sp->stats, 0, sizeof(sp->stats));
868
869 /*
870 * We load the ring here as there seem to be no way to tell the
871 * firmware to wipe the ring without re-initializing it.
872 */
873 ar2313_load_rx_ring(dev, RX_RING_SIZE);
874
875 /*
876 * Init hardware
877 */
878 ar2313_reset_reg(dev);
879
880 /*
881 * Get the IRQ
882 */
883 ecode = request_irq(dev->irq, &ar2313_interrupt, SA_SHIRQ | SA_INTERRUPT, dev->name, dev);
884 if (ecode) {
885 printk(KERN_WARNING "%s: %s: Requested IRQ %d is busy\n",
886 dev->name, __FUNCTION__, dev->irq);
887 goto init_error;
888 }
889
890 #if 0
891 // commented out, for now
892
893 if(mips_machtype == MACH_ARUBA_SAMSUNG) {
894 int i;
895 /* configure Marvell 88E6060 */
896 /* reset chip */
897 armiiwrite(0x1f, 0xa, 0xa130);
898 do {
899 udelay(1000);
900 i = armiiread(sp->phy, 0xa);
901 } while (i & 0x8000);
902
903 /* configure MAC address */
904 armiiwrite(sp->phy, 0x1, dev->dev_addr[0] << 8 | dev->dev_addr[1]);
905 armiiwrite(sp->phy, 0x2, dev->dev_addr[2] << 8 | dev->dev_addr[3]);
906 armiiwrite(sp->phy, 0x3, dev->dev_addr[4] << 8 | dev->dev_addr[5]);
907
908 /* set ports to forwarding */
909 armiiwrite(0x18, 0x4, 0x3);
910 armiiwrite(0x1c, 0x4, 0x3);
911 armiiwrite(0x1d, 0x4, 0x3);
912 }
913 #endif
914
915 tasklet_enable(&sp->rx_tasklet);
916
917 return 0;
918
919 init_error:
920 ar2313_init_cleanup(dev);
921 return ecode;
922 }
923
924 /*
925 * Load the rx ring.
926 *
927 * Loading rings is safe without holding the spin lock since this is
928 * done only before the device is enabled, thus no interrupts are
929 * generated and by the interrupt handler/tasklet handler.
930 */
931 static void ar2313_load_rx_ring(struct net_device *dev, int nr_bufs)
932 {
933
934 struct ar2313_private *sp = ((struct net_device *)dev)->priv;
935 short i, idx;
936
937 idx = sp->rx_skbprd;
938
939 for (i = 0; i < nr_bufs; i++) {
940 struct sk_buff *skb;
941 ar2313_descr_t *rd;
942
943 if (sp->rx_skb[idx]) {
944 #if DEBUG_RX
945 printk(KERN_INFO "ar2313 rx refill full\n");
946 #endif /* DEBUG */
947 break;
948 }
949
950 // partha: create additional room for the second GRE fragment
951 skb = alloc_skb(AR2313_BUFSIZE+128, GFP_ATOMIC);
952 if (!skb) {
953 printk("\n\n\n\n %s: No memory in system\n\n\n\n", __FUNCTION__);
954 break;
955 }
956 // partha: create additional room in the front for tx pkt capture
957 skb_reserve(skb, 32);
958
959 /*
960 * Make sure IP header starts on a fresh cache line.
961 */
962 skb->dev = dev;
963 skb_reserve(skb, RX_OFFSET);
964 sp->rx_skb[idx] = skb;
965
966 rd = (ar2313_descr_t *) &sp->rx_ring[idx];
967
968 /* initialize dma descriptor */
969 rd->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
970 DMA_RX1_CHAINED);
971 rd->addr = virt_to_phys(skb->data);
972 rd->descr = virt_to_phys(&sp->rx_ring[(idx+1) & (AR2313_DESCR_ENTRIES-1)]);
973 rd->status = DMA_RX_OWN;
974
975 idx = DSC_NEXT(idx);
976 }
977
978 if (!i) {
979 #if DEBUG_ERR
980 printk(KERN_INFO "Out of memory when allocating standard receive buffers\n");
981 #endif /* DEBUG */
982 } else {
983 sp->rx_skbprd = idx;
984 }
985
986 return;
987 }
988
989 #define AR2313_MAX_PKTS_PER_CALL 64
990
991 static int ar2313_rx_int(struct net_device *dev)
992 {
993 struct ar2313_private *sp = dev->priv;
994 struct sk_buff *skb, *skb_new;
995 ar2313_descr_t *rxdesc;
996 unsigned int status;
997 u32 idx;
998 int pkts = 0;
999 int rval;
1000
1001 idx = sp->cur_rx;
1002
1003 /* process at most the entire ring and then wait for another interrupt */
1004 while(1) {
1005
1006 rxdesc = &sp->rx_ring[idx];
1007 status = rxdesc->status;
1008 if (status & DMA_RX_OWN) {
1009 /* SiByte owns descriptor or descr not yet filled in */
1010 rval = 0;
1011 break;
1012 }
1013
1014 if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
1015 rval = 1;
1016 break;
1017 }
1018
1019 #if DEBUG_RX
1020 printk("index %d\n", idx);
1021 printk("RX status %08x\n", rxdesc->status);
1022 printk("RX devcs %08x\n", rxdesc->devcs );
1023 printk("RX addr %08x\n", rxdesc->addr );
1024 printk("RX descr %08x\n", rxdesc->descr );
1025 #endif
1026
1027 if ((status & (DMA_RX_ERROR|DMA_RX_ERR_LENGTH)) &&
1028 (!(status & DMA_RX_LONG))){
1029 #if DEBUG_RX
1030 printk("%s: rx ERROR %08x\n", __FUNCTION__, status);
1031 #endif
1032 sp->stats.rx_errors++;
1033 sp->stats.rx_dropped++;
1034
1035 /* add statistics counters */
1036 if (status & DMA_RX_ERR_CRC) sp->stats.rx_crc_errors++;
1037 if (status & DMA_RX_ERR_COL) sp->stats.rx_over_errors++;
1038 if (status & DMA_RX_ERR_LENGTH)
1039 sp->stats.rx_length_errors++;
1040 if (status & DMA_RX_ERR_RUNT) sp->stats.rx_over_errors++;
1041 if (status & DMA_RX_ERR_DESC) sp->stats.rx_over_errors++;
1042
1043 } else {
1044 /* alloc new buffer. */
1045 skb_new = dev_alloc_skb(AR2313_BUFSIZE + RX_OFFSET + 128);
1046 if (skb_new != NULL) {
1047
1048 skb = sp->rx_skb[idx];
1049 /* set skb */
1050 skb_put(skb, ((status >> DMA_RX_LEN_SHIFT) & 0x3fff) - CRC_LEN);
1051
1052 #ifdef CONFIG_MERLOT
1053 if ((dev->am_pkt_handler == NULL) ||
1054 (dev->am_pkt_handler(skb, dev) == 0)) {
1055 #endif
1056 sp->stats.rx_bytes += skb->len;
1057 skb->protocol = eth_type_trans(skb, dev);
1058 /* pass the packet to upper layers */
1059
1060 #ifdef CONFIG_MERLOT
1061 if (dev->asap_netif_rx)
1062 dev->asap_netif_rx(skb);
1063 else
1064 #endif
1065 netif_rx(skb);
1066 #ifdef CONFIG_MERLOT
1067 }
1068 #endif
1069 skb_new->dev = dev;
1070 /* 16 bit align */
1071 skb_reserve(skb_new, RX_OFFSET+32);
1072 /* reset descriptor's curr_addr */
1073 rxdesc->addr = virt_to_phys(skb_new->data);
1074
1075 sp->stats.rx_packets++;
1076 sp->rx_skb[idx] = skb_new;
1077
1078 } else {
1079 sp->stats.rx_dropped++;
1080 }
1081 }
1082
1083 rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
1084 DMA_RX1_CHAINED);
1085 rxdesc->status = DMA_RX_OWN;
1086
1087 idx = DSC_NEXT(idx);
1088 }
1089
1090 sp->cur_rx = idx;
1091
1092 return rval;
1093 }
1094
1095
1096 static void ar2313_tx_int(struct net_device *dev)
1097 {
1098 struct ar2313_private *sp = dev->priv;
1099 u32 idx;
1100 struct sk_buff *skb;
1101 ar2313_descr_t *txdesc;
1102 unsigned int status=0;
1103
1104 idx = sp->tx_csm;
1105
1106 while (idx != sp->tx_prd) {
1107
1108 txdesc = &sp->tx_ring[idx];
1109
1110 #if DEBUG_TX
1111 printk("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
1112 dev->name, sp->tx_csm, idx, sp->tx_prd,
1113 txdesc->status, txdesc->devcs, txdesc->addr, txdesc->descr);
1114 #endif /* DEBUG */
1115
1116 if ((status = txdesc->status) & DMA_TX_OWN) {
1117 /* ar2313 dma still owns descr */
1118 break;
1119 }
1120 /* done with this descriptor */
1121 txdesc->status = 0;
1122
1123 if (status & DMA_TX_ERROR){
1124 sp->stats.tx_errors++;
1125 sp->stats.tx_dropped++;
1126 if(status & DMA_TX_ERR_UNDER)
1127 sp->stats.tx_fifo_errors++;
1128 if(status & DMA_TX_ERR_HB)
1129 sp->stats.tx_heartbeat_errors++;
1130 if(status & (DMA_TX_ERR_LOSS |
1131 DMA_TX_ERR_LINK))
1132 sp->stats.tx_carrier_errors++;
1133 if (status & (DMA_TX_ERR_LATE|
1134 DMA_TX_ERR_COL |
1135 DMA_TX_ERR_JABBER |
1136 DMA_TX_ERR_DEFER))
1137 sp->stats.tx_aborted_errors++;
1138 } else {
1139 /* transmit OK */
1140 sp->stats.tx_packets++;
1141 }
1142
1143 skb = sp->tx_skb[idx];
1144 sp->tx_skb[idx] = NULL;
1145 idx = DSC_NEXT(idx);
1146 sp->stats.tx_bytes += skb->len;
1147 dev_kfree_skb_irq(skb);
1148 }
1149
1150 sp->tx_csm = idx;
1151
1152 return;
1153 }
1154
1155
1156 static void
1157 rx_tasklet_func(unsigned long data)
1158 {
1159 struct net_device *dev = (struct net_device *) data;
1160 struct ar2313_private *sp = dev->priv;
1161
1162 if (sp->unloading) {
1163 return;
1164 }
1165
1166 if (ar2313_rx_int(dev)) {
1167 tasklet_hi_schedule(&sp->rx_tasklet);
1168 }
1169 else {
1170 unsigned long flags;
1171 spin_lock_irqsave(&sp->lock, flags);
1172 sp->dma_regs->intr_ena |= DMA_STATUS_RI;
1173 spin_unlock_irqrestore(&sp->lock, flags);
1174 }
1175 }
1176
1177 static void
1178 rx_schedule(struct net_device *dev)
1179 {
1180 struct ar2313_private *sp = dev->priv;
1181
1182 sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
1183
1184 tasklet_hi_schedule(&sp->rx_tasklet);
1185 }
1186
1187 static irqreturn_t ar2313_interrupt(int irq, void *dev_id)
1188 {
1189 struct net_device *dev = (struct net_device *)dev_id;
1190 struct ar2313_private *sp = dev->priv;
1191 unsigned int status, enabled;
1192
1193 /* clear interrupt */
1194 /*
1195 * Don't clear RI bit if currently disabled.
1196 */
1197 status = sp->dma_regs->status;
1198 enabled = sp->dma_regs->intr_ena;
1199 sp->dma_regs->status = status & enabled;
1200
1201 if (status & DMA_STATUS_NIS) {
1202 /* normal status */
1203 /*
1204 * Don't schedule rx processing if interrupt
1205 * is already disabled.
1206 */
1207 if (status & enabled & DMA_STATUS_RI) {
1208 /* receive interrupt */
1209 rx_schedule(dev);
1210 }
1211 if (status & DMA_STATUS_TI) {
1212 /* transmit interrupt */
1213 ar2313_tx_int(dev);
1214 }
1215 }
1216
1217 if (status & DMA_STATUS_AIS) {
1218 #if DEBUG_INT
1219 printk("%s: AIS set %08x & %x\n", __FUNCTION__,
1220 status, (DMA_STATUS_FBE | DMA_STATUS_TPS));
1221 #endif
1222 /* abnormal status */
1223 if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS)) {
1224 ar2313_restart(dev);
1225 }
1226 }
1227 return IRQ_HANDLED;
1228 }
1229
1230
1231 static int ar2313_open(struct net_device *dev)
1232 {
1233 struct ar2313_private *sp;
1234
1235 sp = dev->priv;
1236
1237 dev->mtu = 1500;
1238 netif_start_queue(dev);
1239
1240 sp->eth_regs->mac_control |= MAC_CONTROL_RE;
1241
1242 AR2313_MOD_INC_USE_COUNT;
1243
1244 return 0;
1245 }
1246
1247 static void ar2313_halt(struct net_device *dev)
1248 {
1249 struct ar2313_private *sp = dev->priv;
1250 int j;
1251
1252 tasklet_disable(&sp->rx_tasklet);
1253
1254 /* kill the MAC */
1255 sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
1256 MAC_CONTROL_TE); /* disable Transmits */
1257 /* stop dma */
1258 sp->dma_regs->control = 0;
1259 sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
1260
1261 /* place phy and MAC in reset */
1262 *sp->int_regs |= (ar_int_mac_mask | ar_int_phy_mask);
1263
1264 /* free buffers on tx ring */
1265 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
1266 struct sk_buff *skb;
1267 ar2313_descr_t *txdesc;
1268
1269 txdesc = &sp->tx_ring[j];
1270 txdesc->descr = 0;
1271
1272 skb = sp->tx_skb[j];
1273 if (skb) {
1274 dev_kfree_skb(skb);
1275 sp->tx_skb[j] = NULL;
1276 }
1277 }
1278 }
1279
1280 /*
1281 * close should do nothing. Here's why. It's called when
1282 * 'ifconfig bond0 down' is run. If it calls free_irq then
1283 * the irq is gone forever ! When bond0 is made 'up' again,
1284 * the ar2313_open () does not call request_irq (). Worse,
1285 * the call to ar2313_halt() generates a WDOG reset due to
1286 * the write to 'sp->int_regs' and the box reboots.
1287 * Commenting this out is good since it allows the
1288 * system to resume when bond0 is made up again.
1289 */
1290 static int ar2313_close(struct net_device *dev)
1291 {
1292 #if 0
1293 /*
1294 * Disable interrupts
1295 */
1296 disable_irq(dev->irq);
1297
1298 /*
1299 * Without (or before) releasing irq and stopping hardware, this
1300 * is an absolute non-sense, by the way. It will be reset instantly
1301 * by the first irq.
1302 */
1303 netif_stop_queue(dev);
1304
1305 /* stop the MAC and DMA engines */
1306 ar2313_halt(dev);
1307
1308 /* release the interrupt */
1309 free_irq(dev->irq, dev);
1310
1311 #endif
1312 AR2313_MOD_DEC_USE_COUNT;
1313 return 0;
1314 }
1315
1316 static int ar2313_start_xmit(struct sk_buff *skb, struct net_device *dev)
1317 {
1318 struct ar2313_private *sp = dev->priv;
1319 ar2313_descr_t *td;
1320 u32 idx;
1321
1322 idx = sp->tx_prd;
1323 td = &sp->tx_ring[idx];
1324
1325 if (td->status & DMA_TX_OWN) {
1326 #if DEBUG_TX
1327 printk("%s: No space left to Tx\n", __FUNCTION__);
1328 #endif
1329 /* free skbuf and lie to the caller that we sent it out */
1330 sp->stats.tx_dropped++;
1331 dev_kfree_skb(skb);
1332
1333 /* restart transmitter in case locked */
1334 sp->dma_regs->xmt_poll = 0;
1335 return 0;
1336 }
1337
1338 /* Setup the transmit descriptor. */
1339 td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
1340 (DMA_TX1_LS|DMA_TX1_IC|DMA_TX1_CHAINED));
1341 td->addr = virt_to_phys(skb->data);
1342 td->status = DMA_TX_OWN;
1343
1344 /* kick transmitter last */
1345 sp->dma_regs->xmt_poll = 0;
1346
1347 #if DEBUG_TX
1348 printk("index %d\n", idx);
1349 printk("TX status %08x\n", td->status);
1350 printk("TX devcs %08x\n", td->devcs );
1351 printk("TX addr %08x\n", td->addr );
1352 printk("TX descr %08x\n", td->descr );
1353 #endif
1354
1355 sp->tx_skb[idx] = skb;
1356 idx = DSC_NEXT(idx);
1357 sp->tx_prd = idx;
1358
1359 //dev->trans_start = jiffies;
1360
1361 return 0;
1362 }
1363
1364 static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1365 {
1366 struct ar2313_private *np = dev->priv;
1367 u32 tmp;
1368
1369 ecmd->supported =
1370 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1371 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1372 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
1373
1374 ecmd->port = PORT_TP;
1375 /* only supports internal transceiver */
1376 ecmd->transceiver = XCVR_INTERNAL;
1377 /* not sure what this is for */
1378 ecmd->phy_address = 1;
1379
1380 ecmd->advertising = ADVERTISED_MII;
1381 tmp = armiiread(np->phy, MII_ADVERTISE);
1382 if (tmp & ADVERTISE_10HALF)
1383 ecmd->advertising |= ADVERTISED_10baseT_Half;
1384 if (tmp & ADVERTISE_10FULL)
1385 ecmd->advertising |= ADVERTISED_10baseT_Full;
1386 if (tmp & ADVERTISE_100HALF)
1387 ecmd->advertising |= ADVERTISED_100baseT_Half;
1388 if (tmp & ADVERTISE_100FULL)
1389 ecmd->advertising |= ADVERTISED_100baseT_Full;
1390
1391 tmp = armiiread(np->phy, MII_BMCR);
1392 if (tmp & BMCR_ANENABLE) {
1393 ecmd->advertising |= ADVERTISED_Autoneg;
1394 ecmd->autoneg = AUTONEG_ENABLE;
1395 } else {
1396 ecmd->autoneg = AUTONEG_DISABLE;
1397 }
1398
1399 if (ecmd->autoneg == AUTONEG_ENABLE) {
1400 tmp = armiiread(np->phy, MII_LPA);
1401 if (tmp & (LPA_100FULL|LPA_10FULL)) {
1402 ecmd->duplex = DUPLEX_FULL;
1403 } else {
1404 ecmd->duplex = DUPLEX_HALF;
1405 }
1406 if (tmp & (LPA_100FULL|LPA_100HALF)) {
1407 ecmd->speed = SPEED_100;
1408 } else {
1409 ecmd->speed = SPEED_10;
1410 }
1411 } else {
1412 if (tmp & BMCR_FULLDPLX) {
1413 ecmd->duplex = DUPLEX_FULL;
1414 } else {
1415 ecmd->duplex = DUPLEX_HALF;
1416 }
1417 if (tmp & BMCR_SPEED100) {
1418 ecmd->speed = SPEED_100;
1419 } else {
1420 ecmd->speed = SPEED_10;
1421 }
1422 }
1423
1424 /* ignore maxtxpkt, maxrxpkt for now */
1425
1426 return 0;
1427 }
1428
1429 static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1430 {
1431 struct ar2313_private *np = dev->priv;
1432 u32 tmp;
1433
1434 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
1435 return -EINVAL;
1436 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1437 return -EINVAL;
1438 if (ecmd->port != PORT_TP)
1439 return -EINVAL;
1440 if (ecmd->transceiver != XCVR_INTERNAL)
1441 return -EINVAL;
1442 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
1443 return -EINVAL;
1444 /* ignore phy_address, maxtxpkt, maxrxpkt for now */
1445
1446 /* WHEW! now lets bang some bits */
1447
1448 tmp = armiiread(np->phy, MII_BMCR);
1449 if (ecmd->autoneg == AUTONEG_ENABLE) {
1450 /* turn on autonegotiation */
1451 tmp |= BMCR_ANENABLE;
1452 printk("%s: Enabling auto-neg\n", dev->name);
1453 } else {
1454 /* turn off auto negotiation, set speed and duplexity */
1455 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
1456 if (ecmd->speed == SPEED_100)
1457 tmp |= BMCR_SPEED100;
1458 if (ecmd->duplex == DUPLEX_FULL)
1459 tmp |= BMCR_FULLDPLX;
1460 printk("%s: Hard coding %d/%s\n", dev->name,
1461 (ecmd->speed == SPEED_100)? 100:10,
1462 (ecmd->duplex == DUPLEX_FULL)? "full":"half");
1463 }
1464 armiiwrite(np->phy, MII_BMCR, tmp);
1465 np->phyData = 0;
1466 return 0;
1467 }
1468
1469 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1470 {
1471 struct ar2313_private *np = dev->priv;
1472 u32 cmd;
1473
1474 if (get_user(cmd, (u32 *)useraddr))
1475 return -EFAULT;
1476
1477 switch (cmd) {
1478 /* get settings */
1479 case ETHTOOL_GSET: {
1480 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1481 spin_lock_irq(&np->lock);
1482 netdev_get_ecmd(dev, &ecmd);
1483 spin_unlock_irq(&np->lock);
1484 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1485 return -EFAULT;
1486 return 0;
1487 }
1488 /* set settings */
1489 case ETHTOOL_SSET: {
1490 struct ethtool_cmd ecmd;
1491 int r;
1492 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1493 return -EFAULT;
1494 spin_lock_irq(&np->lock);
1495 r = netdev_set_ecmd(dev, &ecmd);
1496 spin_unlock_irq(&np->lock);
1497 return r;
1498 }
1499 /* restart autonegotiation */
1500 case ETHTOOL_NWAY_RST: {
1501 int tmp;
1502 int r = -EINVAL;
1503 /* if autoneg is off, it's an error */
1504 tmp = armiiread(np->phy, MII_BMCR);
1505 if (tmp & BMCR_ANENABLE) {
1506 tmp |= (BMCR_ANRESTART);
1507 armiiwrite(np->phy, MII_BMCR, tmp);
1508 r = 0;
1509 }
1510 return r;
1511 }
1512 /* get link status */
1513 case ETHTOOL_GLINK: {
1514 struct ethtool_value edata = {ETHTOOL_GLINK};
1515 edata.data = (armiiread(np->phy, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
1516 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1517 return -EFAULT;
1518 return 0;
1519 }
1520 }
1521
1522 return -EOPNOTSUPP;
1523 }
1524
1525 static int ar2313_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1526 {
1527 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&ifr->ifr_data;
1528
1529 switch (cmd) {
1530 case SIOCDEVPRIVATE: {
1531 struct ar2313_cmd scmd;
1532
1533 if (copy_from_user(&scmd, ifr->ifr_data, sizeof(scmd)))
1534 return -EFAULT;
1535
1536 #if DEBUG
1537 printk("%s: ioctl devprivate c=%d a=%x l=%d m=%d d=%x,%x\n",
1538 dev->name, scmd.cmd,
1539 scmd.address, scmd.length,
1540 scmd.mailbox, scmd.data[0], scmd.data[1]);
1541 #endif /* DEBUG */
1542
1543 switch (scmd.cmd) {
1544 case AR2313_READ_DATA:
1545 if(scmd.length==4){
1546 scmd.data[0] = *((u32*)scmd.address);
1547 } else if(scmd.length==2) {
1548 scmd.data[0] = *((u16*)scmd.address);
1549 } else if (scmd.length==1) {
1550 scmd.data[0] = *((u8*)scmd.address);
1551 } else {
1552 return -EOPNOTSUPP;
1553 }
1554 if(copy_to_user(ifr->ifr_data, &scmd, sizeof(scmd)))
1555 return -EFAULT;
1556 break;
1557
1558 case AR2313_WRITE_DATA:
1559 if(scmd.length==4){
1560 *((u32*)scmd.address) = scmd.data[0];
1561 } else if(scmd.length==2) {
1562 *((u16*)scmd.address) = scmd.data[0];
1563 } else if (scmd.length==1) {
1564 *((u8*)scmd.address) = scmd.data[0];
1565 } else {
1566 return -EOPNOTSUPP;
1567 }
1568 break;
1569
1570 case AR2313_GET_VERSION:
1571 // SAMEER: sprintf((char*) &scmd, "%s", ARUBA_VERSION);
1572 if(copy_to_user(ifr->ifr_data, &scmd, sizeof(scmd)))
1573 return -EFAULT;
1574 break;
1575
1576 default:
1577 return -EOPNOTSUPP;
1578 }
1579 return 0;
1580 }
1581
1582 case SIOCETHTOOL:
1583 return netdev_ethtool_ioctl(dev, (void *) ifr->ifr_data);
1584
1585 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1586 data->phy_id = 1;
1587 /* Fall Through */
1588
1589 case SIOCGMIIREG: /* Read MII PHY register. */
1590 case SIOCDEVPRIVATE+1: /* for binary compat, remove in 2.5 */
1591 data->val_out = armiiread(data->phy_id & 0x1f,
1592 data->reg_num & 0x1f);
1593 return 0;
1594 case SIOCSMIIREG: /* Write MII PHY register. */
1595 case SIOCDEVPRIVATE+2: /* for binary compat, remove in 2.5 */
1596 if (!capable(CAP_NET_ADMIN))
1597 return -EPERM;
1598 armiiwrite(data->phy_id & 0x1f,
1599 data->reg_num & 0x1f, data->val_in);
1600 return 0;
1601
1602 case SIOCSIFHWADDR:
1603 if (copy_from_user(dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
1604 return -EFAULT;
1605 return 0;
1606
1607 case SIOCGIFHWADDR:
1608 if (copy_to_user(ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
1609 return -EFAULT;
1610 return 0;
1611
1612 default:
1613 break;
1614 }
1615
1616 return -EOPNOTSUPP;
1617 }
1618
1619 static struct net_device_stats *ar2313_get_stats(struct net_device *dev)
1620 {
1621 struct ar2313_private *sp = dev->priv;
1622 return &sp->stats;
1623 }
1624
1625 static short
1626 armiiread(short phy, short reg)
1627 {
1628 volatile ETHERNET_STRUCT * ethernet;
1629
1630 ethernet = (volatile ETHERNET_STRUCT *)ETHERNET_BASE; /* always MAC 0 */
1631 ethernet->mii_addr = ((reg << MII_ADDR_REG_SHIFT) |
1632 (phy << MII_ADDR_PHY_SHIFT));
1633 while (ethernet->mii_addr & MII_ADDR_BUSY);
1634 return (ethernet->mii_data >> MII_DATA_SHIFT);
1635 }
1636
1637 static void
1638 armiiwrite(short phy, short reg, short data)
1639 {
1640 volatile ETHERNET_STRUCT * ethernet;
1641
1642 ethernet = (volatile ETHERNET_STRUCT *)ETHERNET_BASE; /* always MAC 0 */
1643 while (ethernet->mii_addr & MII_ADDR_BUSY);
1644 ethernet->mii_data = data << MII_DATA_SHIFT;
1645 ethernet->mii_addr = ((reg << MII_ADDR_REG_SHIFT) |
1646 (phy << MII_ADDR_PHY_SHIFT) |
1647 MII_ADDR_WRITE);
1648 }
1649
This page took 0.118737 seconds and 5 git commands to generate.