os/linux-2.6-tag--devboard-R2_10-4/drivers/net/cris/eth_v32.c

Go to the documentation of this file.
00001 /*
00002  * Driver for the ETRAX FS network controller.
00003  *
00004  * Copyright (c) 2003-2006 Axis Communications AB.
00005  */
00006 
00007 #include <linux/module.h>
00008 
00009 #include <linux/kernel.h>
00010 #include <linux/sched.h>
00011 #include <linux/delay.h>
00012 #include <linux/types.h>
00013 #include <linux/fcntl.h>
00014 #include <linux/interrupt.h>
00015 #include <linux/ptrace.h>
00016 #include <linux/ioport.h>
00017 #include <linux/in.h>
00018 #include <linux/slab.h>
00019 #include <linux/string.h>
00020 #include <linux/spinlock.h>
00021 #include <linux/errno.h>
00022 #include <linux/init.h>
00023 #include <linux/cpufreq.h>
00024 
00025 #include <linux/netdevice.h>
00026 #include <linux/etherdevice.h>
00027 #include <linux/skbuff.h>
00028 #include <linux/ethtool.h>
00029 #include <linux/mii.h>
00030 
00031 #include <asm/io.h>             /* LED_* I/O functions */
00032 #include <asm/irq.h>
00033 #include <asm/arch/hwregs/reg_map.h>
00034 #include <asm/arch/hwregs/reg_rdwr.h>
00035 #include <asm/arch/hwregs/dma.h>
00036 #include <asm/arch/hwregs/eth_defs.h>
00037 #ifdef CONFIG_ETRAXFS
00038 #include <asm/arch/hwregs/config_defs.h>
00039 #else
00040 #include <asm/arch/hwregs/clkgen_defs.h>
00041 #endif
00042 #include <asm/arch/hwregs/intr_vect_defs.h>
00043 #include <asm/system.h>
00044 #include <asm/bitops.h>
00045 #include <asm/ethernet.h>
00046 #include <asm/arch/dma.h>
00047 #include <asm/arch/intmem.h>
00048 #include <asm/arch/pinmux.h>
00049 
00050 #include "eth_v32.h"
00051 
00052 #ifndef CONFIG_ETRAXFS
00053 #define ETH0_INTR_VECT ETH_INTR_VECT
00054 #define ETH1_INTR_VECT ETH_INTR_VECT
00055 #define regi_eth0 regi_eth
00056 #define regi_eth1 regi_
00057 #endif
00058 
00059 #define DEBUG(x)
00060 #define GET_BIT(bit,val)   (((val) >> (bit)) & 0x01)
00061 
00062 /* Toggle network LEDs on/off at runtime */
00063 static int use_network_leds = 1;
00064 
00065 static void update_rx_stats(struct crisv32_ethernet_local *np);
00066 static void update_tx_stats(struct crisv32_ethernet_local *np); 
00067 static int crisv32_eth_poll(struct net_device *dev, int *budget);
00068 static void crisv32_eth_setup_controller(struct net_device *dev);
00069 static int  crisv32_eth_request_irqdma(struct net_device *dev);
00070 static void crisv32_ethernet_bug(struct net_device *dev);
00071 
00072 /*
00073  * The name of the card. Is used for messages and in the requests for
00074  * io regions, irqs and dma channels.
00075  */
00076 static const char *cardname = "ETRAX FS built-in ethernet controller";
00077 
00078 static int autoneg_normal = 1;
00079 
00080 /* Some chipset needs special care. */
00081 struct transceiver_ops transceivers[] = {
00082         {0x1018, broadcom_check_speed, broadcom_check_duplex},
00083         /* TDK 2120 and TDK 2120C */
00084         {0xC039, tdk_check_speed, tdk_check_duplex}, 
00085         {0x039C, tdk_check_speed, tdk_check_duplex},    
00086         /* Intel LXT972A*/
00087         {0x04de, intel_check_speed, intel_check_duplex},
00088         /* National Semiconductor DP83865 */
00089         {0x0017, national_check_speed, national_check_duplex},
00090         /* Vitesse VCS8641 */
00091         {0x01c1, vitesse_check_speed, vitesse_check_duplex},
00092         /* Generic, must be last. */
00093         {0x0000, generic_check_speed, generic_check_duplex}
00094 };
00095 
00096 static struct net_device *crisv32_dev[2];
00097 static struct crisv32_eth_leds *crisv32_leds[3];
00098 
00099 /* Default MAC address for interface 0.
00100  * The real one will be set later. */
00101 static struct sockaddr default_mac_iface0 = 
00102   {0, {0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00}};
00103 
00104 #ifdef CONFIG_CPU_FREQ
00105 static int
00106 crisv32_ethernet_freq_notifier(struct notifier_block *nb, unsigned long val,
00107                                void *data);
00108 
00109 static struct notifier_block crisv32_ethernet_freq_notifier_block = {
00110         .notifier_call  = crisv32_ethernet_freq_notifier
00111 };
00112 #endif
00113 
00114 /*
00115  * mask in and out tx/rx interrupts.
00116  */
00117 static inline void crisv32_disable_tx_ints(struct crisv32_ethernet_local *np)
00118 {
00119         reg_dma_rw_intr_mask intr_mask_tx = { .data = regk_dma_no };
00120         REG_WR(dma, np->dma_out_inst, rw_intr_mask, intr_mask_tx);
00121 }
00122 
00123 static inline void crisv32_enable_tx_ints(struct crisv32_ethernet_local *np)
00124 {
00125         reg_dma_rw_intr_mask intr_mask_tx = { .data = regk_dma_yes };
00126         REG_WR(dma, np->dma_out_inst, rw_intr_mask, intr_mask_tx);
00127 }
00128 
00129 static inline void crisv32_disable_rx_ints(struct crisv32_ethernet_local *np)
00130 {
00131         reg_dma_rw_intr_mask intr_mask_rx = { .in_eop = regk_dma_no };
00132         REG_WR(dma, np->dma_in_inst, rw_intr_mask, intr_mask_rx);
00133 }
00134 
00135 static inline void crisv32_enable_rx_ints(struct crisv32_ethernet_local *np)
00136 {
00137         reg_dma_rw_intr_mask intr_mask_rx = { .in_eop = regk_dma_yes };
00138         REG_WR(dma, np->dma_in_inst, rw_intr_mask, intr_mask_rx);
00139 }
00140 
00141 /* start/stop receiver */
00142 static inline void crisv32_start_receiver(struct crisv32_ethernet_local *np)
00143 {
00144         reg_eth_rw_rec_ctrl rec_ctrl;
00145         
00146         rec_ctrl = REG_RD(eth, np->eth_inst, rw_rec_ctrl);
00147         rec_ctrl.ma0 = regk_eth_yes;
00148         rec_ctrl.broadcast = regk_eth_rec;
00149         REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
00150 }
00151 
00152 static inline void crisv32_stop_receiver(struct crisv32_ethernet_local *np)
00153 {
00154         reg_eth_rw_rec_ctrl rec_ctrl;
00155 
00156         rec_ctrl = REG_RD(eth, np->eth_inst, rw_rec_ctrl);
00157         rec_ctrl.ma0 = regk_eth_no;
00158         rec_ctrl.broadcast = regk_eth_discard;
00159         REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
00160 }
00161 
00162 static int __init
00163 crisv32_eth_request_irqdma(struct net_device *dev)
00164 {
00165         struct crisv32_ethernet_local *np = netdev_priv(dev);
00166         
00167         /* Allocate IRQs and DMAs. */
00168         if (np->eth_inst == regi_eth0) {
00169                 if (request_irq(DMA0_INTR_VECT, crisv32tx_eth_interrupt, 
00170                                 0, cardname, dev)) {
00171                         return -EAGAIN;
00172                 }
00173 
00174                 if (request_irq(DMA1_INTR_VECT, crisv32rx_eth_interrupt,
00175                                 IRQF_SAMPLE_RANDOM, cardname, dev)) {
00176                         goto err0_1; 
00177                 }
00178                 
00179                 if (crisv32_request_dma(0, cardname, DMA_VERBOSE_ON_ERROR,
00180                                         12500000, dma_eth0))
00181                         goto err0_2;
00182                 
00183                 if (crisv32_request_dma(1, cardname, DMA_VERBOSE_ON_ERROR,
00184                                         12500000, dma_eth0))
00185                         goto err0_3;
00186                 
00187                 if (request_irq(ETH0_INTR_VECT, crisv32nw_eth_interrupt, 0,
00188                                 cardname, dev)) {
00189                         crisv32_free_dma(1);
00190                   err0_3:
00191                         crisv32_free_dma(0);
00192                   err0_2:
00193                         free_irq(DMA1_INTR_VECT, dev);
00194                   err0_1:
00195                         free_irq(DMA0_INTR_VECT, dev);
00196                         return -EAGAIN;
00197                 }
00198         } else {
00199                 if (request_irq(DMA6_INTR_VECT, crisv32tx_eth_interrupt,
00200                                 0, cardname, dev))
00201                         return -EAGAIN;
00202                 
00203                 if (request_irq(DMA7_INTR_VECT, crisv32rx_eth_interrupt,
00204                                 IRQF_SAMPLE_RANDOM, cardname, dev))
00205                         goto err1_1;
00206 
00207                 if (crisv32_request_dma(6, cardname, DMA_VERBOSE_ON_ERROR,
00208                                         0, dma_eth1))
00209                         goto err1_2;
00210 
00211                 if (crisv32_request_dma(7, cardname, DMA_VERBOSE_ON_ERROR,
00212                                         0, dma_eth1))
00213                         goto err1_3;
00214 
00215                 if (request_irq(ETH1_INTR_VECT, crisv32nw_eth_interrupt, 0,
00216                                 cardname, dev)) {
00217                         crisv32_free_dma(7);
00218                   err1_3:
00219                         crisv32_free_dma(6);
00220                   err1_2:
00221                         free_irq(DMA7_INTR_VECT, dev);
00222                   err1_1:
00223                         free_irq(DMA6_INTR_VECT, dev);
00224                         return -EAGAIN;
00225                 }
00226         }
00227         return 0;
00228 }
00229 
00230 static void __init
00231 crisv32_eth_setup_controller(struct net_device *dev)
00232 {
00233         struct crisv32_ethernet_local *np = netdev_priv(dev);
00234         
00235         reg_eth_rw_tr_ctrl tr_ctrl = {
00236                 .retry = regk_eth_yes,
00237                 .pad = regk_eth_yes,
00238                 .crc = regk_eth_yes
00239         };
00240         
00241         reg_eth_rw_rec_ctrl rec_ctrl = {
00242                 .ma0 = regk_eth_no,       /* enable at open() */
00243                 .broadcast = regk_eth_no,
00244                 .max_size = regk_eth_size1522
00245         };
00246                 
00247         reg_eth_rw_ga_lo ga_lo = { 0 };
00248         reg_eth_rw_ga_hi ga_hi = { 0 };
00249 
00250         reg_eth_rw_gen_ctrl gen_ctrl = {
00251           .phy = regk_eth_mii_clk,
00252           .flow_ctrl = regk_eth_yes
00253         };
00254 
00255         /* 
00256          * Initialize group address registers to make sure that no
00257          * unwanted addresses are matched.
00258          */
00259         REG_WR(eth, np->eth_inst, rw_ga_lo, ga_lo);
00260         REG_WR(eth, np->eth_inst, rw_ga_hi, ga_hi);
00261         
00262         /* Configure receiver and transmitter */
00263         REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
00264         REG_WR(eth, np->eth_inst, rw_tr_ctrl, tr_ctrl);
00265 
00266         /* Enable ethernet controller with mii clk. */
00267         REG_WR(eth, np->eth_inst, rw_gen_ctrl, gen_ctrl);
00268         gen_ctrl.en = regk_eth_yes;
00269         REG_WR(eth, np->eth_inst, rw_gen_ctrl, gen_ctrl);
00270         
00271         /* keep reset low (RESET_LEN) */
00272         udelay(500);
00273 
00274         /* done */
00275         
00276 #ifdef CONFIG_ETRAXFS
00277         reg_config_rw_pad_ctrl pad_ctrl;
00278         pad_ctrl = REG_RD(config, regi_config, rw_pad_ctrl);
00279         pad_ctrl.phyrst_n = 1;
00280         REG_WR(config, regi_config, rw_pad_ctrl, pad_ctrl);
00281 #else
00282         gen_ctrl.phyrst_n = 1;
00283         REG_WR(eth, np->eth_inst, rw_gen_ctrl, gen_ctrl);
00284 #endif
00285 
00286         /* Let the PHY reset (RESET_WAIT) */
00287         udelay(200);
00288                 
00289         if(crisv32_eth_probe_transceiver(dev)) {
00290                 printk("%s: No transceiver found, removing interface\n", 
00291                        dev->name);
00292                 unregister_netdev(dev);
00293                 /* We should probably do more here... */
00294         }
00295 }
00296 
00297 static void
00298 crisv32_eth_reset_rings(struct net_device *dev)
00299 {
00300         struct crisv32_ethernet_local *np = netdev_priv(dev);
00301         int i;
00302 
00303         /* free un-handled tx packets */       
00304         while(np->txpackets
00305               || np->catch_tx_desc != np->active_tx_desc) {
00306                 np->txpackets--;
00307                 if (np->catch_tx_desc->skb)
00308                         dev_kfree_skb(np->catch_tx_desc->skb);
00309                 
00310                 np->catch_tx_desc->skb = 0;
00311                 np->catch_tx_desc =
00312                         phys_to_virt((int)np->catch_tx_desc->descr.next);
00313         } while (np->catch_tx_desc != np->active_tx_desc);
00314         WARN_ON(np->txpackets != 0);
00315         np->txpackets = 0;
00316 
00317         /* cleanup the rx-ring */
00318         for (i = 0; i < NBR_RX_DESC; i++) {
00319                 struct sk_buff *skb;
00320                 skb = np->dma_rx_descr_list[i].skb;
00321                 if (!skb
00322                     || (np->dma_rx_descr_list[i].descr.buf !=
00323                         (void *)virt_to_phys(skb->data)))
00324                 {                      
00325                         printk("%s:%d: damaged rx-ring! "
00326                                "i=%d skb=%p %lx %lx %p %p\n",
00327                                __func__, __LINE__, i,
00328                                skb,
00329                                virt_to_phys(skb->data),
00330                                virt_to_phys(skb->data + MAX_MEDIA_DATA_SIZE),
00331                                np->dma_rx_descr_list[i].descr.buf,
00332                                np->dma_rx_descr_list[i].descr.after);
00333                         WARN_ON(1);
00334                         crisv32_ethernet_bug(dev);                      
00335                         if (skb)
00336                                 dev_kfree_skb(skb);
00337                         skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE);
00338                         np->dma_rx_descr_list[i].skb = skb;
00339                         np->dma_rx_descr_list[i].descr.buf =
00340                                 (char*)virt_to_phys(skb->data);
00341                 }
00342                 np->dma_rx_descr_list[i].descr.after =
00343                         (char*)virt_to_phys(skb->data
00344                                             + MAX_MEDIA_DATA_SIZE);
00345                 np->dma_rx_descr_list[i].descr.eol = 0;
00346                 np->dma_rx_descr_list[i].descr.in_eop = 0;
00347                 /* Workaround cache bug */
00348                 flush_dma_descr(&np->dma_rx_descr_list[i].descr, 1); 
00349         }
00350         
00351         /* reset rx-ring */
00352         np->active_rx_desc = &np->dma_rx_descr_list[0];
00353         np->prev_rx_desc = &np->dma_rx_descr_list[NBR_RX_DESC - 1];
00354         np->last_rx_desc = np->prev_rx_desc;
00355         np->dma_rx_descr_list[NBR_RX_DESC - 1].descr.eol = 1;
00356         /* ready to accept new packets.  */
00357         np->new_rx_package = 1;
00358         
00359         /* reset tx-ring */
00360         np->dma_tx_descr_list[0].descr.buf =
00361                 np->dma_tx_descr_list[0].descr.after = 0;
00362         np->dma_rx_descr_list[i].descr.in_eop = 0;
00363         np->dma_tx_descr_list[0].descr.eol = 1;
00364         
00365         np->active_tx_desc = &np->dma_tx_descr_list[0];
00366         np->prev_tx_desc = &np->dma_tx_descr_list[NBR_TX_DESC - 1];
00367         np->catch_tx_desc = &np->dma_tx_descr_list[0];
00368         
00369         /* Fill context descriptors. */
00370         np->ctxt_in.next = 0;
00371         np->ctxt_in.saved_data =
00372                 (void *)virt_to_phys(&np->active_rx_desc->descr);
00373         np->ctxt_in.saved_data_buf = np->active_rx_desc->descr.buf;
00374                 
00375         np->ctxt_out.next = 0;
00376         np->ctxt_out.saved_data =
00377                 (void *)virt_to_phys(&np->dma_tx_descr_list[0].descr);
00378 }
00379 
00380 /*
00381  * Really advance the receive ring. RX interrupts must be off.
00382  */
00383 static void
00384 __crisv32_eth_rx_ring_advance(struct crisv32_ethernet_local *np)
00385 {
00386         if (np->newbuf)
00387                 np->active_rx_desc->descr.buf = (void *) np->newbuf;
00388         np->active_rx_desc->descr.after =
00389                 np->active_rx_desc->descr.buf + MAX_MEDIA_DATA_SIZE;    
00390         np->active_rx_desc->descr.eol = 1;
00391         np->active_rx_desc->descr.in_eop = 0;
00392         np->active_rx_desc = phys_to_virt((int)np->active_rx_desc->descr.next);
00393         barrier();
00394         np->prev_rx_desc->descr.eol = 0;
00395 
00396         /* Workaround cache bug.  */
00397         flush_dma_descr(&np->prev_rx_desc->descr, 0);
00398         np->prev_rx_desc = phys_to_virt((int)np->prev_rx_desc->descr.next);
00399         flush_dma_descr(&np->prev_rx_desc->descr, 1);
00400 }
00401 
00402 /*
00403  * Advance the receive ring. RX interrupts must be off.
00404  */
00405 static inline void
00406 crisv32_eth_rx_ring_advance(struct crisv32_ethernet_local *np)
00407 {
00408         /*
00409          * When the input DMA reaches eol precaution must be taken, otherwise
00410          * the DMA could stop. The problem occurs if the eol flag is re-placed
00411          * on the descriptor that the DMA stands on before the DMA proceed to
00412          * the next descriptor. This case could, for example, happen if there
00413          * is a traffic burst and then the network goes silent. To prevent this
00414          * we make sure that we do not set the eol flag on the descriptor that
00415          * the DMA stands on.
00416          */
00417         if(virt_to_phys(&np->active_rx_desc->descr) !=
00418            REG_RD_INT(dma, np->dma_in_inst, rw_saved_data)) {
00419                 /* Now really advance the ring one step.  */
00420                 __crisv32_eth_rx_ring_advance(np);
00421                 if ((((crisv32_eth_descr*)(phys_to_virt((int)np->active_rx_desc->descr.next)))->descr.in_eop) && !(np->active_rx_desc->descr.in_eop)) __crisv32_eth_rx_ring_advance(np);
00422         } else {
00423                 /* delay the advancing of the ring.  */
00424                 np->new_rx_package = 0;
00425         }
00426 }
00427 
00428 static void __init
00429 crisv32_eth_init_rings(struct net_device *dev)
00430 {
00431         struct crisv32_ethernet_local *np = netdev_priv(dev);
00432         int i;
00433         
00434         /* Initialise receive descriptors for interface. */
00435         for (i = 0; i < NBR_RX_DESC; i++) {
00436                 struct sk_buff *skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE);
00437 
00438                 np->dma_rx_descr_list[i].skb = skb;
00439                 np->dma_rx_descr_list[i].descr.buf =
00440                         (char*)virt_to_phys(skb->data);
00441                 np->dma_rx_descr_list[i].descr.after =
00442                     (char*)virt_to_phys(skb->data + MAX_MEDIA_DATA_SIZE);
00443 
00444                 np->dma_rx_descr_list[i].descr.eol = 0;
00445                 np->dma_rx_descr_list[i].descr.in_eop = 0;
00446                 np->dma_rx_descr_list[i].descr.next =
00447                     (void *) virt_to_phys(&np->dma_rx_descr_list[i + 1].descr);
00448         }
00449         /* bend the list into a ring */ 
00450         np->dma_rx_descr_list[NBR_RX_DESC - 1].descr.next =
00451                 (void *) virt_to_phys(&np->dma_rx_descr_list[0].descr);
00452         
00453         /* Initialize transmit descriptors. */
00454         for (i = 0; i < NBR_TX_DESC; i++) {
00455                 np->dma_tx_descr_list[i].descr.wait = 1;
00456                 np->dma_tx_descr_list[i].descr.eol = 0;
00457                 np->dma_tx_descr_list[i].descr.out_eop = 0;
00458                 np->dma_tx_descr_list[i].descr.next =
00459                         (void*)virt_to_phys(&np->dma_tx_descr_list[i+1].descr);
00460         }
00461         /* bend the list into a ring */
00462         np->dma_tx_descr_list[NBR_TX_DESC - 1].descr.next =
00463                 (void *) virt_to_phys(&np->dma_tx_descr_list[0].descr);
00464 
00465         crisv32_eth_reset_rings(dev);
00466 }
00467 
00468 static void __init
00469 crisv32_init_leds(int ledgrp, struct net_device* dev)
00470 {
00471         struct timer_list timer_init = TIMER_INITIALIZER(NULL, 0, 0);
00472         struct crisv32_ethernet_local *np = netdev_priv(dev);
00473 
00474         /* Use already allocated led grp if initialized */
00475         if (crisv32_leds[ledgrp] != NULL) {
00476                 np->leds = crisv32_leds[ledgrp];
00477                 return;
00478         }
00479 
00480         crisv32_leds[ledgrp] = kmalloc(sizeof(struct crisv32_eth_leds),
00481                                        GFP_KERNEL);
00482 
00483         crisv32_leds[ledgrp]->ledgrp = ledgrp;
00484         crisv32_leds[ledgrp]->led_active = 0;
00485         /* NOTE: Should this value be set to zero as the jiffies timer 
00486            can wrap? */
00487         crisv32_leds[ledgrp]->led_next_time = jiffies;
00488 
00489         crisv32_leds[ledgrp]->clear_led_timer = timer_init;
00490         crisv32_leds[ledgrp]->clear_led_timer.function = 
00491                 crisv32_clear_network_leds;
00492         crisv32_leds[ledgrp]->clear_led_timer.data = (unsigned long) dev;
00493 
00494         spin_lock_init(&crisv32_leds[ledgrp]->led_lock);
00495 
00496         np->leds = crisv32_leds[ledgrp];
00497 }
00498 
00499 static int __init
00500 crisv32_ethernet_init(void)
00501 {
00502         struct crisv32_ethernet_local *np;
00503         int ret = 0;
00504 
00505         printk("ETRAX FS 10/100MBit ethernet v0.01 (c)"
00506                " 2003 Axis Communications AB\n");
00507 
00508 #ifdef CONFIG_CRIS_MACH_ARTPEC3
00509         {
00510                 reg_clkgen_rw_clk_ctrl clk_ctrl = REG_RD(clkgen, regi_clkgen, 
00511                                                          rw_clk_ctrl);
00512                 clk_ctrl.eth = clk_ctrl.dma0_1_eth = regk_clkgen_yes;  
00513                 REG_WR(clkgen, regi_clkgen, rw_clk_ctrl, clk_ctrl);
00514         }
00515 #endif
00516 #ifdef CONFIG_ETRAX_ETHERNET_IFACE0
00517 {
00518         int iface0 = 0;
00519 
00520 #ifdef CONFIG_CRIS_MACH_ARTPEC3
00521         if (crisv32_pinmux_alloc_fixed(pinmux_eth))
00522                 panic("Eth pinmux\n");
00523 #ifdef CONFIG_ETRAX_ETHERNET_GBIT
00524         if (crisv32_pinmux_alloc_fixed(pinmux_geth))
00525                 panic("Eth pinmux\n");
00526 #endif
00527 #endif
00528         
00529         if (!(crisv32_dev[iface0] = alloc_etherdev(sizeof *np)))
00530                 return -ENOMEM;
00531         
00532         ret |= crisv32_ethernet_device_init(crisv32_dev[iface0]);
00533         
00534 #if defined(CONFIG_ETRAX_ETH0_USE_LEDGRP0)
00535         crisv32_init_leds(LED_GRP_0,crisv32_dev[iface0]);
00536 #elif defined(CONFIG_ETRAX_ETH0_USE_LEDGRP1)
00537         crisv32_init_leds(LED_GRP_1,crisv32_dev[iface0]);
00538 #else
00539         crisv32_init_leds(LED_GRP_NONE,crisv32_dev[iface0]);
00540 #endif
00541 
00542         np = (struct crisv32_ethernet_local *) crisv32_dev[iface0]->priv;
00543         np->eth_inst = regi_eth0;
00544         np->dma_out_inst = regi_dma0;
00545         np->dma_in_inst = regi_dma1;
00546         
00547         register_netdev(crisv32_dev[iface0]);
00548         
00549         /* Set up default MAC address */
00550         memcpy(crisv32_dev[iface0]->dev_addr, default_mac_iface0.sa_data, 6);
00551         crisv32_eth_set_mac_address(crisv32_dev[iface0], &default_mac_iface0);
00552         if (crisv32_eth_request_irqdma(crisv32_dev[iface0]))
00553                 printk("%s: eth0 unable to allocate IRQ and DMA resources\n",
00554                        __func__);
00555         np->txpackets = 0;
00556         crisv32_eth_init_rings(crisv32_dev[iface0]);
00557         crisv32_eth_setup_controller(crisv32_dev[iface0]);
00558 }
00559 #endif /* CONFIG_ETRAX_ETHERNET_IFACE0 */
00560 
00561 #ifdef CONFIG_ETRAX_ETHERNET_IFACE1
00562 {
00563         int iface1 = 0;
00564         /* Default MAC address for interface 1.
00565          * The real one will be set later. */
00566         static struct sockaddr default_mac_iface1 = 
00567                 {0, {0x00, 0x40, 0x8C, 0xCD, 0x00, 0x01}};
00568 
00569         if (crisv32_pinmux_alloc_fixed(pinmux_eth1))
00570                 panic("Eth pinmux\n");
00571 
00572         /* Increase index to device array if interface 0 is enabled as well.*/
00573 #ifdef CONFIG_ETRAX_ETHERNET_IFACE0
00574         iface1++;
00575 #endif
00576         if (!(crisv32_dev[iface1] = alloc_etherdev(sizeof *np)))
00577                 return -ENOMEM;
00578         
00579         ret |= crisv32_ethernet_device_init(crisv32_dev[iface1]);
00580 
00581 #if defined(CONFIG_ETRAX_ETH1_USE_LEDGRP0)
00582         crisv32_init_leds(LED_GRP_0,crisv32_dev[iface1]);
00583 #elif defined(CONFIG_ETRAX_ETH1_USE_LEDGRP1)
00584         crisv32_init_leds(LED_GRP_1,crisv32_dev[iface1]);
00585 #else
00586         crisv32_init_leds(LED_GRP_NONE,crisv32_dev[iface1]);
00587 #endif
00588 
00589         np = (struct crisv32_ethernet_local *) crisv32_dev[iface1]->priv;
00590         np->eth_inst = regi_eth1;
00591         np->dma_out_inst = regi_dma6;
00592         np->dma_in_inst = regi_dma7;
00593         
00594         register_netdev(crisv32_dev[iface1]);
00595 
00596         /* Set up default MAC address */
00597         memcpy(crisv32_dev[iface1]->dev_addr, default_mac_iface1.sa_data, 6);
00598         crisv32_eth_set_mac_address(crisv32_dev[iface1], &default_mac_iface1);
00599         
00600         if (crisv32_eth_request_irqdma(crisv32_dev[iface1]))
00601                 printk("%s: eth1 unable to allocate IRQ and DMA resources\n",
00602                        __func__);
00603         np->txpackets = 0;
00604         crisv32_eth_init_rings(crisv32_dev[iface1]);
00605         crisv32_eth_setup_controller(crisv32_dev[iface1]);
00606 }
00607 #endif /* CONFIG_ETRAX_ETHERNET_IFACE1 */
00608 
00609 #ifdef CONFIG_CPU_FREQ
00610         cpufreq_register_notifier(&crisv32_ethernet_freq_notifier_block,
00611                                   CPUFREQ_TRANSITION_NOTIFIER);
00612 #endif
00613 
00614         return ret;
00615 }
00616 
00617 static int __init
00618 crisv32_ethernet_device_init(struct net_device* dev)
00619 {
00620         struct timer_list timer_init = TIMER_INITIALIZER(NULL, 0, 0);
00621         struct crisv32_ethernet_local *np;
00622 
00623         dev->base_addr = 0;     /* Just to have something to show. */
00624 
00625         /* we do our own locking */
00626         dev->features |= NETIF_F_LLTX;
00627         
00628         /* We use several IRQs and DMAs so just report 0 here. */
00629         dev->irq = 0;
00630         dev->dma = 0;
00631 
00632         /* 
00633          * Fill in our handlers so the network layer can talk to us in the
00634          * future. 
00635          */
00636         dev->open = crisv32_eth_open;
00637         dev->hard_start_xmit = crisv32_eth_send_packet;
00638         dev->stop = crisv32_eth_close;
00639         dev->get_stats = crisv32_get_stats;
00640         dev->set_multicast_list = crisv32_eth_set_multicast_list;
00641         dev->set_mac_address = crisv32_eth_set_mac_address;
00642         dev->ethtool_ops = &crisv32_ethtool_ops;
00643         dev->do_ioctl = crisv32_eth_ioctl;
00644         dev->set_config = crisv32_eth_set_config;
00645         dev->tx_timeout = crisv32_eth_tx_timeout;
00646 #ifdef CONFIG_NET_POLL_CONTROLLER
00647         dev->poll_controller = crisv32_netpoll;
00648 #endif
00649         /*
00650          * 8 skbs keeps the system very reponsive even under high load.
00651          * At 64 the system locks, pretty much the same way as without NAPI.
00652          * 
00653          * TODO: meassure with 2 interfaces
00654          */
00655         dev->weight = 8;
00656         dev->poll = crisv32_eth_poll;
00657         
00658         np = netdev_priv(dev);
00659 
00660         spin_lock_init(&np->lock);
00661         spin_lock_init(&np->transceiver_lock);
00662         
00663         /* Initialize speed indicator stuff. */
00664         np->current_speed = 10;
00665         np->current_speed_selection = 0;        /* Auto. */
00666         np->speed_timer = timer_init;
00667         np->speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
00668         np->speed_timer.data = (unsigned long) dev;
00669         np->speed_timer.function = crisv32_eth_check_speed;
00670 
00671         np->full_duplex = 0;
00672         np->current_duplex = autoneg;
00673         np->duplex_timer = timer_init;
00674         np->duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
00675         np->duplex_timer.data = (unsigned long) dev;
00676         np->duplex_timer.function = crisv32_eth_check_duplex;
00677         
00678         return 0;
00679 }
00680 
00681 static int
00682 crisv32_eth_open(struct net_device *dev)
00683 {
00684         struct sockaddr mac_addr;
00685         reg_dma_rw_ack_intr ack_intr = { .data = 1,.in_eop = 1 };
00686         reg_eth_rw_clr_err clr_err = {.clr = regk_eth_yes};
00687         /*
00688          * dont interrupt us at any stat counter thresholds, only at urun
00689          * and exc_col.
00690          */
00691         int intr_mask_nw = 0x1800;
00692         int eth_ack_intr = 0xffff;
00693         struct crisv32_ethernet_local *np = netdev_priv(dev);
00694 
00695         spin_lock(&np->lock);
00696 #ifdef CONFIG_CRIS_MACH_ARTPEC3
00697         np->gigabit_mode = 0;
00698 #endif
00699         crisv32_disable_tx_ints(np);
00700         crisv32_disable_rx_ints(np);
00701         
00702         REG_WR(eth, np->eth_inst, rw_clr_err, clr_err);
00703         REG_WR_INT(eth, np->eth_inst, rw_ack_intr, eth_ack_intr);
00704         REG_WR_INT(eth, np->eth_inst, rw_intr_mask, intr_mask_nw);
00705         crisv32_eth_reset_rings(dev);
00706         
00707         /* Give the hardware an idea of what MAC address we want. */
00708         memcpy(mac_addr.sa_data, dev->dev_addr, dev->addr_len);
00709         crisv32_eth_set_mac_address(dev, &mac_addr);
00710 
00711         /* Enable irq and make sure that the irqs are cleared. */
00712         REG_WR(dma, np->dma_out_inst, rw_ack_intr, ack_intr);
00713         REG_WR(dma, np->dma_in_inst, rw_ack_intr, ack_intr);
00714         
00715         /* Prepare input DMA. */
00716         DMA_RESET(np->dma_in_inst);
00717         DMA_ENABLE(np->dma_in_inst);
00718 #ifdef CONFIG_CRIS_MACH_ARTPEC3
00719         DMA_WR_CMD(np->dma_in_inst, regk_dma_set_w_size2);
00720 #endif
00721         DMA_START_CONTEXT( np->dma_in_inst, virt_to_phys(&np->ctxt_in));
00722         DMA_CONTINUE(np->dma_in_inst);
00723         crisv32_enable_rx_ints(np);
00724         crisv32_start_receiver(np);     
00725         
00726         /* Prepare output DMA. */
00727         DMA_RESET(np->dma_out_inst);
00728         DMA_ENABLE(np->dma_out_inst);
00729 #ifdef CONFIG_CRIS_MACH_ARTPEC3
00730         DMA_WR_CMD(np->dma_out_inst, regk_dma_set_w_size4);
00731 #endif
00732         netif_start_queue(dev);
00733         crisv32_enable_tx_ints(np);
00734         
00735         /* Start duplex/speed timers */
00736         add_timer(&np->speed_timer);
00737         add_timer(&np->duplex_timer);   
00738 
00739         spin_unlock(&np->lock);
00740         /* 
00741          * We are now ready to accept transmit requests from the queueing
00742          * layer of the networking.
00743          */
00744         netif_carrier_on(dev);
00745         netif_poll_enable(dev);
00746 
00747         return 0;
00748 }
00749 
00750 static int
00751 crisv32_eth_close(struct net_device *dev)
00752 {
00753         reg_dma_rw_ack_intr ack_intr = {0};
00754 
00755         struct crisv32_ethernet_local *np = netdev_priv(dev);
00756         unsigned long flags;
00757 
00758         printk(KERN_INFO "Closing %s.\n", dev->name);
00759         spin_lock_irqsave(&np->lock, flags);
00760 
00761         /* stop the receiver before the DMA channels to avoid overruns. */
00762         crisv32_disable_rx_ints(np);
00763         netif_poll_disable(dev);
00764         crisv32_stop_receiver(np);
00765         
00766         netif_stop_queue(dev);
00767 
00768         /* Reset the TX DMA in case it has hung on something. */
00769         DMA_RESET(np->dma_in_inst);
00770 
00771         /* Stop DMA */
00772         DMA_STOP(np->dma_in_inst);
00773         DMA_STOP(np->dma_out_inst);
00774 
00775         /* Disable irq and make sure that the irqs are cleared. */
00776         crisv32_disable_tx_ints(np);
00777         ack_intr.data = 1;
00778         REG_WR(dma, np->dma_out_inst, rw_ack_intr, ack_intr);
00779 
00780         ack_intr.in_eop = 1;
00781         REG_WR(dma, np->dma_in_inst, rw_ack_intr, ack_intr);
00782 
00783         np->sender_started = 0; 
00784         spin_unlock_irqrestore(&np->lock, flags);
00785 
00786         /* Update the statistics. */
00787         update_rx_stats(np);
00788         update_tx_stats(np);
00789 
00790         /* Stop speed/duplex timers */
00791         del_timer(&np->speed_timer);
00792         del_timer(&np->duplex_timer);
00793 
00794         return 0;
00795 }
00796 
00797 static int
00798 crisv32_eth_set_mac_address(struct net_device *dev, void *vpntr)
00799 {
00800         int i;
00801         unsigned char *addr = ((struct sockaddr*)vpntr)->sa_data;
00802         
00803         reg_eth_rw_ma0_lo ma0_lo =
00804           { addr[0] | (addr[1] << 8) | (addr[2] << 16) | (addr[3] << 24)};
00805         
00806         reg_eth_rw_ma0_hi ma0_hi = { addr[4] | (addr[5] << 8) };
00807 
00808         struct crisv32_ethernet_local *np = netdev_priv(dev);
00809 
00810         /* Remember the address. */
00811         memcpy(dev->dev_addr, addr, dev->addr_len);
00812 
00813         /* 
00814          * Write the address to the hardware.
00815          * Note the way the address is wrapped:
00816          * ma0_l0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
00817          * ma0_hi = a0_4 | (a0_5 << 8);
00818          */
00819         REG_WR(eth, np->eth_inst, rw_ma0_lo, ma0_lo);
00820         REG_WR(eth, np->eth_inst, rw_ma0_hi, ma0_hi);
00821 
00822         printk(KERN_INFO "%s: changed MAC to ", dev->name);
00823 
00824         for (i = 0; i < 5; i++)
00825                 printk("%02X:", dev->dev_addr[i]);
00826 
00827         printk("%02X\n", dev->dev_addr[i]);
00828 
00829         return 0;
00830 }
00831 
00832 static irqreturn_t
00833 crisv32rx_eth_interrupt(int irq, void *dev_id)
00834 {
00835         struct net_device *dev = (struct net_device *) dev_id;
00836         struct crisv32_ethernet_local *np = netdev_priv(dev);
00837         reg_dma_r_masked_intr masked_in;
00838 
00839         masked_in = REG_RD(dma, np->dma_in_inst, r_masked_intr);
00840         
00841         if (masked_in.in_eop) {
00842                 reg_dma_rw_ack_intr ack_intr = {0};     
00843 
00844                         /*
00845                  * Ack the rx irq even if we are not prepared to start
00846                  * polling. This is needed to handle incomming packets
00847                  * during the stop sequence.
00848                          */
00849                 ack_intr.in_eop = 1;
00850                         REG_WR(dma, np->dma_in_inst, rw_ack_intr, ack_intr);
00851                 if (netif_rx_schedule_prep(dev)) {
00852                         crisv32_disable_rx_ints(np);
00853                         /* put us onto the poll list */
00854                         __netif_rx_schedule(dev);
00855                 }
00856         } else {
00857                 /* Unexpected, ACK it and hope for the best.  */
00858                 reg_dma_rw_ack_intr ack_intr = {0xffff};
00859                 printk("unexpected eth-rx irq %x\n",
00860                        REG_RD_INT(dma, np->dma_in_inst, r_masked_intr));
00861                 ack_intr.in_eop = 0;
00862                 REG_WR(dma, np->dma_in_inst, rw_ack_intr, ack_intr);
00863         }
00864 
00865         return IRQ_HANDLED;
00866 }
00867 
00868 static irqreturn_t
00869 crisv32tx_eth_interrupt(int irq, void *dev_id)
00870 {
00871         reg_dma_rw_stat stat;
00872         dma_descr_data *dma_pos;
00873         reg_dma_rw_ack_intr ack_intr = { .data = 1 };
00874         reg_dma_r_masked_intr masked_out;
00875         
00876         struct net_device *dev = (struct net_device *) dev_id;
00877         struct crisv32_ethernet_local *np = netdev_priv(dev);
00878         unsigned long flags;
00879         
00880         masked_out = REG_RD(dma, np->dma_out_inst, r_masked_intr);
00881                 
00882         /* Get the current output dma position. */
00883         stat = REG_RD(dma, np->dma_out_inst, rw_stat);
00884         if (stat.list_state == regk_dma_data_at_eol)
00885                 dma_pos = &np->active_tx_desc->descr;
00886         else
00887                 dma_pos = phys_to_virt(REG_RD_INT(dma, np->dma_out_inst,
00888                                                   rw_data));
00889         
00890         /* ack the interrupt, if it was active */
00891         if (masked_out.data)
00892         REG_WR(dma, np->dma_out_inst, rw_ack_intr, ack_intr);
00893         else
00894                 printk("unexpected eth-tx irq %x\n", 
00895                        REG_RD_INT(dma, np->dma_out_inst, r_masked_intr));
00896         
00897         /* protect against ethernet excessive-col interrupts */
00898         spin_lock_irqsave(&np->lock, flags);
00899 
00900         /* Take care of transmited dma descriptors and report sent packet. */
00901         while (np->txpackets && ((&np->catch_tx_desc->descr != dma_pos)
00902                                  || netif_queue_stopped(dev))) {
00903                 /* Update sent packet statistics. */
00904                 np->stats.tx_bytes += np->catch_tx_desc->skb->len;
00905                 np->stats.tx_packets++;
00906                 
00907                 dev_kfree_skb_irq(np->catch_tx_desc->skb);
00908                 np->catch_tx_desc->skb = 0;
00909                 np->txpackets--;
00910                 np->catch_tx_desc =
00911                         phys_to_virt((int)np->catch_tx_desc->descr.next);
00912 #ifdef CONFIG_CRIS_MACH_ARTPEC3
00913                 if (np->gigabit_mode) {
00914                   np->intmem_tx_buf_catch->free = 1;
00915                   np->intmem_tx_buf_catch = np->intmem_tx_buf_catch->next;
00916                 }
00917 #endif
00918                 netif_wake_queue(dev);
00919         }
00920         spin_unlock_irqrestore(&np->lock, flags);
00921         return IRQ_HANDLED;
00922 }
00923 
00924 
00925 /* Update receive errors. */
00926 static void
00927 update_rx_stats(struct crisv32_ethernet_local *np)
00928 {
00929         reg_eth_rs_rec_cnt r;
00930         reg_eth_rs_phy_cnt rp;
00931 
00932         r = REG_RD(eth, np->eth_inst, rs_rec_cnt);
00933         rp = REG_RD(eth, np->eth_inst, rs_phy_cnt);
00934 
00935         np->stats.rx_fifo_errors += r.congestion;
00936         np->stats.rx_crc_errors += r.crc_err;
00937         np->stats.rx_frame_errors += r.align_err;
00938         np->stats.rx_length_errors += r.oversize;
00939 }
00940 
00941 /* Update transmit errors. */
00942 static void
00943 update_tx_stats(struct crisv32_ethernet_local *np)
00944 {
00945         reg_eth_rs_tr_cnt r;
00946 
00947         r = REG_RD(eth, np->eth_inst, rs_tr_cnt);
00948 
00949         np->stats.collisions += r.single_col + r.mult_col;
00950         np->stats.tx_errors += r.deferred;
00951 }
00952 
00953 /* Get current statistics. */
00954 static struct net_device_stats *
00955 crisv32_get_stats(struct net_device *dev)
00956 {
00957         unsigned long flags;
00958         struct crisv32_ethernet_local *np = netdev_priv(dev);
00959         
00960         spin_lock_irqsave(&np->lock, flags);
00961 
00962         update_rx_stats(np);
00963         update_tx_stats(np);
00964 
00965         spin_unlock_irqrestore(&np->lock, flags);
00966 
00967         return &np->stats;
00968 }
00969 
00970 /* Check for network errors. This acknowledge the received interrupt. */
00971 static irqreturn_t
00972 crisv32nw_eth_interrupt(int irq, void *dev_id)
00973 {
00974         struct net_device *dev = (struct net_device *) dev_id;
00975         struct crisv32_ethernet_local *np = netdev_priv(dev);
00976         reg_eth_r_masked_intr intr_mask;
00977         int ack_intr = 0xffff;
00978         reg_eth_rw_clr_err clr_err;
00979 
00980         intr_mask = REG_RD(eth, np->eth_inst, r_masked_intr);
00981 
00982         /*
00983          * Check for underrun and/or excessive collisions. Note that the
00984          * rw_clr_err register clears both underrun and excessive collision
00985          * errors, so there's no need to check them separately.
00986          */
00987         if (np->sender_started
00988             && (intr_mask.urun || intr_mask.exc_col)) {
00989                 unsigned long flags;
00990                 dma_descr_data *dma_pos;
00991                 reg_dma_rw_stat stat;
00992 
00993                 /* Get the current output dma position. */
00994                 stat = REG_RD(dma, np->dma_out_inst, rw_stat);
00995                 if (stat.list_state == regk_dma_data_at_eol)
00996                         dma_pos = &np->active_tx_desc->descr;
00997                 else
00998                         dma_pos = phys_to_virt(REG_RD_INT(dma,
00999                                                           np->dma_out_inst,
01000                                                           rw_data));
01001 
01002                 /*
01003                  * Protect against the tx-interrupt messing with
01004                  * the tx-ring.
01005                  */
01006                 spin_lock_irqsave(&np->lock, flags);
01007                 /*
01008                  * If we have more than one packet in the tx-ring
01009                  * drop one and move ahead. Upper layers rely on
01010                  * packeloss when doing congestion control.
01011                  */
01012                 if (intr_mask.exc_col && np->txpackets > 1) {
01013                         dev_kfree_skb_irq(np->catch_tx_desc->skb);
01014                         np->catch_tx_desc->skb = 0;
01015                         np->catch_tx_desc = 
01016                                 phys_to_virt((int)
01017                                              np->catch_tx_desc->descr.next);
01018                         np->txpackets--;
01019                         netif_wake_queue(dev);
01020                 }
01021                 np->ctxt_out.next = 0;
01022                 if (np->txpackets) {
01023                         np->ctxt_out.saved_data = (void *)
01024                                 virt_to_phys(&np->catch_tx_desc->descr);
01025                         np->ctxt_out.saved_data_buf =
01026                                 np->catch_tx_desc->descr.buf;
01027                         
01028                         /* restart the DMA */
01029                         DMA_START_CONTEXT(np->dma_out_inst,
01030                                           (int) virt_to_phys(&np->ctxt_out));
01031                 }
01032                 else {
01033                         /* let the next packet restart the DMA */
01034                         np->ctxt_out.saved_data = (void *)
01035                                 virt_to_phys(&np->active_tx_desc->descr);
01036                         np->sender_started = 0;
01037                 }
01038                 
01039                 spin_unlock_irqrestore(&np->lock, flags);
01040                 np->stats.tx_errors++;
01041         }
01042 
01043         REG_WR_INT(eth, np->eth_inst, rw_ack_intr, ack_intr);
01044         clr_err.clr = 1;
01045         REG_WR(eth, np->eth_inst, rw_clr_err, clr_err);
01046         
01047         update_rx_stats(np);
01048         update_tx_stats(np);
01049         
01050         return IRQ_HANDLED;
01051 }
01052 
01053 /* We have a good packet(s), get it/them out of the buffers. */
01054 static void
01055 crisv32_eth_receive_packet(struct net_device *dev)
01056 {
01057         int length;
01058         struct sk_buff *skb;
01059         struct crisv32_ethernet_local *np = netdev_priv(dev);
01060         struct sk_buff *tmp;
01061         unsigned long flags;
01062 
01063         DEBUG(printk("crisv32_receive_packet\n"));
01064 
01065         /* Activate LED */
01066         spin_lock_irqsave(&np->leds->led_lock, flags);
01067         if (!np->leds->led_active && time_after(jiffies, 
01068                                                 np->leds->led_next_time)) {
01069                 /* light the network leds depending on the current speed. */
01070                 crisv32_set_network_leds(LED_ACTIVITY, dev);
01071 
01072                 /* Set the earliest time we may clear the LED */
01073                 np->leds->led_next_time = jiffies + NET_FLASH_TIME;
01074                 np->leds->led_active = 1;
01075                 np->leds->clear_led_timer.data = (unsigned long) dev;
01076                 mod_timer(&np->leds->clear_led_timer, jiffies + HZ/10);
01077         }
01078         spin_unlock_irqrestore(&np->leds->led_lock, flags);
01079 
01080         /* Discard CRC (4 bytes). */
01081         length = (np->active_rx_desc->descr.after) - 
01082                 (np->active_rx_desc->descr.buf) - 4;
01083 
01084         /* Update received packet statistics. */
01085         np->stats.rx_bytes += length;
01086 
01087         if (np->active_rx_desc != np->last_rx_desc) {
01088 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01089                 if (np->gigabit_mode) {
01090                         skb = dev_alloc_skb(length);
01091                         if(!skb) {
01092                                 np->stats.rx_errors++;
01093                                 printk(KERN_NOTICE "%s: memory squeeze,"
01094                                        " dropping packet.", dev->name);
01095                                 return;
01096                         }
01097                         /* Allocate room for the packet body. */
01098                         skb_put(skb, length - ETHER_HEAD_LEN);
01099                         /* Allocate room for the header and copy the data to
01100                          * the SKB */
01101                         memcpy(skb_push(skb, ETHER_HEAD_LEN),
01102                                crisv32_intmem_phys_to_virt((unsigned long)np->active_rx_desc->descr.buf), length);
01103                         skb->dev = dev;
01104                         skb->protocol = eth_type_trans(skb, dev);
01105                         skb->ip_summed = CHECKSUM_NONE;
01106                         /* Send the packet to the upper layer. */
01107                         netif_receive_skb(skb);
01108                         np->last_rx_desc =
01109                                 (void *) phys_to_virt(np->last_rx_desc->descr.next);
01110                         np->newbuf = 0;
01111                 } else {
01112 #endif
01113                         tmp = dev_alloc_skb(MAX_MEDIA_DATA_SIZE);
01114                         if (!tmp) {
01115                                 np->stats.rx_errors++;
01116                                 printk(KERN_NOTICE "%s: memory squeeze,"
01117                                        " dropping packet.",
01118                                        dev->name);
01119                                 return;
01120                         }
01121                         skb = np->active_rx_desc->skb;
01122                         np->active_rx_desc->skb = tmp;
01123                         skb_put(skb, length);
01124 
01125                         np->newbuf = 
01126                                 virt_to_phys(np->active_rx_desc->skb->data);
01127 
01128                         skb->dev = dev;
01129                         skb->protocol = eth_type_trans(skb, dev);
01130                         skb->ip_summed = CHECKSUM_NONE;
01131 
01132                         /* Send the packet to the upper layer. */
01133                         netif_receive_skb(skb);
01134                         np->last_rx_desc =
01135                                 phys_to_virt((int)
01136                                              np->last_rx_desc->descr.next);
01137                 }
01138 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01139         }
01140 #endif
01141 
01142         /* Forward rotate the receive ring.  */
01143         crisv32_eth_rx_ring_advance(np);
01144 }
01145 
01146 /*
01147  * Is there work to do in the rx-path?
01148  */
01149 static inline int crisv32_has_rx_work(struct crisv32_ethernet_local *np,
01150                                       dma_descr_data *active)
01151 {
01152         int mw;
01153         mw = (active->in_eop == 1 && np->new_rx_package);
01154         return mw;
01155 }
01156 
01157 /*
01158  * NAPI poll
01159  *
01160  * We are allowed to poll dev->quota skb's from the rx-ring.
01161  * Must update *budget and dev->quota. If we are done, remove us from the
01162  * poll list and re-enable interrupts.
01163  *
01164  * TODO: Add tx-reclaim,
01165  *       Are there races when we re-enable ints?
01166  */
01167 static int crisv32_eth_poll(struct net_device *dev, int *budget)
01168 {
01169         struct crisv32_ethernet_local *np = netdev_priv(dev);
01170         int workdone = dev->quota;
01171         int morework;
01172         reg_dma_rw_ack_intr ack_intr = {0};     
01173 
01174         ack_intr.in_eop = 1;
01175 
01176         if (np->new_rx_package == 0) {
01177         /*
01178                  * In the previous round we pulled a packet from the ring but 
01179                  * we didn't advance the ring due to hw DMA bug. Try to do it
01180                  * now.
01181          */
01182                 np->new_rx_package = 1;
01183                 crisv32_eth_rx_ring_advance(np);
01184         }
01185 
01186         morework = crisv32_has_rx_work(np, &np->active_rx_desc->descr);
01187 
01188         while (morework && dev->quota)
01189         {
01190                 crisv32_eth_receive_packet(dev);
01191                 dev->quota--;
01192                 np->stats.rx_packets++;
01193 
01194                 /* Ack irq and restart rx dma */
01195                 REG_WR(dma, np->dma_in_inst, rw_ack_intr, ack_intr);
01196                 DMA_CONTINUE_DATA(np->dma_in_inst);
01197 
01198                 morework = crisv32_has_rx_work(np, &np->active_rx_desc->descr);
01199         }
01200         update_rx_stats(np);
01201 
01202         /* it's our responsability to update the global budget. */
01203         *budget -= (workdone - dev->quota);
01204 
01205         if (!morework) {                
01206                 /* first mark as done, then enable irq's */
01207                 netif_rx_complete(dev);
01208                 crisv32_enable_rx_ints(np);
01209         }
01210 
01211         return morework;
01212 }
01213 
01214 /* 
01215  * This function (i.e. hard_start_xmit) is protected from concurent calls by a
01216  * spinlock (xmit_lock) in the net_device structure.
01217  */
01218 static int
01219 crisv32_eth_send_packet(struct sk_buff *skb, struct net_device *dev)
01220 {
01221         struct crisv32_ethernet_local *np = netdev_priv(dev);
01222         unsigned char *buf = skb->data;
01223         unsigned long flags;
01224 
01225         dev->trans_start = jiffies;
01226         
01227         spin_lock_irqsave(&np->leds->led_lock, flags);
01228         if (!np->leds->led_active && time_after(jiffies, 
01229                                                 np->leds->led_next_time)) {
01230                 /* light the network leds depending on the current speed. */
01231                 crisv32_set_network_leds(LED_ACTIVITY, dev);
01232 
01233                 /* Set the earliest time we may clear the LED */
01234                 np->leds->led_next_time = jiffies + NET_FLASH_TIME;
01235                 np->leds->led_active = 1;
01236                 np->leds->clear_led_timer.data = (unsigned long) dev;
01237                 mod_timer(&np->leds->clear_led_timer, jiffies + HZ/10);
01238         }
01239         spin_unlock_irqrestore(&np->leds->led_lock, flags);
01240 
01241         /*
01242          * Need to disable irq to avoid updating pointer in interrupt while
01243          * sending packets.
01244          */
01245         spin_lock_irqsave(&np->lock, flags);
01246         
01247         np->active_tx_desc->skb = skb;
01248 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01249         if (np->gigabit_mode) {
01250                 if(np->intmem_tx_buf_active->free) {
01251                         memcpy(np->intmem_tx_buf_active->buf,
01252                                skb->data, skb->len);
01253                         np->intmem_tx_buf_active->free = 0;
01254                         crisv32_eth_hw_send_packet(
01255                                 np->intmem_tx_buf_active->buf, skb->len, np);
01256                         np->intmem_tx_buf_active =
01257                                 np->intmem_tx_buf_active->next;
01258                 } else {
01259                         printk("%s: Internal tx memory buffer not free!\n\r",
01260                                __FILE__);
01261                         spin_unlock_irqrestore(&np->lock, flags);
01262                         return 1;
01263                 }
01264         }
01265         else 
01266 #endif
01267         {
01268                 crisv32_eth_hw_send_packet(buf, skb->len, np);
01269         }
01270 
01271         /* Stop queue if full. */
01272         if (
01273 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01274                 phys_to_virt((int)np->active_tx_desc->descr.next)
01275 #else
01276                 np->active_tx_desc
01277 #endif
01278                 == np->catch_tx_desc) {
01279                 netif_stop_queue(dev);
01280         }
01281         
01282         np->txpackets++;
01283         spin_unlock_irqrestore(&np->lock, flags);
01284         
01285         return 0;
01286 }
01287 
01288 
01289 static void
01290 crisv32_eth_hw_send_packet(unsigned char *buf, int length, void *priv)
01291 {
01292         struct crisv32_ethernet_local *np =
01293                 (struct crisv32_ethernet_local *) priv;
01294 
01295         /* Configure the tx dma descriptor. */
01296 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01297         if (np->gigabit_mode) {
01298           np->active_tx_desc->descr.buf = 
01299                   (unsigned char *) crisv32_intmem_virt_to_phys(buf);
01300         } else 
01301 #endif
01302         {
01303           np->active_tx_desc->descr.buf = (unsigned char *) virt_to_phys(buf);
01304         }
01305           
01306         np->active_tx_desc->descr.after = np->active_tx_desc->descr.buf +
01307                 length;
01308         np->active_tx_desc->descr.intr = 1;        
01309         np->active_tx_desc->descr.out_eop = 1;
01310 
01311         /* Move eol. */
01312         np->active_tx_desc->descr.eol = 1;
01313         np->prev_tx_desc->descr.eol = 0;
01314 
01315 
01316         /* Update pointers. */
01317         np->prev_tx_desc = np->active_tx_desc;
01318         np->active_tx_desc = phys_to_virt((int)np->active_tx_desc->descr.next);
01319 
01320         /* Start DMA. */
01321         crisv32_start_dma_out(np);
01322 }
01323 
01324 static void
01325 crisv32_start_dma_out(struct crisv32_ethernet_local* np)
01326 {
01327         if (!np->sender_started) {
01328                 /* Start DMA for the first time. */
01329                 np->ctxt_out.saved_data_buf = np->prev_tx_desc->descr.buf;
01330                 REG_WR(dma, np->dma_out_inst, rw_group_down,
01331                        (int) virt_to_phys(&np->ctxt_out));
01332                 DMA_WR_CMD(np->dma_out_inst, regk_dma_load_c);
01333                 DMA_WR_CMD(np->dma_out_inst, regk_dma_load_d | regk_dma_burst);
01334                 np->sender_started = 1;
01335         } else {
01336                 DMA_CONTINUE_DATA(np->dma_out_inst);
01337         }
01338 }
01339 
01340 /* 
01341  * Called by upper layers if they decide it took too long to complete sending
01342  * a packet - we need to reset and stuff.
01343  */
01344 static void
01345 crisv32_eth_tx_timeout(struct net_device *dev)
01346 {
01347         struct crisv32_ethernet_local *np = netdev_priv(dev);
01348         reg_dma_rw_stat stat = {0};
01349         unsigned long flags;
01350 
01351         printk(KERN_WARNING "%s: transmit timed out\n", dev->name);
01352 
01353         spin_lock_irqsave(&np->lock, flags);
01354         update_tx_stats(np);
01355 
01356         crisv32_ethernet_bug(dev);
01357 
01358         np->txpackets = 0;
01359         /* Update error stats. */
01360         np->stats.tx_errors++; 
01361 
01362         /* Reset the TX DMA in case it has hung on something. */
01363         DMA_RESET(np->dma_out_inst);
01364         do {
01365                 stat = REG_RD(dma, np->dma_out_inst, rw_stat);
01366         } while (stat.mode != regk_dma_rst);
01367 
01368         /* Reset the tranceiver. */
01369         crisv32_eth_reset_tranceiver(dev);
01370 
01371         /* Get rid of the packets that never got an interrupt. */
01372         do {
01373                 if (np->catch_tx_desc->skb)
01374                         dev_kfree_skb(np->catch_tx_desc->skb);
01375                 
01376                 np->catch_tx_desc->skb = 0;
01377                 np->catch_tx_desc =
01378                         phys_to_virt((int)np->catch_tx_desc->descr.next);
01379         } while (np->catch_tx_desc != np->active_tx_desc);
01380 
01381         reg_eth_rw_gen_ctrl gen_ctrl = { 0 };
01382         gen_ctrl.en = regk_eth_yes;
01383         REG_WR(eth, np->eth_inst, rw_gen_ctrl, gen_ctrl);
01384 
01385         int ack_intr = 0xffff;
01386         REG_WR_INT(eth, np->eth_inst, rw_ack_intr, ack_intr);
01387         reg_eth_rw_clr_err clr_err;
01388         clr_err.clr = 1;
01389         REG_WR(eth, np->eth_inst, rw_clr_err, clr_err);
01390 
01391         crisv32_enable_tx_ints(np);
01392 
01393         DMA_ENABLE(np->dma_out_inst);
01394 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01395         DMA_WR_CMD(np->dma_out_inst, regk_dma_set_w_size4);
01396 #endif
01397 
01398         /* Next packet will restart output DMA. */
01399         np->sender_started = 0;
01400 
01401         spin_unlock_irqrestore(&np->lock, flags);
01402 
01403         /* Tell the upper layers we're ok again. */
01404         netif_wake_queue(dev);
01405 }
01406 
01407 /*
01408  * Set or clear the multicast filter for this adaptor.
01409  * num_addrs == -1      Promiscuous mode, receive all packets
01410  * num_addrs == 0       Normal mode, clear multicast list
01411  * num_addrs > 0        Multicast mode, receive normal and MC packets,
01412  *                      and do best-effort filtering.
01413  */
01414 static void
01415 crisv32_eth_set_multicast_list(struct net_device *dev)
01416 {
01417         int num_addr = dev->mc_count;
01418         unsigned long int lo_bits;
01419         unsigned long int hi_bits;
01420         reg_eth_rw_rec_ctrl rec_ctrl = {0};
01421         reg_eth_rw_ga_lo ga_lo = {0};
01422         reg_eth_rw_ga_hi ga_hi = {0};
01423         struct crisv32_ethernet_local *np = netdev_priv(dev);
01424 
01425         if (dev->flags & IFF_PROMISC) {
01426                 /* Promiscuous mode. */
01427                 lo_bits = 0xfffffffful;
01428                 hi_bits = 0xfffffffful;
01429 
01430                 /* Enable individual receive. */
01431                 rec_ctrl = (reg_eth_rw_rec_ctrl) REG_RD(eth, np->eth_inst,
01432                                                         rw_rec_ctrl);
01433                 rec_ctrl.individual = regk_eth_yes;
01434                 REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
01435         } else if (dev->flags & IFF_ALLMULTI) {
01436                 /* Enable all multicasts. */
01437                 lo_bits = 0xfffffffful;
01438                 hi_bits = 0xfffffffful;
01439 
01440                 /* Disable individual receive */
01441                 rec_ctrl =
01442                   (reg_eth_rw_rec_ctrl) REG_RD(eth, np->eth_inst, rw_rec_ctrl);
01443                 rec_ctrl.individual = regk_eth_no;
01444                 REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
01445         } else if (num_addr == 0) {
01446                 /* Normal, clear the mc list. */
01447                 lo_bits = 0x00000000ul;
01448                 hi_bits = 0x00000000ul;
01449 
01450                 /* Disable individual receive */
01451                 rec_ctrl =
01452                   (reg_eth_rw_rec_ctrl) REG_RD(eth, np->eth_inst, rw_rec_ctrl);
01453                 rec_ctrl.individual = regk_eth_no;
01454                 REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
01455         } else {
01456                 /* MC mode, receive normal and MC packets. */
01457                 char hash_ix;
01458                 struct dev_mc_list *dmi = dev->mc_list;
01459                 int i;
01460                 char *baddr;
01461                 lo_bits = 0x00000000ul;
01462                 hi_bits = 0x00000000ul;
01463                 
01464                 for (i = 0; i < num_addr; i++) {
01465                         /* Calculate the hash index for the GA registers. */
01466                         hash_ix = 0;
01467                         baddr = dmi->dmi_addr;
01468                         hash_ix ^= (*baddr) & 0x3f;
01469                         hash_ix ^= ((*baddr) >> 6) & 0x03;
01470                         ++baddr;
01471                         hash_ix ^= ((*baddr) << 2) & 0x03c;
01472                         hash_ix ^= ((*baddr) >> 4) & 0xf;
01473                         ++baddr;
01474                         hash_ix ^= ((*baddr) << 4) & 0x30;
01475                         hash_ix ^= ((*baddr) >> 2) & 0x3f;
01476                         ++baddr;
01477                         hash_ix ^= (*baddr) & 0x3f;
01478                         hash_ix ^= ((*baddr) >> 6) & 0x03;
01479                         ++baddr;
01480                         hash_ix ^= ((*baddr) << 2) & 0x03c;
01481                         hash_ix ^= ((*baddr) >> 4) & 0xf;
01482                         ++baddr;
01483                         hash_ix ^= ((*baddr) << 4) & 0x30;
01484                         hash_ix ^= ((*baddr) >> 2) & 0x3f;
01485 
01486                         hash_ix &= 0x3f;
01487 
01488                         if (hash_ix > 32)
01489                                 hi_bits |= (1 << (hash_ix - 32));
01490                         else
01491                                 lo_bits |= (1 << hash_ix);
01492 
01493                         dmi = dmi->next;
01494                 }
01495                 
01496                 /* Disable individual receive. */
01497                 rec_ctrl =
01498                   (reg_eth_rw_rec_ctrl) REG_RD(eth, np->eth_inst, rw_rec_ctrl);
01499                 rec_ctrl.individual = regk_eth_no;
01500                 REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
01501         }
01502 
01503         ga_lo.tbl = (unsigned int) lo_bits;
01504         ga_hi.tbl = (unsigned int) hi_bits;
01505 
01506         REG_WR(eth, np->eth_inst, rw_ga_lo, ga_lo);
01507         REG_WR(eth, np->eth_inst, rw_ga_hi, ga_hi);
01508 }
01509 
01510 static int
01511 crisv32_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
01512 {
01513         struct mii_ioctl_data *data = if_mii(ifr);
01514         struct crisv32_ethernet_local *np = netdev_priv(dev);
01515         int old_autoneg;
01516 
01517         spin_lock(&np->lock); /* Preempt protection */
01518         switch (cmd) {
01519                 case SIOCGMIIPHY: /* Get PHY address */
01520                         data->phy_id = np->mdio_phy_addr;
01521                         break;
01522                 case SIOCGMIIREG: /* Read MII register */
01523                         data->val_out = crisv32_eth_get_mdio_reg(dev,
01524                                                                  data->reg_num);
01525                         break;
01526                 case SIOCSMIIREG: /* Write MII register */
01527                         crisv32_eth_set_mdio_reg(dev, data->reg_num,
01528                                                  data->val_in);
01529                         break;
01530                 case SET_ETH_ENABLE_LEDS:
01531                         use_network_leds = 1;
01532                         break;
01533                 case SET_ETH_DISABLE_LEDS:
01534                         use_network_leds = 0;
01535                         break;
01536                 case SET_ETH_AUTONEG:
01537                         old_autoneg = autoneg_normal;
01538                         autoneg_normal = *(int*)data;
01539                         if (autoneg_normal != old_autoneg)
01540                                 crisv32_eth_negotiate(dev);
01541                         break;
01542         default:
01543                 spin_unlock(&np->lock); /* Preempt protection */
01544                 return -EINVAL;
01545         }
01546         spin_unlock(&np->lock);
01547         return 0;
01548 }
01549 
01550 static int crisv32_eth_get_settings(struct net_device *dev,
01551                              struct ethtool_cmd *ecmd)
01552 {
01553         struct crisv32_ethernet_local *np = netdev_priv(dev);
01554         /* What about GMII and 1000xpause? not included in ethtool.h */
01555         ecmd->supported = SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII |
01556                           SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
01557                           SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
01558 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01559         ecmd->supported |= SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
01560 #endif
01561         ecmd->port = PORT_TP;
01562         ecmd->transceiver = XCVR_EXTERNAL;
01563         ecmd->phy_address = np->mdio_phy_addr;
01564         ecmd->speed = np->current_speed;
01565         ecmd->duplex = np->full_duplex;
01566         ecmd->advertising = ADVERTISED_TP;
01567 
01568         if (np->current_duplex == autoneg && np->current_speed_selection == 0)
01569                 ecmd->advertising |= ADVERTISED_Autoneg;
01570         else {
01571                 ecmd->advertising |=
01572                         ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
01573                         ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
01574 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01575                 ecmd->advertising |= ADVERTISED_1000baseT_Half |
01576                         ADVERTISED_1000baseT_Full;
01577 #endif
01578                 if (np->current_speed_selection == 10)
01579                         ecmd->advertising &= ~(ADVERTISED_100baseT_Half |
01580                                                ADVERTISED_100baseT_Full |
01581                                                ADVERTISED_1000baseT_Half |
01582                                                ADVERTISED_1000baseT_Full);
01583 
01584                 else if (np->current_speed_selection == 100)
01585                         ecmd->advertising &= ~(ADVERTISED_10baseT_Half |
01586                                                ADVERTISED_10baseT_Full |
01587                                                ADVERTISED_1000baseT_Half |
01588                                                ADVERTISED_1000baseT_Full);
01589 
01590                 else if (np->current_speed_selection == 1000)
01591                         ecmd->advertising &= ~(ADVERTISED_10baseT_Half |
01592                                                ADVERTISED_10baseT_Full |
01593                                                ADVERTISED_100baseT_Half |
01594                                                ADVERTISED_100baseT_Full);
01595 
01596                 if (np->current_duplex == half)
01597                         ecmd->advertising &= ~(ADVERTISED_10baseT_Full |
01598                                                ADVERTISED_100baseT_Full |
01599                                                ADVERTISED_1000baseT_Full);
01600                 else if (np->current_duplex == full)
01601                         ecmd->advertising &= ~(ADVERTISED_10baseT_Half |
01602                                                ADVERTISED_100baseT_Half |
01603                                                ADVERTISED_1000baseT_Half);
01604         }
01605 
01606         ecmd->autoneg = AUTONEG_ENABLE;
01607         return 0;
01608 }
01609 
01610 static int crisv32_eth_set_settings(struct net_device *dev,
01611                              struct ethtool_cmd *ecmd)
01612 {
01613         if (ecmd->autoneg == AUTONEG_ENABLE) {
01614                 crisv32_eth_set_duplex(dev, autoneg);
01615                 crisv32_eth_set_speed(dev, 0);
01616         } else {
01617                 crisv32_eth_set_duplex(dev, ecmd->duplex);
01618                 crisv32_eth_set_speed(dev, ecmd->speed);
01619         }
01620 
01621         return 0;
01622 }
01623 
01624 static void crisv32_eth_get_drvinfo(struct net_device *dev,
01625                              struct ethtool_drvinfo *info)
01626 {
01627         strncpy(info->driver, "ETRAX FS", sizeof(info->driver) - 1);
01628         strncpy(info->version, "$Revision: 1.1.1.1 $", sizeof(info->version) - 1);
01629         strncpy(info->fw_version, "N/A", sizeof(info->fw_version) - 1);
01630         strncpy(info->bus_info, "N/A", sizeof(info->bus_info) - 1);
01631 }
01632 
01633 static int crisv32_eth_nway_reset(struct net_device *dev)
01634 {
01635         struct crisv32_ethernet_local *np = netdev_priv(dev);
01636 
01637         if (np->current_duplex == autoneg && np->current_speed_selection == 0)
01638                 crisv32_eth_negotiate(dev);
01639         return 0;
01640 }
01641 
01642 static struct ethtool_ops crisv32_ethtool_ops = {
01643         .get_settings   = crisv32_eth_get_settings,
01644         .set_settings   = crisv32_eth_set_settings,
01645         .get_drvinfo    = crisv32_eth_get_drvinfo,
01646         .nway_reset     = crisv32_eth_nway_reset,
01647         .get_link       = ethtool_op_get_link,
01648 };
01649 
01650 /* Is this function really needed? Use ethtool instead? */
01651 static int
01652 crisv32_eth_set_config(struct net_device *dev, struct ifmap *map)
01653 {
01654         struct crisv32_ethernet_local *np = netdev_priv(dev);
01655 
01656         spin_lock(&np->lock); /* Preempt protection */
01657 
01658         switch(map->port) {
01659                 case IF_PORT_UNKNOWN:
01660                         /* Use autoneg */
01661                         crisv32_eth_set_speed(dev, 0);
01662                         crisv32_eth_set_duplex(dev, autoneg);
01663                         break;
01664                 case IF_PORT_10BASET:
01665                         crisv32_eth_set_speed(dev, 10);
01666                         crisv32_eth_set_duplex(dev, autoneg);
01667                         break;
01668                 case IF_PORT_100BASET:
01669                 case IF_PORT_100BASETX:
01670                         crisv32_eth_set_speed(dev, 100);
01671                         crisv32_eth_set_duplex(dev, autoneg);
01672                         break;
01673                 case IF_PORT_100BASEFX:
01674                 case IF_PORT_10BASE2:
01675                 case IF_PORT_AUI:
01676                         spin_unlock(&np->lock);
01677                         return -EOPNOTSUPP;
01678                         break;
01679                 default:
01680                         printk(KERN_ERR "%s: Invalid media selected",
01681                                dev->name);
01682                         spin_unlock(&np->lock);
01683                         return -EINVAL;
01684         }
01685         spin_unlock(&np->lock);
01686         return 0;
01687 }
01688 
01689 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01690 /* 
01691  * Switch the behaviour of the tx and rx buffers using 
01692  * external or internal memory. Usage of the internal 
01693  * memory is required for gigabit operation.
01694  */
01695 static void 
01696 crisv32_eth_switch_intmem_usage(struct net_device *dev) 
01697 {
01698         struct crisv32_ethernet_local *np = netdev_priv(dev);
01699 
01700         int i;
01701         reg_dma_rw_stat stat;
01702         reg_dma_rw_cfg cfg = {0};
01703         reg_dma_rw_intr_mask intr_mask_in = { .in_eop = regk_dma_yes };
01704         reg_dma_rw_ack_intr ack_intr = { .data = 1,.in_eop = 1 };
01705         unsigned char *intmem_tmp;
01706 
01707         /* Notify the kernel that the interface has stopped */
01708         netif_stop_queue(dev);
01709 
01710         /* Stop the receiver DMA */
01711         cfg.en = regk_dma_no;
01712         REG_WR(dma, np->dma_in_inst, rw_cfg, cfg);
01713 
01714         if (!(np->gigabit_mode)) {
01715                 /* deallocate SKBs in rx_desc */
01716                 for (i = 0; i < NBR_RX_DESC; i++)
01717                         dev_kfree_skb(np->dma_rx_descr_list[i].skb);
01718 
01719                 /* Init TX*/
01720                 for(i=0; i < NBR_INTMEM_TX_BUF; i++) {
01721                         /* Allocate internal memory */
01722                         intmem_tmp = NULL;
01723                         intmem_tmp = crisv32_intmem_alloc(MAX_MEDIA_DATA_SIZE,
01724                                                           32);
01725                         /* Check that we really got the memory */
01726                         if (intmem_tmp == NULL) {
01727                                 printk(KERN_ERR "%s: Can't allocate intmem for"
01728                                        " RX buffer nbr: %d\n", dev->name, i);
01729                                 return;
01730                         }
01731                         /* Setup the list entry */
01732                         np->tx_intmem_buf_list[i].free = 1;
01733                         np->tx_intmem_buf_list[i].buf = intmem_tmp;
01734                         np->tx_intmem_buf_list[i].next = 
01735                                 &np->tx_intmem_buf_list[i + 1];
01736                 }
01737                 /* Setup the last list entry */
01738                 np->tx_intmem_buf_list[NBR_INTMEM_TX_BUF - 1].next = 
01739                         &np->tx_intmem_buf_list[0];
01740                 /* Setup initial pointer */
01741                 np->intmem_tx_buf_active = np->tx_intmem_buf_list;
01742                 np->intmem_tx_buf_catch = np->tx_intmem_buf_list;
01743 
01744                 /* Init RX */
01745                 for (i=0; i < NBR_INTMEM_RX_DESC; i++) {
01746                         /* Allocate internal memory */
01747                         intmem_tmp = NULL;
01748                         intmem_tmp = crisv32_intmem_alloc(MAX_MEDIA_DATA_SIZE, 
01749                                                           32);
01750                         /* Check that we really got the memory */
01751                         if (intmem_tmp == NULL) {
01752                                 printk(KERN_ERR "%s: Can't allocate intmem for"
01753                                        " desc nbr: %d\n", dev->name, i);
01754                                 return;
01755                         }
01756                         /* Setup the descriptors*/
01757                         np->dma_rx_descr_list[i].skb = NULL;
01758                         np->dma_rx_descr_list[i].descr.buf = 
01759                              (void *) crisv32_intmem_virt_to_phys(intmem_tmp);
01760                         np->dma_rx_descr_list[i].descr.after =
01761                              (void *) crisv32_intmem_virt_to_phys(intmem_tmp + MAX_MEDIA_DATA_SIZE);
01762                         np->dma_rx_descr_list[i].descr.eol = 0;
01763                         np->dma_rx_descr_list[i].descr.in_eop = 0;
01764                         np->dma_rx_descr_list[i].descr.next =
01765                              (void *) virt_to_phys(&np->dma_rx_descr_list[i+1].descr);
01766                 }
01767                 /* Setup the last rx descriptor */
01768                 np->dma_rx_descr_list[NBR_INTMEM_RX_DESC - 1].descr.eol = 1;
01769                 np->dma_rx_descr_list[NBR_INTMEM_RX_DESC - 1].descr.next =
01770                    (void*) virt_to_phys(&np->dma_rx_descr_list[0].descr);
01771                 /* Initialise initial receive pointers. */
01772                 np->active_rx_desc = &np->dma_rx_descr_list[0];
01773                 np->prev_rx_desc = 
01774                         &np->dma_rx_descr_list[NBR_INTMEM_RX_DESC - 1];
01775                 np->last_rx_desc = np->prev_rx_desc;
01776 
01777                 np->gigabit_mode = 1;
01778         } else {
01779                 /* dealloc TX intmem */
01780                 for(i=0; i < NBR_INTMEM_TX_BUF; i++)
01781                         crisv32_intmem_free(np->tx_intmem_buf_list[i].buf);
01782 
01783                 /* dealloc RX intmem */
01784                 for (i=0; i < NBR_INTMEM_RX_DESC; i++)
01785                         crisv32_intmem_free(crisv32_intmem_phys_to_virt((unsigned long)np->dma_rx_descr_list[i].descr.buf));
01786 
01787                 /* Setup new rx_desc and alloc SKBs */
01788                 for (i = 0; i < NBR_RX_DESC; i++) {
01789                         struct sk_buff *skb;
01790                         
01791                         skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE);
01792                         np->dma_rx_descr_list[i].skb = skb;
01793                         np->dma_rx_descr_list[i].descr.buf =
01794                                 (char*)virt_to_phys(skb->data);
01795                         np->dma_rx_descr_list[i].descr.after =
01796                            (char*)virt_to_phys(skb->data + MAX_MEDIA_DATA_SIZE);
01797 
01798                         np->dma_rx_descr_list[i].descr.eol = 0;
01799                         np->dma_rx_descr_list[i].descr.in_eop = 0;
01800                         np->dma_rx_descr_list[i].descr.next =
01801                            (void *) virt_to_phys(&np->dma_rx_descr_list[i + 1].descr);
01802                 }
01803     
01804                 np->dma_rx_descr_list[NBR_RX_DESC - 1].descr.eol = 1;
01805                 np->dma_rx_descr_list[NBR_RX_DESC - 1].descr.next =
01806                    (void *) virt_to_phys(&np->dma_rx_descr_list[0].descr);
01807       
01808                 /* Initialise initial receive pointers. */
01809                 np->active_rx_desc = &np->dma_rx_descr_list[0];
01810                 np->prev_rx_desc = &np->dma_rx_descr_list[NBR_RX_DESC - 1];
01811                 np->last_rx_desc = np->prev_rx_desc;
01812 
01813                 np->gigabit_mode = 0;
01814         }
01815 
01816         /* Fill context descriptors. */
01817         np->ctxt_in.next = 0;        
01818         np->ctxt_in.saved_data =
01819            (dma_descr_data *) virt_to_phys(&np->dma_rx_descr_list[0].descr);
01820         np->ctxt_in.saved_data_buf = np->dma_rx_descr_list[0].descr.buf;
01821 
01822         /* Enable irq and make sure that the irqs are cleared. */
01823         REG_WR(dma, np->dma_in_inst, rw_intr_mask, intr_mask_in);
01824         REG_WR(dma, np->dma_in_inst, rw_ack_intr, ack_intr);
01825 
01826         /* Start input dma */
01827         cfg.en = regk_dma_yes;
01828         REG_WR(dma, np->dma_in_inst, rw_cfg, cfg);
01829         REG_WR(dma, np->dma_in_inst, rw_group_down,
01830                (int) virt_to_phys(&np->ctxt_in));
01831 
01832         DMA_WR_CMD(np->dma_in_inst, regk_dma_load_c);
01833         DMA_WR_CMD(np->dma_in_inst, regk_dma_load_d | regk_dma_burst);
01834 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01835   /* Reset the dma word size. */
01836         DMA_WR_CMD(np->dma_in_inst, regk_dma_set_w_size2);
01837 #endif
01838 
01839         netif_wake_queue(dev);
01840 
01841         stat = REG_RD(dma, np->dma_in_inst, rw_stat);
01842 }
01843 #endif
01844 
01845 static void
01846 crisv32_eth_negotiate(struct net_device *dev)
01847 {
01848         unsigned short data =
01849             crisv32_eth_get_mdio_reg(dev, MII_ADVERTISE);
01850         unsigned short ctrl1000 =
01851             crisv32_eth_get_mdio_reg(dev, MII_CTRL1000);
01852         struct crisv32_ethernet_local *np = netdev_priv(dev);
01853 
01854         /* Make all capabilities available */
01855         data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
01856                 ADVERTISE_100HALF | ADVERTISE_100FULL;
01857         ctrl1000 |= ADVERTISE_1000HALF | ADVERTISE_1000FULL;
01858 
01859         /* Remove the speed capabilities that we that do not want */
01860         switch (np->current_speed_selection) {
01861                 case 10 :
01862                         data &= ~(ADVERTISE_100HALF | ADVERTISE_100FULL);
01863                         ctrl1000 &= ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
01864                         break;
01865                 case 100 :
01866                         data &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL);
01867                         ctrl1000 &= ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
01868                         break;
01869                 case 1000 :
01870                         data &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
01871                                   ADVERTISE_100HALF | ADVERTISE_100FULL);
01872                         break;
01873         }
01874 
01875         /* Remove the duplex capabilites that we do not want */
01876         if (np->current_duplex == full) {
01877                 data &= ~(ADVERTISE_10HALF | ADVERTISE_100HALF);
01878                 ctrl1000 &= ~(ADVERTISE_1000HALF);
01879         }
01880         else if (np->current_duplex == half) {
01881                 data &= ~(ADVERTISE_10FULL | ADVERTISE_100FULL);
01882                 ctrl1000 &= ~(ADVERTISE_1000FULL);
01883         }
01884 
01885         crisv32_eth_set_mdio_reg(dev, MII_ADVERTISE, data);
01886 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01887         crisv32_eth_set_mdio_reg(dev, MII_CTRL1000, ctrl1000);
01888 #endif
01889 
01890         /* Renegotiate with link partner */
01891         if (autoneg_normal) {
01892           data = crisv32_eth_get_mdio_reg(dev, MII_BMCR);
01893           data |= BMCR_ANENABLE | BMCR_ANRESTART;
01894         }
01895         crisv32_eth_set_mdio_reg(dev, MII_BMCR, data);
01896 }
01897 static void
01898 crisv32_eth_check_speed(unsigned long idev)
01899 {
01900         static int led_initiated = 0;
01901         struct net_device *dev = (struct net_device *) idev;
01902         struct crisv32_ethernet_local *np = netdev_priv(dev);
01903 
01904         unsigned long data;
01905         int old_speed;
01906         unsigned long flags;
01907         
01908         BUG_ON(!np);
01909         BUG_ON(!np->transceiver);
01910 
01911         spin_lock(&np->transceiver_lock);
01912 
01913         old_speed = np->current_speed;
01914         data = crisv32_eth_get_mdio_reg(dev, MII_BMSR);
01915 
01916         if (!(data & BMSR_LSTATUS))
01917                 np->current_speed = 0;
01918         else
01919                 np->transceiver->check_speed(dev);
01920 
01921 #ifdef CONFIG_CRIS_MACH_ARTPEC3
01922         if ((old_speed != np->current_speed)
01923             && ((old_speed == 1000) || (np->current_speed == 1000))) {
01924                 /* Switch between mii and gmii */
01925                 reg_eth_rw_gen_ctrl gen_ctrl = REG_RD(eth, np->eth_inst,
01926                                                       rw_gen_ctrl);
01927                 reg_eth_rw_tr_ctrl tr_ctrl = REG_RD(eth, np->eth_inst,
01928                                                     rw_tr_ctrl);
01929                 if (old_speed == 1000) {
01930                         gen_ctrl.phy = regk_eth_mii;
01931                         gen_ctrl.gtxclk_out = regk_eth_no;
01932                         tr_ctrl.carrier_ext = regk_eth_no;
01933                 }
01934                 else {
01935                         gen_ctrl.phy = regk_eth_gmii;
01936                         gen_ctrl.gtxclk_out = regk_eth_yes;
01937                         tr_ctrl.carrier_ext = regk_eth_yes;
01938                 }
01939                 REG_WR(eth, np->eth_inst, rw_tr_ctrl, tr_ctrl);
01940                 REG_WR(eth, np->eth_inst, rw_gen_ctrl, gen_ctrl);
01941 
01942                 crisv32_eth_switch_intmem_usage(dev);
01943         }
01944 #endif
01945 
01946         spin_lock_irqsave(&np->leds->led_lock, flags);
01947         if ((old_speed != np->current_speed) || !led_initiated) {
01948                 led_initiated = 1;
01949                 np->leds->clear_led_timer.data = (unsigned long) dev;
01950                 if (np->current_speed) {
01951                         netif_carrier_on(dev);
01952                         crisv32_set_network_leds(LED_LINK, dev);
01953                 } else {
01954                         netif_carrier_off(dev);
01955                         crisv32_set_network_leds(LED_NOLINK, dev);
01956                 }
01957         }
01958         spin_unlock_irqrestore(&np->leds->led_lock, flags);
01959 
01960         /* Reinitialize the timer. */
01961         np->speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
01962         add_timer(&np->speed_timer);
01963 
01964         spin_unlock(&np->transceiver_lock);
01965 }
01966 
01967 static void
01968 crisv32_eth_set_speed(struct net_device *dev, unsigned long speed)
01969 {
01970         struct crisv32_ethernet_local *np = netdev_priv(dev);
01971         
01972         spin_lock(&np->transceiver_lock);
01973         if (np->current_speed_selection != speed) {
01974                 np->current_speed_selection = speed;
01975                 crisv32_eth_negotiate(dev);
01976         }
01977         spin_unlock(&np->transceiver_lock);
01978 }
01979 
01980 static void
01981 crisv32_eth_check_duplex(unsigned long idev)
01982 {
01983         struct net_device *dev = (struct net_device *) idev;
01984         struct crisv32_ethernet_local *np = netdev_priv(dev);
01985         reg_eth_rw_rec_ctrl rec_ctrl;
01986         int old_duplex = np->full_duplex;
01987 
01988         np->transceiver->check_duplex(dev);
01989         
01990         if (old_duplex != np->full_duplex) {
01991                 /* Duplex changed. */
01992                 rec_ctrl = (reg_eth_rw_rec_ctrl) REG_RD(eth, np->eth_inst,
01993                                                         rw_rec_ctrl);
01994                 rec_ctrl.duplex = np->full_duplex;
01995                 REG_WR(eth, np->eth_inst, rw_rec_ctrl, rec_ctrl);
01996         }
01997 
01998         /* Reinitialize the timer. */
01999         np->duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
02000         add_timer(&np->duplex_timer);
02001 }
02002 
02003 static void
02004 crisv32_eth_set_duplex(struct net_device *dev, enum duplex new_duplex)
02005 {
02006         struct crisv32_ethernet_local *np = netdev_priv(dev);
02007         spin_lock(&np->transceiver_lock);
02008         if (np->current_duplex != new_duplex) {
02009                 np->current_duplex = new_duplex;
02010                 crisv32_eth_negotiate(dev);
02011         }
02012         spin_unlock(&np->transceiver_lock);
02013 }
02014 
02015 static int
02016 crisv32_eth_probe_transceiver(struct net_device *dev)
02017 {
02018         unsigned int phyid_high;
02019         unsigned int phyid_low;
02020         unsigned int oui;
02021         struct transceiver_ops *ops = NULL;
02022         struct crisv32_ethernet_local *np = netdev_priv(dev);
02023 
02024         /* Probe MDIO physical address. */
02025         for (np->mdio_phy_addr = 0;
02026              np->mdio_phy_addr <= 31; np->mdio_phy_addr++) {
02027                 if (crisv32_eth_get_mdio_reg(dev, MII_BMSR) != 0xffff)
02028                         break;
02029         }
02030 
02031         if (np->mdio_phy_addr == 32)
02032                 return -ENODEV;
02033 
02034         /* Get manufacturer. */
02035         phyid_high = crisv32_eth_get_mdio_reg(dev, MII_PHYSID1);
02036         phyid_low = crisv32_eth_get_mdio_reg(dev, MII_PHYSID2);
02037         
02038         oui = (phyid_high << 6) | (phyid_low >> 10);
02039 
02040         for (ops = &transceivers[0]; ops->oui; ops++) {
02041                 if (ops->oui == oui)
02042                         break;
02043         }
02044 
02045         np->transceiver = ops;
02046         return 0;
02047 }
02048 
02049 static void
02050 generic_check_speed(struct net_device *dev)
02051 {
02052         unsigned long data;
02053         struct crisv32_ethernet_local *np = netdev_priv(dev);
02054 
02055         data = crisv32_eth_get_mdio_reg(dev, MII_ADVERTISE);
02056         if ((data & ADVERTISE_100FULL) ||
02057             (data & ADVERTISE_100HALF))
02058                 np->current_speed = 100;
02059         else
02060                 np->current_speed = 10; 
02061 }
02062 
02063 static void
02064 generic_check_duplex(struct net_device *dev)
02065 {
02066         unsigned long data;
02067         struct crisv32_ethernet_local *np = netdev_priv(dev);
02068         
02069         data = crisv32_eth_get_mdio_reg(dev, MII_ADVERTISE);
02070         if ((data & ADVERTISE_10FULL) ||
02071             (data & ADVERTISE_100FULL))
02072                 np->full_duplex = 1;
02073         else
02074                 np->full_duplex = 0;
02075 }
02076 
02077 static void
02078 broadcom_check_speed(struct net_device *dev)
02079 {
02080         unsigned long data;
02081         struct crisv32_ethernet_local *np = netdev_priv(dev);
02082 
02083         data = crisv32_eth_get_mdio_reg(dev, MDIO_AUX_CTRL_STATUS_REG);
02084         np->current_speed = (data & MDIO_BC_SPEED ? 100 : 10);
02085 }
02086 
02087 static void
02088 broadcom_check_duplex(struct net_device *dev)
02089 {
02090         unsigned long data;
02091         struct crisv32_ethernet_local *np = netdev_priv(dev);
02092 
02093         data = crisv32_eth_get_mdio_reg(dev, MDIO_AUX_CTRL_STATUS_REG);        
02094         np->full_duplex = (data & MDIO_BC_FULL_DUPLEX_IND) ? 1 : 0;
02095 }
02096 
02097 static void
02098 tdk_check_speed(struct net_device *dev)
02099 {
02100         unsigned long data;
02101         struct crisv32_ethernet_local *np = netdev_priv(dev);
02102         
02103         data = crisv32_eth_get_mdio_reg(dev, MDIO_TDK_DIAGNOSTIC_REG);
02104         np->current_speed = (data & MDIO_TDK_DIAGNOSTIC_RATE ? 100 : 10);
02105 }
02106 
02107 static void
02108 tdk_check_duplex(struct net_device *dev)
02109 {
02110         unsigned long data;
02111         struct crisv32_ethernet_local *np = netdev_priv(dev);
02112 
02113         data = crisv32_eth_get_mdio_reg(dev, MDIO_TDK_DIAGNOSTIC_REG);
02114         np->full_duplex = (data & MDIO_TDK_DIAGNOSTIC_DPLX) ? 1 : 0;
02115 
02116 }
02117 
02118 static void
02119 intel_check_speed(struct net_device *dev)
02120 {
02121         unsigned long data;
02122         struct crisv32_ethernet_local *np = netdev_priv(dev);
02123         data = crisv32_eth_get_mdio_reg(dev, MDIO_INT_STATUS_REG_2);
02124         np->current_speed = (data & MDIO_INT_SPEED ? 100 : 10);
02125 }
02126 
02127 static void
02128 intel_check_duplex(struct net_device *dev)
02129 {
02130         unsigned long data;
02131         struct crisv32_ethernet_local *np = netdev_priv(dev);
02132         
02133         data = crisv32_eth_get_mdio_reg(dev, MDIO_INT_STATUS_REG_2);        
02134         np->full_duplex = (data & MDIO_INT_FULL_DUPLEX_IND) ? 1 : 0;
02135 }
02136 
02137 static void
02138 national_check_speed(struct net_device *dev)
02139 {
02140         unsigned long data;
02141         struct crisv32_ethernet_local *np = netdev_priv(dev);
02142 
02143         data = crisv32_eth_get_mdio_reg(dev, MDIO_NAT_LINK_AN_REG);
02144         if (data & MDIO_NAT_1000)
02145                 np->current_speed = 1000;
02146         else if (data & MDIO_NAT_100)
02147                 np->current_speed = 100;
02148         else
02149                 np->current_speed = 10; 
02150 }
02151 
02152 static void
02153 national_check_duplex(struct net_device *dev)
02154 {
02155         unsigned long data;
02156         struct crisv32_ethernet_local *np = netdev_priv(dev);
02157         
02158         data = crisv32_eth_get_mdio_reg(dev, MDIO_NAT_LINK_AN_REG);
02159         if (data & MDIO_NAT_FULL_DUPLEX_IND)
02160                 np->full_duplex = 1;
02161         else
02162                 np->full_duplex = 0;
02163 }
02164 
02165 static void
02166 vitesse_check_speed(struct net_device *dev)
02167 {
02168         unsigned long data;
02169         struct crisv32_ethernet_local *np = netdev_priv(dev);
02170 
02171         data = crisv32_eth_get_mdio_reg(dev, MDIO_VIT_AUX_STAT);
02172         if ((data & 0x18) == MDIO_VIT_1000)
02173                 np->current_speed = 1000;
02174         else if ((data & 0x18) == MDIO_VIT_100)
02175                 np->current_speed = 100;
02176         else
02177                 np->current_speed = 10; 
02178 }
02179 
02180 static void
02181 vitesse_check_duplex(struct net_device *dev)
02182 {
02183         unsigned long data;
02184         struct crisv32_ethernet_local *np = netdev_priv(dev);
02185 
02186         data = crisv32_eth_get_mdio_reg(dev, MDIO_VIT_AUX_STAT);
02187         if (data & 0x20)
02188                 np->full_duplex = 1;
02189         else
02190                 np->full_duplex = 0;
02191 }
02192 
02193 static void
02194 crisv32_eth_reset_tranceiver(struct net_device *dev)
02195 {
02196         int i;
02197         unsigned short cmd;
02198         unsigned short data;
02199         struct crisv32_ethernet_local *np = netdev_priv(dev);
02200 
02201         data = crisv32_eth_get_mdio_reg(dev, MII_BMCR);
02202 
02203         cmd = (MDIO_START << 14)
02204                 | (MDIO_WRITE << 12)
02205                 | (np->mdio_phy_addr << 7)
02206                 | (MII_BMCR << 2);
02207 
02208         crisv32_eth_send_mdio_cmd(dev, cmd, 1);
02209 
02210         data |= 0x8000;
02211 
02212         /* Magic value is number of bits. */
02213         for (i = 15; i >= 0; i--)
02214                 crisv32_eth_send_mdio_bit(dev, GET_BIT(i, data));
02215 }
02216 
02217 static unsigned short
02218 crisv32_eth_get_mdio_reg(struct net_device *dev, unsigned char reg_num)
02219 {
02220         int i;
02221         unsigned short cmd;     /* Data to be sent on MDIO port. */
02222         unsigned short data;    /* Data read from MDIO. */
02223         struct crisv32_ethernet_local *np = netdev_priv(dev);
02224 
02225         /* Start of frame, OP Code, Physical Address, Register Address. */
02226         cmd = (MDIO_START << 14)
02227                 | (MDIO_READ << 12)
02228                 | (np->mdio_phy_addr << 7)
02229                 | (reg_num << 2);
02230 
02231         crisv32_eth_send_mdio_cmd(dev, cmd, 0);
02232 
02233         data = 0;
02234 
02235         /* Receive data. Magic value is number of bits. */
02236         for (i = 15; i >= 0; i--)
02237                 data |= (crisv32_eth_receive_mdio_bit(dev) << i);
02238 
02239         return data;
02240 }
02241 
02242 static void
02243 crisv32_eth_set_mdio_reg(struct net_device *dev, unsigned char reg, int value)
02244 {
02245         int bitCounter;
02246         unsigned short cmd;
02247         struct crisv32_ethernet_local *np = netdev_priv(dev);
02248 
02249         cmd = (MDIO_START << 14)
02250                 | (MDIO_WRITE << 12)
02251                 | (np->mdio_phy_addr << 7)
02252                 | (reg << 2);
02253         
02254         crisv32_eth_send_mdio_cmd(dev, cmd, 1);
02255 
02256         /* Data... */
02257         for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
02258                 crisv32_eth_send_mdio_bit(dev, GET_BIT(bitCounter, value));
02259         }
02260 }
02261 
02262 static void
02263 crisv32_eth_send_mdio_cmd(struct net_device *dev, unsigned short cmd,
02264                           int write_cmd)
02265 {
02266         int i;
02267         unsigned char data = 0x2;
02268 
02269         /* Preamble. Magic value is number of bits. */
02270         for (i = 31; i >= 0; i--)
02271                 crisv32_eth_send_mdio_bit(dev, GET_BIT(i, MDIO_PREAMBLE));
02272 
02273         for (i = 15; i >= 2; i--)
02274                 crisv32_eth_send_mdio_bit(dev, GET_BIT(i, cmd));
02275 
02276         /* Turnaround. */
02277         for (i = 1; i >= 0; i--)
02278                 if (write_cmd)
02279                         crisv32_eth_send_mdio_bit(dev, GET_BIT(i, data));
02280                 else
02281                         crisv32_eth_receive_mdio_bit(dev);
02282 }
02283 
02284 static void
02285 crisv32_eth_send_mdio_bit(struct net_device *dev, unsigned char bit)
02286 {
02287         struct crisv32_ethernet_local *np = netdev_priv(dev);
02288         
02289         reg_eth_rw_mgm_ctrl mgm_ctrl = {
02290                 .mdoe = regk_eth_yes,
02291                 .mdio = bit & 1
02292         };
02293 
02294         REG_WR(eth, np->eth_inst, rw_mgm_ctrl, mgm_ctrl);
02295 
02296         udelay(1);
02297 
02298         mgm_ctrl.mdc = 1;
02299         REG_WR(eth, np->eth_inst, rw_mgm_ctrl, mgm_ctrl);
02300 
02301         udelay(1);
02302 }
02303 
02304 static unsigned char
02305 crisv32_eth_receive_mdio_bit(struct net_device *dev)
02306 {
02307         reg_eth_r_stat stat;
02308         reg_eth_rw_mgm_ctrl mgm_ctrl = {0};
02309         struct crisv32_ethernet_local *np = netdev_priv(dev);
02310 
02311         REG_WR(eth, np->eth_inst, rw_mgm_ctrl, mgm_ctrl);
02312         stat = REG_RD(eth, np->eth_inst, r_stat);
02313 
02314         udelay(1);
02315 
02316         mgm_ctrl.mdc = 1;
02317         REG_WR(eth, np->eth_inst, rw_mgm_ctrl, mgm_ctrl);
02318 
02319         udelay(1);
02320         return stat.mdio;
02321 }
02322 
02323 static void
02324 crisv32_clear_network_leds(unsigned long priv)
02325 {
02326         struct net_device *dev = (struct net_device*)priv;
02327         struct crisv32_ethernet_local *np = netdev_priv(dev);
02328         unsigned long flags;
02329 
02330         spin_lock_irqsave(&np->leds->led_lock, flags);
02331         if (np->leds->led_active && time_after(jiffies, 
02332                                                np->leds->led_next_time)) {
02333                 crisv32_set_network_leds(LED_NOACTIVITY, dev);
02334 
02335                 /* Set the earliest time we may set the LED */
02336                 np->leds->led_next_time = jiffies + NET_FLASH_PAUSE;
02337                 np->leds->led_active = 0;
02338         }
02339         spin_unlock_irqrestore(&np->leds->led_lock, flags);
02340 }
02341 
02342 static void
02343 crisv32_set_network_leds(int active, struct net_device *dev)
02344 {
02345         struct crisv32_ethernet_local *np = netdev_priv(dev);
02346         int light_leds = 0;
02347 
02348         if (np->leds->ledgrp == LED_GRP_NONE)
02349           return;
02350 
02351         if (active == LED_NOLINK) {
02352                 if (dev == crisv32_dev[0])
02353                         np->leds->ifisup[0] = 0;
02354                 else
02355                         np->leds->ifisup[1] = 0;
02356         }
02357         else if (active == LED_LINK) {
02358                 if (dev == crisv32_dev[0])
02359                         np->leds->ifisup[0] = 1;
02360                 else
02361                         np->leds->ifisup[1] = 1;
02362 #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK) 
02363                 light_leds = 1;
02364         } else {
02365                 light_leds = (active == LED_NOACTIVITY);
02366 #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
02367                 light_leds = 0;
02368         } else {
02369                 light_leds = (active == LED_ACTIVITY);
02370 #else
02371 #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
02372 #endif 
02373         }
02374 
02375         if (!use_network_leds) {
02376                 // NET_LED_SET(np->leds->ledgrp,LED_OFF);
02377                 return;
02378         }
02379 
02380         if (!np->current_speed) {
02381                 /* Set link down if none of the interfaces that use this led
02382                    group is up */
02383                 if ((np->leds->ifisup[0] + np->leds->ifisup[1]) == 0) {
02384 #if defined(CONFIG_ETRAX_NETWORK_RED_ON_NO_CONNECTION)
02385                         /* Make LED red, link is down */
02386                         // NET_LED_SET(np->leds->ledgrp,LED_RED);
02387 #else
02388                         // NET_LED_SET(np->leds->ledgrp,LED_OFF);
02389 #endif  
02390                 }
02391         }
02392         else if (light_leds) {
02393                 if (np->current_speed == 10) {
02394                         // NET_LED_SET(np->leds->ledgrp,LED_ORANGE);
02395                 } else {
02396                         // NET_LED_SET(np->leds->ledgrp,LED_GREEN);
02397                 }
02398         }
02399         else {
02400                 // NET_LED_SET(np->leds->ledgrp,LED_OFF);
02401         }
02402 }
02403 
02404 #ifdef CONFIG_NET_POLL_CONTROLLER
02405 static void
02406 crisv32_netpoll(struct net_device* netdev)
02407 {
02408         crisv32rx_eth_interrupt(DMA0_INTR_VECT, netdev);
02409 }
02410 #endif
02411 
02412 #ifdef CONFIG_CPU_FREQ
02413 static int
02414 crisv32_ethernet_freq_notifier(struct notifier_block *nb,
02415                                unsigned long val, void *data)
02416 {
02417         struct cpufreq_freqs *freqs = data;
02418         if (val == CPUFREQ_POSTCHANGE) {
02419                 int i;
02420                 for (i = 0; i < 2; i++) {
02421                         struct net_device* dev = crisv32_dev[i];
02422                         unsigned short data;
02423                         if (dev == NULL)
02424                                 continue;
02425 
02426                         data = crisv32_eth_get_mdio_reg(dev, MII_BMCR);
02427                         if (freqs->new == 200000)
02428                                 data &= ~BMCR_PDOWN;
02429                         else
02430                                 data |= BMCR_PDOWN;
02431                         crisv32_eth_set_mdio_reg(dev, MII_BMCR, data);
02432                 }
02433         }
02434         return 0;
02435 }
02436 #endif
02437 
02438 /*
02439  * Must be called with the np->lock held.
02440  */
02441 static void crisv32_ethernet_bug(struct net_device *dev)
02442 {
02443         struct crisv32_ethernet_local *np = netdev_priv(dev);
02444         dma_descr_data *dma_pos;
02445         dma_descr_data *in_dma_pos;
02446         reg_dma_rw_stat stat = {0};
02447         reg_dma_rw_stat in_stat = {0};
02448         int i;
02449                 
02450         /* Get the current output dma position. */
02451         stat = REG_RD(dma, np->dma_out_inst, rw_stat);
02452 
02453 #ifdef CONFIG_CRIS_MACH_ARTPEC3
02454         if (np->gigabit_mode) {
02455                 dma_pos = crisv32_intmem_phys_to_virt(
02456                         REG_RD_INT(dma, np->dma_out_inst, rw_data));
02457                 in_dma_pos = crisv32_intmem_phys_to_virt(
02458                         REG_RD_INT(dma, np->dma_in_inst, rw_data));
02459         } else
02460 #endif
02461         {
02462                 dma_pos = phys_to_virt(REG_RD_INT(dma, np->dma_out_inst, 
02463                                                   rw_data));
02464                 in_dma_pos = phys_to_virt(REG_RD_INT(dma, np->dma_in_inst, 
02465                                                      rw_data));
02466         }
02467         in_stat = REG_RD(dma, np->dma_in_inst, rw_stat);
02468         
02469         printk("%s:\n"
02470                "stat.list_state=%x\n"
02471                "stat.mode=%x\n"
02472                "stat.stream_cmd_src=%x\n"
02473                "dma_pos=%x\n"
02474                "tx catch=%x active=%x\n"
02475                "packets=%d queue=%d\n"
02476                "intr_vect.r_vect=%x\n"
02477                "dma.r_masked_intr=%x dma.rw_ack_intr=%x "
02478                "dma.r_intr=%x dma.rw_intr_masked=%x\n"
02479                "eth.r_stat=%x\n",
02480                __func__,
02481                stat.list_state, stat.mode, stat.stream_cmd_src,
02482                (unsigned int)dma_pos,
02483                (unsigned int)&np->catch_tx_desc->descr,
02484                (unsigned int)&np->active_tx_desc->descr,
02485                np->txpackets,
02486                netif_queue_stopped(dev),
02487                REG_RD_INT(intr_vect, regi_irq, r_vect),
02488                REG_RD_INT(dma, np->dma_out_inst, r_masked_intr),
02489                REG_RD_INT(dma, np->dma_out_inst, rw_ack_intr),
02490                REG_RD_INT(dma, np->dma_out_inst, r_intr),
02491                REG_RD_INT(dma, np->dma_out_inst, rw_intr_mask),
02492                REG_RD_INT(eth, np->eth_inst, r_stat));
02493 
02494         printk("in_stat.list_state=%x\n"
02495                "in_stat.mode=%x\n"
02496                "in_stat.stream_cmd_src=%x\n"
02497                "in_dma_pos=%x\n"
02498                "rx last=%x prev=%x active=%x\n",
02499                in_stat.list_state, in_stat.mode, in_stat.stream_cmd_src,
02500                (unsigned int)in_dma_pos,
02501                (unsigned int)&np->last_rx_desc->descr,
02502                (unsigned int)&np->prev_rx_desc->descr,
02503                (unsigned int)&np->active_rx_desc->descr);
02504                
02505 
02506         printk("rx-descriptors:\n");
02507         for (i = 0; i < NBR_RX_DESC; i++) {
02508                 printk("rxdesc[%d]=0x%x\n", i, (unsigned int)
02509                        virt_to_phys(&np->dma_rx_descr_list[i].descr));
02510                 printk("rxdesc[%d].skb=0x%x\n", i,
02511                        (unsigned int)np->dma_rx_descr_list[i].skb);
02512                 printk("rxdesc[%d].buf=0x%x\n", i,
02513                        (unsigned int)np->dma_rx_descr_list[i].descr.buf);
02514                 printk("rxdesc[%d].after=0x%x\n", i,
02515                        (unsigned int)np->dma_rx_descr_list[i].descr.after);
02516                 printk("rxdesc[%d].intr=%x\n", i,
02517                        np->dma_rx_descr_list[i].descr.intr);
02518                 printk("rxdesc[%d].eol=%x\n", i,
02519                        np->dma_rx_descr_list[i].descr.eol);
02520                 printk("rxdesc[%d].out_eop=%x\n", i,
02521                        np->dma_rx_descr_list[i].descr.out_eop);
02522                 printk("rxdesc[%d].in_eop=%x\n", i,
02523                        np->dma_rx_descr_list[i].descr.in_eop);
02524                 printk("rxdesc[%d].wait=%x\n", i,
02525                        np->dma_rx_descr_list[i].descr.wait);
02526         }
02527         printk("tx-descriptors:\n");
02528         for (i = 0; i < NBR_TX_DESC; i++) {
02529                 printk("txdesc[%d]=0x%x\n", i, (unsigned int)
02530                        virt_to_phys(&np->dma_tx_descr_list[i].descr));
02531                 printk("txdesc[%d].skb=0x%x\n", i,
02532                        (unsigned int)np->dma_tx_descr_list[i].skb);
02533                 printk("txdesc[%d].buf=0x%x\n", i,
02534                        (unsigned int)np->dma_tx_descr_list[i].descr.buf);
02535                 printk("txdesc[%d].after=0x%x\n", i,
02536                        (unsigned int)np->dma_tx_descr_list[i].descr.after);
02537                 printk("txdesc[%d].intr=%x\n", i,
02538                        np->dma_tx_descr_list[i].descr.intr);
02539                 printk("txdesc[%d].eol=%x\n", i,
02540                        np->dma_tx_descr_list[i].descr.eol);
02541                 printk("txdesc[%d].out_eop=%x\n", i,
02542                        np->dma_tx_descr_list[i].descr.out_eop);
02543                 printk("txdesc[%d].in_eop=%x\n", i,
02544                        np->dma_tx_descr_list[i].descr.in_eop);
02545                 printk("txdesc[%d].wait=%x\n", i,
02546                        np->dma_tx_descr_list[i].descr.wait);
02547         }
02548 }
02549 
02550 
02551 static int
02552 crisv32_init_module(void)
02553 {
02554         return crisv32_ethernet_init();
02555 }
02556 
02557 static int __init
02558 crisv32_boot_setup(char* str)
02559 {
02560         struct sockaddr sa = {0};
02561         int i;
02562 
02563         /* Parse the colon separated Ethernet station address */
02564         for (i = 0; i <  ETH_ALEN; i++) {
02565                 unsigned int tmp;
02566                 if (sscanf(str + 3*i, "%2x", &tmp) != 1) {
02567                         printk(KERN_WARNING "Malformed station address");
02568                         return 0;
02569                 }
02570                 sa.sa_data[i] = (char)tmp;
02571         }
02572 
02573         default_mac_iface0 = sa;
02574         return 1;
02575 }
02576 
02577 __setup("crisv32_eth=", crisv32_boot_setup);
02578 
02579 module_init(crisv32_init_module);

Generated on Fri Nov 28 00:06:24 2008 for elphel by  doxygen 1.5.1