[NET] drivers/net: statistics cleanup #1 -- save memory and shrink code
[safe/jmp/linux-2.6] / drivers / net / bfin_mac.c
1 /*
2  * File:        drivers/net/bfin_mac.c
3  * Based on:
4  * Maintainer:
5  *              Bryan Wu <bryan.wu@analog.com>
6  *
7  * Original author:
8  *              Luke Yang <luke.yang@analog.com>
9  *
10  * Created:
11  * Description:
12  *
13  * Modified:
14  *              Copyright 2004-2006 Analog Devices Inc.
15  *
16  * Bugs:        Enter bugs at http://blackfin.uclinux.org/
17  *
18  * This program is free software ;  you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation ;  either version 2, or (at your option)
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY ;  without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program ;  see the file COPYING.
30  * If not, write to the Free Software Foundation,
31  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32  */
33
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/delay.h>
40 #include <linux/timer.h>
41 #include <linux/errno.h>
42 #include <linux/irq.h>
43 #include <linux/io.h>
44 #include <linux/ioport.h>
45 #include <linux/crc32.h>
46 #include <linux/device.h>
47 #include <linux/spinlock.h>
48 #include <linux/ethtool.h>
49 #include <linux/mii.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/platform_device.h>
54
55 #include <asm/dma.h>
56 #include <linux/dma-mapping.h>
57
58 #include <asm/blackfin.h>
59 #include <asm/cacheflush.h>
60 #include <asm/portmux.h>
61
62 #include "bfin_mac.h"
63
64 #define DRV_NAME        "bfin_mac"
65 #define DRV_VERSION     "1.1"
66 #define DRV_AUTHOR      "Bryan Wu, Luke Yang"
67 #define DRV_DESC        "Blackfin BF53[67] on-chip Ethernet MAC driver"
68
69 MODULE_AUTHOR(DRV_AUTHOR);
70 MODULE_LICENSE("GPL");
71 MODULE_DESCRIPTION(DRV_DESC);
72
73 #if defined(CONFIG_BFIN_MAC_USE_L1)
74 # define bfin_mac_alloc(dma_handle, size)  l1_data_sram_zalloc(size)
75 # define bfin_mac_free(dma_handle, ptr)    l1_data_sram_free(ptr)
76 #else
77 # define bfin_mac_alloc(dma_handle, size) \
78         dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
79 # define bfin_mac_free(dma_handle, ptr) \
80         dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
81 #endif
82
83 #define PKT_BUF_SZ 1580
84
85 #define MAX_TIMEOUT_CNT 500
86
87 /* pointers to maintain transmit list */
88 static struct net_dma_desc_tx *tx_list_head;
89 static struct net_dma_desc_tx *tx_list_tail;
90 static struct net_dma_desc_rx *rx_list_head;
91 static struct net_dma_desc_rx *rx_list_tail;
92 static struct net_dma_desc_rx *current_rx_ptr;
93 static struct net_dma_desc_tx *current_tx_ptr;
94 static struct net_dma_desc_tx *tx_desc;
95 static struct net_dma_desc_rx *rx_desc;
96
97 static void desc_list_free(void)
98 {
99         struct net_dma_desc_rx *r;
100         struct net_dma_desc_tx *t;
101         int i;
102 #if !defined(CONFIG_BFIN_MAC_USE_L1)
103         dma_addr_t dma_handle = 0;
104 #endif
105
106         if (tx_desc) {
107                 t = tx_list_head;
108                 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
109                         if (t) {
110                                 if (t->skb) {
111                                         dev_kfree_skb(t->skb);
112                                         t->skb = NULL;
113                                 }
114                                 t = t->next;
115                         }
116                 }
117                 bfin_mac_free(dma_handle, tx_desc);
118         }
119
120         if (rx_desc) {
121                 r = rx_list_head;
122                 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
123                         if (r) {
124                                 if (r->skb) {
125                                         dev_kfree_skb(r->skb);
126                                         r->skb = NULL;
127                                 }
128                                 r = r->next;
129                         }
130                 }
131                 bfin_mac_free(dma_handle, rx_desc);
132         }
133 }
134
135 static int desc_list_init(void)
136 {
137         int i;
138         struct sk_buff *new_skb;
139 #if !defined(CONFIG_BFIN_MAC_USE_L1)
140         /*
141          * This dma_handle is useless in Blackfin dma_alloc_coherent().
142          * The real dma handler is the return value of dma_alloc_coherent().
143          */
144         dma_addr_t dma_handle;
145 #endif
146
147         tx_desc = bfin_mac_alloc(&dma_handle,
148                                 sizeof(struct net_dma_desc_tx) *
149                                 CONFIG_BFIN_TX_DESC_NUM);
150         if (tx_desc == NULL)
151                 goto init_error;
152
153         rx_desc = bfin_mac_alloc(&dma_handle,
154                                 sizeof(struct net_dma_desc_rx) *
155                                 CONFIG_BFIN_RX_DESC_NUM);
156         if (rx_desc == NULL)
157                 goto init_error;
158
159         /* init tx_list */
160         tx_list_head = tx_list_tail = tx_desc;
161
162         for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
163                 struct net_dma_desc_tx *t = tx_desc + i;
164                 struct dma_descriptor *a = &(t->desc_a);
165                 struct dma_descriptor *b = &(t->desc_b);
166
167                 /*
168                  * disable DMA
169                  * read from memory WNR = 0
170                  * wordsize is 32 bits
171                  * 6 half words is desc size
172                  * large desc flow
173                  */
174                 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
175                 a->start_addr = (unsigned long)t->packet;
176                 a->x_count = 0;
177                 a->next_dma_desc = b;
178
179                 /*
180                  * enabled DMA
181                  * write to memory WNR = 1
182                  * wordsize is 32 bits
183                  * disable interrupt
184                  * 6 half words is desc size
185                  * large desc flow
186                  */
187                 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
188                 b->start_addr = (unsigned long)(&(t->status));
189                 b->x_count = 0;
190
191                 t->skb = NULL;
192                 tx_list_tail->desc_b.next_dma_desc = a;
193                 tx_list_tail->next = t;
194                 tx_list_tail = t;
195         }
196         tx_list_tail->next = tx_list_head;      /* tx_list is a circle */
197         tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
198         current_tx_ptr = tx_list_head;
199
200         /* init rx_list */
201         rx_list_head = rx_list_tail = rx_desc;
202
203         for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
204                 struct net_dma_desc_rx *r = rx_desc + i;
205                 struct dma_descriptor *a = &(r->desc_a);
206                 struct dma_descriptor *b = &(r->desc_b);
207
208                 /* allocate a new skb for next time receive */
209                 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
210                 if (!new_skb) {
211                         printk(KERN_NOTICE DRV_NAME
212                                ": init: low on mem - packet dropped\n");
213                         goto init_error;
214                 }
215                 skb_reserve(new_skb, 2);
216                 r->skb = new_skb;
217
218                 /*
219                  * enabled DMA
220                  * write to memory WNR = 1
221                  * wordsize is 32 bits
222                  * disable interrupt
223                  * 6 half words is desc size
224                  * large desc flow
225                  */
226                 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
227                 /* since RXDWA is enabled */
228                 a->start_addr = (unsigned long)new_skb->data - 2;
229                 a->x_count = 0;
230                 a->next_dma_desc = b;
231
232                 /*
233                  * enabled DMA
234                  * write to memory WNR = 1
235                  * wordsize is 32 bits
236                  * enable interrupt
237                  * 6 half words is desc size
238                  * large desc flow
239                  */
240                 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
241                                 NDSIZE_6 | DMAFLOW_LARGE;
242                 b->start_addr = (unsigned long)(&(r->status));
243                 b->x_count = 0;
244
245                 rx_list_tail->desc_b.next_dma_desc = a;
246                 rx_list_tail->next = r;
247                 rx_list_tail = r;
248         }
249         rx_list_tail->next = rx_list_head;      /* rx_list is a circle */
250         rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
251         current_rx_ptr = rx_list_head;
252
253         return 0;
254
255 init_error:
256         desc_list_free();
257         printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
258         return -ENOMEM;
259 }
260
261
262 /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
263
264 /* Set FER regs to MUX in Ethernet pins */
265 static int setup_pin_mux(int action)
266 {
267 #if defined(CONFIG_BFIN_MAC_RMII)
268         u16 pin_req[] = P_RMII0;
269 #else
270         u16 pin_req[] = P_MII0;
271 #endif
272
273         if (action) {
274                 if (peripheral_request_list(pin_req, DRV_NAME)) {
275                         printk(KERN_ERR DRV_NAME
276                         ": Requesting Peripherals failed\n");
277                         return -EFAULT;
278                 }
279         } else
280                 peripheral_free_list(pin_req);
281
282         return 0;
283 }
284
285 /* Wait until the previous MDC/MDIO transaction has completed */
286 static void poll_mdc_done(void)
287 {
288         int timeout_cnt = MAX_TIMEOUT_CNT;
289
290         /* poll the STABUSY bit */
291         while ((bfin_read_EMAC_STAADD()) & STABUSY) {
292                 mdelay(10);
293                 if (timeout_cnt-- < 0) {
294                         printk(KERN_ERR DRV_NAME
295                         ": wait MDC/MDIO transaction to complete timeout\n");
296                         break;
297                 }
298         }
299 }
300
301 /* Read an off-chip register in a PHY through the MDC/MDIO port */
302 static u16 read_phy_reg(u16 PHYAddr, u16 RegAddr)
303 {
304         poll_mdc_done();
305         /* read mode */
306         bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) |
307                                 SET_REGAD(RegAddr) |
308                                 STABUSY);
309         poll_mdc_done();
310
311         return (u16) bfin_read_EMAC_STADAT();
312 }
313
314 /* Write an off-chip register in a PHY through the MDC/MDIO port */
315 static void raw_write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data)
316 {
317         bfin_write_EMAC_STADAT(Data);
318
319         /* write mode */
320         bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) |
321                                 SET_REGAD(RegAddr) |
322                                 STAOP |
323                                 STABUSY);
324
325         poll_mdc_done();
326 }
327
328 static void write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data)
329 {
330         poll_mdc_done();
331         raw_write_phy_reg(PHYAddr, RegAddr, Data);
332 }
333
334 /* set up the phy */
335 static void bf537mac_setphy(struct net_device *dev)
336 {
337         u16 phydat;
338         struct bf537mac_local *lp = netdev_priv(dev);
339
340         /* Program PHY registers */
341         pr_debug("start setting up phy\n");
342
343         /* issue a reset */
344         raw_write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, 0x8000);
345
346         /* wait half a second */
347         msleep(500);
348
349         phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL);
350
351         /* advertise flow control supported */
352         phydat = read_phy_reg(lp->PhyAddr, PHYREG_ANAR);
353         phydat |= (1 << 10);
354         write_phy_reg(lp->PhyAddr, PHYREG_ANAR, phydat);
355
356         phydat = 0;
357         if (lp->Negotiate)
358                 phydat |= 0x1000;       /* enable auto negotiation */
359         else {
360                 if (lp->FullDuplex)
361                         phydat |= (1 << 8);     /* full duplex */
362                 else
363                         phydat &= (~(1 << 8));  /* half duplex */
364
365                 if (!lp->Port10)
366                         phydat |= (1 << 13);    /* 100 Mbps */
367                 else
368                         phydat &= (~(1 << 13)); /* 10 Mbps */
369         }
370
371         if (lp->Loopback)
372                 phydat |= (1 << 14);    /* enable TX->RX loopback */
373
374         write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, phydat);
375         msleep(500);
376
377         phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL);
378         /* check for SMSC PHY */
379         if ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID1) == 0x7) &&
380         ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID2) & 0xfff0) == 0xC0A0)) {
381                 /*
382                  * we have SMSC PHY so reqest interrupt
383                  * on link down condition
384                  */
385
386                 /* enable interrupts */
387                 write_phy_reg(lp->PhyAddr, 30, 0x0ff);
388         }
389 }
390
391 /**************************************************************************/
392 void setup_system_regs(struct net_device *dev)
393 {
394         int phyaddr;
395         unsigned short sysctl, phydat;
396         u32 opmode;
397         struct bf537mac_local *lp = netdev_priv(dev);
398         int count = 0;
399
400         phyaddr = lp->PhyAddr;
401
402         /* Enable PHY output */
403         if (!(bfin_read_VR_CTL() & PHYCLKOE))
404                 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
405
406         /* MDC  = 2.5 MHz */
407         sysctl = SET_MDCDIV(24);
408         /* Odd word alignment for Receive Frame DMA word */
409         /* Configure checksum support and rcve frame word alignment */
410 #if defined(BFIN_MAC_CSUM_OFFLOAD)
411         sysctl |= RXDWA | RXCKS;
412 #else
413         sysctl |= RXDWA;
414 #endif
415         bfin_write_EMAC_SYSCTL(sysctl);
416         /* auto negotiation on  */
417         /* full duplex          */
418         /* 100 Mbps             */
419         phydat = PHY_ANEG_EN | PHY_DUPLEX | PHY_SPD_SET;
420         write_phy_reg(phyaddr, PHYREG_MODECTL, phydat);
421
422         /* test if full duplex supported */
423         do {
424                 msleep(100);
425                 phydat = read_phy_reg(phyaddr, PHYREG_MODESTAT);
426                 if (count > 30) {
427                         printk(KERN_NOTICE DRV_NAME ": Link is down\n");
428                         printk(KERN_NOTICE DRV_NAME
429                                  "please check your network connection\n");
430                         break;
431                 }
432                 count++;
433         } while (!(phydat & 0x0004));
434
435         phydat = read_phy_reg(phyaddr, PHYREG_ANLPAR);
436
437         if ((phydat & 0x0100) || (phydat & 0x0040)) {
438                 opmode = FDMODE;
439         } else {
440                 opmode = 0;
441                 printk(KERN_INFO DRV_NAME
442                         ": Network is set to half duplex\n");
443         }
444
445 #if defined(CONFIG_BFIN_MAC_RMII)
446         opmode |= RMII; /* For Now only 100MBit are supported */
447 #endif
448
449         bfin_write_EMAC_OPMODE(opmode);
450
451         bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
452
453         /* Initialize the TX DMA channel registers */
454         bfin_write_DMA2_X_COUNT(0);
455         bfin_write_DMA2_X_MODIFY(4);
456         bfin_write_DMA2_Y_COUNT(0);
457         bfin_write_DMA2_Y_MODIFY(0);
458
459         /* Initialize the RX DMA channel registers */
460         bfin_write_DMA1_X_COUNT(0);
461         bfin_write_DMA1_X_MODIFY(4);
462         bfin_write_DMA1_Y_COUNT(0);
463         bfin_write_DMA1_Y_MODIFY(0);
464 }
465
466 void setup_mac_addr(u8 * mac_addr)
467 {
468         u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
469         u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
470
471         /* this depends on a little-endian machine */
472         bfin_write_EMAC_ADDRLO(addr_low);
473         bfin_write_EMAC_ADDRHI(addr_hi);
474 }
475
476 static void adjust_tx_list(void)
477 {
478         int timeout_cnt = MAX_TIMEOUT_CNT;
479
480         if (tx_list_head->status.status_word != 0
481             && current_tx_ptr != tx_list_head) {
482                 goto adjust_head;       /* released something, just return; */
483         }
484
485         /*
486          * if nothing released, check wait condition
487          * current's next can not be the head,
488          * otherwise the dma will not stop as we want
489          */
490         if (current_tx_ptr->next->next == tx_list_head) {
491                 while (tx_list_head->status.status_word == 0) {
492                         mdelay(10);
493                         if (tx_list_head->status.status_word != 0
494                             || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
495                                 goto adjust_head;
496                         }
497                         if (timeout_cnt-- < 0) {
498                                 printk(KERN_ERR DRV_NAME
499                                 ": wait for adjust tx list head timeout\n");
500                                 break;
501                         }
502                 }
503                 if (tx_list_head->status.status_word != 0) {
504                         goto adjust_head;
505                 }
506         }
507
508         return;
509
510 adjust_head:
511         do {
512                 tx_list_head->desc_a.config &= ~DMAEN;
513                 tx_list_head->status.status_word = 0;
514                 if (tx_list_head->skb) {
515                         dev_kfree_skb(tx_list_head->skb);
516                         tx_list_head->skb = NULL;
517                 } else {
518                         printk(KERN_ERR DRV_NAME
519                                ": no sk_buff in a transmitted frame!\n");
520                 }
521                 tx_list_head = tx_list_head->next;
522         } while (tx_list_head->status.status_word != 0
523                  && current_tx_ptr != tx_list_head);
524         return;
525
526 }
527
528 static int bf537mac_hard_start_xmit(struct sk_buff *skb,
529                                 struct net_device *dev)
530 {
531         struct bf537mac_local *lp = netdev_priv(dev);
532         unsigned int data;
533
534         current_tx_ptr->skb = skb;
535
536         /*
537          * Is skb->data always 16-bit aligned?
538          * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
539          */
540         if ((((unsigned int)(skb->data)) & 0x02) == 2) {
541                 /* move skb->data to current_tx_ptr payload */
542                 data = (unsigned int)(skb->data) - 2;
543                 *((unsigned short *)data) = (unsigned short)(skb->len);
544                 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
545                 /* this is important! */
546                 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
547
548         } else {
549                 *((unsigned short *)(current_tx_ptr->packet)) =
550                     (unsigned short)(skb->len);
551                 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
552                        (skb->len));
553                 current_tx_ptr->desc_a.start_addr =
554                     (unsigned long)current_tx_ptr->packet;
555                 if (current_tx_ptr->status.status_word != 0)
556                         current_tx_ptr->status.status_word = 0;
557                 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
558                                             packet,
559                                             (unsigned int)(current_tx_ptr->
560                                                            packet + skb->len) +
561                                             2);
562         }
563
564         /* enable this packet's dma */
565         current_tx_ptr->desc_a.config |= DMAEN;
566
567         /* tx dma is running, just return */
568         if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
569                 goto out;
570
571         /* tx dma is not running */
572         bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
573         /* dma enabled, read from memory, size is 6 */
574         bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
575         /* Turn on the EMAC tx */
576         bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
577
578 out:
579         adjust_tx_list();
580         current_tx_ptr = current_tx_ptr->next;
581         dev->trans_start = jiffies;
582         dev->stats.tx_packets++;
583         dev->stats.tx_bytes += (skb->len);
584         return 0;
585 }
586
587 static void bf537mac_rx(struct net_device *dev)
588 {
589         struct sk_buff *skb, *new_skb;
590         struct bf537mac_local *lp = netdev_priv(dev);
591         unsigned short len;
592
593         /* allocate a new skb for next time receive */
594         skb = current_rx_ptr->skb;
595         new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
596         if (!new_skb) {
597                 printk(KERN_NOTICE DRV_NAME
598                        ": rx: low on mem - packet dropped\n");
599                 dev->stats.rx_dropped++;
600                 goto out;
601         }
602         /* reserve 2 bytes for RXDWA padding */
603         skb_reserve(new_skb, 2);
604         current_rx_ptr->skb = new_skb;
605         current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
606
607         len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
608         skb_put(skb, len);
609         blackfin_dcache_invalidate_range((unsigned long)skb->head,
610                                          (unsigned long)skb->tail);
611
612         dev->last_rx = jiffies;
613         skb->dev = dev;
614         skb->protocol = eth_type_trans(skb, dev);
615 #if defined(BFIN_MAC_CSUM_OFFLOAD)
616         skb->csum = current_rx_ptr->status.ip_payload_csum;
617         skb->ip_summed = CHECKSUM_PARTIAL;
618 #endif
619
620         netif_rx(skb);
621         dev->stats.rx_packets++;
622         dev->stats.rx_bytes += len;
623         current_rx_ptr->status.status_word = 0x00000000;
624         current_rx_ptr = current_rx_ptr->next;
625
626 out:
627         return;
628 }
629
630 /* interrupt routine to handle rx and error signal */
631 static irqreturn_t bf537mac_interrupt(int irq, void *dev_id)
632 {
633         struct net_device *dev = dev_id;
634         int number = 0;
635
636 get_one_packet:
637         if (current_rx_ptr->status.status_word == 0) {
638                 /* no more new packet received */
639                 if (number == 0) {
640                         if (current_rx_ptr->next->status.status_word != 0) {
641                                 current_rx_ptr = current_rx_ptr->next;
642                                 goto real_rx;
643                         }
644                 }
645                 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
646                                            DMA_DONE | DMA_ERR);
647                 return IRQ_HANDLED;
648         }
649
650 real_rx:
651         bf537mac_rx(dev);
652         number++;
653         goto get_one_packet;
654 }
655
656 #ifdef CONFIG_NET_POLL_CONTROLLER
657 static void bf537mac_poll(struct net_device *dev)
658 {
659         disable_irq(IRQ_MAC_RX);
660         bf537mac_interrupt(IRQ_MAC_RX, dev);
661         enable_irq(IRQ_MAC_RX);
662 }
663 #endif                          /* CONFIG_NET_POLL_CONTROLLER */
664
665 static void bf537mac_reset(void)
666 {
667         unsigned int opmode;
668
669         opmode = bfin_read_EMAC_OPMODE();
670         opmode &= (~RE);
671         opmode &= (~TE);
672         /* Turn off the EMAC */
673         bfin_write_EMAC_OPMODE(opmode);
674 }
675
676 /*
677  * Enable Interrupts, Receive, and Transmit
678  */
679 static int bf537mac_enable(struct net_device *dev)
680 {
681         u32 opmode;
682
683         pr_debug("%s: %s\n", dev->name, __FUNCTION__);
684
685         /* Set RX DMA */
686         bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
687         bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
688
689         /* Wait MII done */
690         poll_mdc_done();
691
692         /* We enable only RX here */
693         /* ASTP   : Enable Automatic Pad Stripping
694            PR     : Promiscuous Mode for test
695            PSF    : Receive frames with total length less than 64 bytes.
696            FDMODE : Full Duplex Mode
697            LB     : Internal Loopback for test
698            RE     : Receiver Enable */
699         opmode = bfin_read_EMAC_OPMODE();
700         if (opmode & FDMODE)
701                 opmode |= PSF;
702         else
703                 opmode |= DRO | DC | PSF;
704         opmode |= RE;
705
706 #if defined(CONFIG_BFIN_MAC_RMII)
707         opmode |= RMII; /* For Now only 100MBit are supported */
708 #ifdef CONFIG_BF_REV_0_2
709         opmode |= TE;
710 #endif
711 #endif
712         /* Turn on the EMAC rx */
713         bfin_write_EMAC_OPMODE(opmode);
714
715         return 0;
716 }
717
718 /* Our watchdog timed out. Called by the networking layer */
719 static void bf537mac_timeout(struct net_device *dev)
720 {
721         pr_debug("%s: %s\n", dev->name, __FUNCTION__);
722
723         bf537mac_reset();
724
725         /* reset tx queue */
726         tx_list_tail = tx_list_head->next;
727
728         bf537mac_enable(dev);
729
730         /* We can accept TX packets again */
731         dev->trans_start = jiffies;
732         netif_wake_queue(dev);
733 }
734
735 /*
736  * This routine will, depending on the values passed to it,
737  * either make it accept multicast packets, go into
738  * promiscuous mode (for TCPDUMP and cousins) or accept
739  * a select set of multicast packets
740  */
741 static void bf537mac_set_multicast_list(struct net_device *dev)
742 {
743         u32 sysctl;
744
745         if (dev->flags & IFF_PROMISC) {
746                 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
747                 sysctl = bfin_read_EMAC_OPMODE();
748                 sysctl |= RAF;
749                 bfin_write_EMAC_OPMODE(sysctl);
750         } else if (dev->flags & IFF_ALLMULTI || dev->mc_count) {
751                 /* accept all multicast */
752                 sysctl = bfin_read_EMAC_OPMODE();
753                 sysctl |= PAM;
754                 bfin_write_EMAC_OPMODE(sysctl);
755         } else {
756                 /* clear promisc or multicast mode */
757                 sysctl = bfin_read_EMAC_OPMODE();
758                 sysctl &= ~(RAF | PAM);
759                 bfin_write_EMAC_OPMODE(sysctl);
760         }
761 }
762
763 /*
764  * this puts the device in an inactive state
765  */
766 static void bf537mac_shutdown(struct net_device *dev)
767 {
768         /* Turn off the EMAC */
769         bfin_write_EMAC_OPMODE(0x00000000);
770         /* Turn off the EMAC RX DMA */
771         bfin_write_DMA1_CONFIG(0x0000);
772         bfin_write_DMA2_CONFIG(0x0000);
773 }
774
775 /*
776  * Open and Initialize the interface
777  *
778  * Set up everything, reset the card, etc..
779  */
780 static int bf537mac_open(struct net_device *dev)
781 {
782         int retval;
783         pr_debug("%s: %s\n", dev->name, __FUNCTION__);
784
785         /*
786          * Check that the address is valid.  If its not, refuse
787          * to bring the device up.  The user must specify an
788          * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
789          */
790         if (!is_valid_ether_addr(dev->dev_addr)) {
791                 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
792                 return -EINVAL;
793         }
794
795         /* initial rx and tx list */
796         retval = desc_list_init();
797
798         if (retval)
799                 return retval;
800
801         bf537mac_setphy(dev);
802         setup_system_regs(dev);
803         bf537mac_reset();
804         bf537mac_enable(dev);
805
806         pr_debug("hardware init finished\n");
807         netif_start_queue(dev);
808         netif_carrier_on(dev);
809
810         return 0;
811 }
812
813 /*
814  *
815  * this makes the board clean up everything that it can
816  * and not talk to the outside world.   Caused by
817  * an 'ifconfig ethX down'
818  */
819 static int bf537mac_close(struct net_device *dev)
820 {
821         pr_debug("%s: %s\n", dev->name, __FUNCTION__);
822
823         netif_stop_queue(dev);
824         netif_carrier_off(dev);
825
826         /* clear everything */
827         bf537mac_shutdown(dev);
828
829         /* free the rx/tx buffers */
830         desc_list_free();
831
832         return 0;
833 }
834
835 static int __init bf537mac_probe(struct net_device *dev)
836 {
837         struct bf537mac_local *lp = netdev_priv(dev);
838         int retval;
839
840         /* Grab the MAC address in the MAC */
841         *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
842         *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
843
844         /* probe mac */
845         /*todo: how to proble? which is revision_register */
846         bfin_write_EMAC_ADDRLO(0x12345678);
847         if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
848                 pr_debug("can't detect bf537 mac!\n");
849                 retval = -ENODEV;
850                 goto err_out;
851         }
852
853         /* set the GPIO pins to Ethernet mode */
854         retval = setup_pin_mux(1);
855
856         if (retval)
857                 return retval;
858
859         /*Is it valid? (Did bootloader initialize it?) */
860         if (!is_valid_ether_addr(dev->dev_addr)) {
861                 /* Grab the MAC from the board somehow - this is done in the
862                    arch/blackfin/mach-bf537/boards/eth_mac.c */
863                 get_bf537_ether_addr(dev->dev_addr);
864         }
865
866         /* If still not valid, get a random one */
867         if (!is_valid_ether_addr(dev->dev_addr)) {
868                 random_ether_addr(dev->dev_addr);
869         }
870
871         setup_mac_addr(dev->dev_addr);
872
873         /* Fill in the fields of the device structure with ethernet values. */
874         ether_setup(dev);
875
876         dev->open = bf537mac_open;
877         dev->stop = bf537mac_close;
878         dev->hard_start_xmit = bf537mac_hard_start_xmit;
879         dev->tx_timeout = bf537mac_timeout;
880         dev->set_multicast_list = bf537mac_set_multicast_list;
881 #ifdef CONFIG_NET_POLL_CONTROLLER
882         dev->poll_controller = bf537mac_poll;
883 #endif
884
885         /* fill in some of the fields */
886         lp->version = 1;
887         lp->PhyAddr = 0x01;
888         lp->CLKIN = 25;
889         lp->FullDuplex = 0;
890         lp->Negotiate = 1;
891         lp->FlowControl = 0;
892         spin_lock_init(&lp->lock);
893
894         /* now, enable interrupts */
895         /* register irq handler */
896         if (request_irq
897             (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED,
898              "BFIN537_MAC_RX", dev)) {
899                 printk(KERN_WARNING DRV_NAME
900                        ": Unable to attach BlackFin MAC RX interrupt\n");
901                 return -EBUSY;
902         }
903
904         /* Enable PHY output early */
905         if (!(bfin_read_VR_CTL() & PHYCLKOE))
906                 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
907
908         retval = register_netdev(dev);
909         if (retval == 0) {
910                 /* now, print out the card info, in a short format.. */
911                 printk(KERN_INFO "%s: Version %s, %s\n",
912                          DRV_NAME, DRV_VERSION, DRV_DESC);
913         }
914
915 err_out:
916         return retval;
917 }
918
919 static int bfin_mac_probe(struct platform_device *pdev)
920 {
921         struct net_device *ndev;
922
923         ndev = alloc_etherdev(sizeof(struct bf537mac_local));
924         if (!ndev) {
925                 printk(KERN_WARNING DRV_NAME ": could not allocate device\n");
926                 return -ENOMEM;
927         }
928
929         SET_NETDEV_DEV(ndev, &pdev->dev);
930
931         platform_set_drvdata(pdev, ndev);
932
933         if (bf537mac_probe(ndev) != 0) {
934                 platform_set_drvdata(pdev, NULL);
935                 free_netdev(ndev);
936                 printk(KERN_WARNING DRV_NAME ": not found\n");
937                 return -ENODEV;
938         }
939
940         return 0;
941 }
942
943 static int bfin_mac_remove(struct platform_device *pdev)
944 {
945         struct net_device *ndev = platform_get_drvdata(pdev);
946
947         platform_set_drvdata(pdev, NULL);
948
949         unregister_netdev(ndev);
950
951         free_irq(IRQ_MAC_RX, ndev);
952
953         free_netdev(ndev);
954
955         setup_pin_mux(0);
956
957         return 0;
958 }
959
960 static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t state)
961 {
962         return 0;
963 }
964
965 static int bfin_mac_resume(struct platform_device *pdev)
966 {
967         return 0;
968 }
969
970 static struct platform_driver bfin_mac_driver = {
971         .probe = bfin_mac_probe,
972         .remove = bfin_mac_remove,
973         .resume = bfin_mac_resume,
974         .suspend = bfin_mac_suspend,
975         .driver = {
976                    .name = DRV_NAME,
977                    },
978 };
979
980 static int __init bfin_mac_init(void)
981 {
982         return platform_driver_register(&bfin_mac_driver);
983 }
984
985 module_init(bfin_mac_init);
986
987 static void __exit bfin_mac_cleanup(void)
988 {
989         platform_driver_unregister(&bfin_mac_driver);
990 }
991
992 module_exit(bfin_mac_cleanup);