f011aa51d4cfccba86ea1758278febb79993c2c1
[firefly-linux-kernel-4.4.55.git] / drivers / net / dm9000.c
1 /*
2  *      Davicom DM9000 Fast Ethernet driver for Linux.
3  *      Copyright (C) 1997  Sten Wang
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      as published by the Free Software Foundation; either version 2
8  *      of the License, or (at your option) any later version.
9  *
10  *      This program is distributed in the hope that it will be useful,
11  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *      GNU General Public License for more details.
14  *
15  * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
16  *
17  * Additional updates, Copyright:
18  *      Ben Dooks <ben@simtec.co.uk>
19  *      Sascha Hauer <s.hauer@pengutronix.de>
20  */
21
22 #include <linux/module.h>
23 #include <linux/ioport.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/init.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/crc32.h>
30 #include <linux/mii.h>
31 #include <linux/ethtool.h>
32 #include <linux/dm9000.h>
33 #include <linux/delay.h>
34 #include <linux/platform_device.h>
35 #include <linux/irq.h>
36
37 #include <asm/delay.h>
38 #include <asm/irq.h>
39 #include <asm/io.h>
40 #include <mach/gpio.h>
41 #include <mach/iomux.h>
42
43 #include "dm9000.h"
44
45 /* Board/System/Debug information/definition ---------------- */
46
47 #define DM9000_PHY              0x40    /* PHY address 0x01 */
48
49 #define CARDNAME        "dm9000"
50 #define DRV_VERSION     "1.31"
51
52 /*
53  * Transmit timeout, default 5 seconds.
54  */
55 static int watchdog = 5000;
56 module_param(watchdog, int, 0400);
57 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
58
59 /* DM9000 register address locking.
60  *
61  * The DM9000 uses an address register to control where data written
62  * to the data register goes. This means that the address register
63  * must be preserved over interrupts or similar calls.
64  *
65  * During interrupt and other critical calls, a spinlock is used to
66  * protect the system, but the calls themselves save the address
67  * in the address register in case they are interrupting another
68  * access to the device.
69  *
70  * For general accesses a lock is provided so that calls which are
71  * allowed to sleep are serialised so that the address register does
72  * not need to be saved. This lock also serves to serialise access
73  * to the EEPROM and PHY access registers which are shared between
74  * these two devices.
75  */
76
77 /* The driver supports the original DM9000E, and now the two newer
78  * devices, DM9000A and DM9000B.
79  */
80
81 enum dm9000_type {
82         TYPE_DM9000E,   /* original DM9000 */
83         TYPE_DM9000A,
84         TYPE_DM9000B
85 };
86
87 /* Structure/enum declaration ------------------------------- */
88 typedef struct board_info {
89
90         void __iomem    *io_addr;       /* Register I/O base address */
91         void __iomem    *io_data;       /* Data I/O address */
92         u16              irq;           /* IRQ */
93
94         u16             tx_pkt_cnt;
95         u16             queue_pkt_len;
96         u16             queue_start_addr;
97         u16             queue_ip_summed;
98         u16             dbug_cnt;
99         u8              io_mode;                /* 0:word, 2:byte */
100         u8              phy_addr;
101         u8              imr_all;
102
103         unsigned int    flags;
104         unsigned int    in_suspend :1;
105         int             debug_level;
106
107         enum dm9000_type type;
108         
109 #ifdef CONFIG_DM9000_USE_NAND_CONTROL
110     void *dev_id;
111     struct work_struct dm9k_work;
112         struct workqueue_struct *dm9000_wq;
113 #endif
114
115         void (*inblk)(void __iomem *port, void *data, int length);
116         void (*outblk)(void __iomem *port, void *data, int length);
117         void (*dumpblk)(void __iomem *port, int length);
118
119         struct device   *dev;        /* parent device */
120
121         struct resource *addr_res;   /* resources found */
122         struct resource *data_res;
123         struct resource *addr_req;   /* resources requested */
124         struct resource *data_req;
125         struct resource *irq_res;
126
127         struct mutex     addr_lock;     /* phy and eeprom access lock */
128
129         struct delayed_work phy_poll;
130         struct net_device  *ndev;
131
132         spinlock_t      lock;
133
134         struct mii_if_info mii;
135         u32             msg_enable;
136
137         int             rx_csum;
138         int             can_csum;
139         int             ip_summed;
140 } board_info_t;
141
142 /* debug code */
143 #define dm9000_dbg(db, lev, msg...) do {                \
144         if ((lev) < CONFIG_DM9000_DEBUGLEVEL &&         \
145             (lev) < db->debug_level) {                  \
146                 dev_dbg(db->dev, msg);                  \
147         }                                               \
148 } while (0)
149
150 #if defined(CONFIG_DM9000_USE_NAND_CONTROL) && defined(CONFIG_MTD_NAND_RK2818)
151 extern void rk2818_nand_status_mutex_lock(void);
152 extern  int rk2818_nand_status_mutex_trylock(void);
153 extern  void rk2818_nand_status_mutex_unlock(void);
154 #else
155 static void rk2818_nand_status_mutex_lock(void){return;}
156 static int rk2818_nand_status_mutex_trylock(void) {return 1;}
157 static void rk2818_nand_status_mutex_unlock(void) {return;}
158 #endif
159
160 static inline board_info_t *to_dm9000_board(struct net_device *dev)
161 {
162         return netdev_priv(dev);
163 }
164
165 /* DM9000 network board routine ---------------------------- */
166
167 static void
168 dm9000_reset(board_info_t * db)
169 {
170         dev_dbg(db->dev, "resetting device\n");
171
172         /* RESET device */
173         writeb(DM9000_NCR, db->io_addr);
174         udelay(200);
175         writeb(NCR_RST, db->io_data);
176         udelay(200);
177 }
178
179 /*
180  *   Read a byte from I/O port
181  */
182 static u8
183 ior(board_info_t * db, int reg)
184 {
185         writeb(reg, db->io_addr);
186         return readb(db->io_data);
187 }
188
189 /*
190  *   Write a byte to I/O port
191  */
192
193 static void
194 iow(board_info_t * db, int reg, int value)
195 {
196         writeb(reg, db->io_addr);
197         writeb(value, db->io_data);
198 }
199
200 /* routines for sending block to chip */
201
202 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
203 {
204         writesb(reg, data, count);
205 }
206
207 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
208 {
209         writesw(reg, data, (count+1) >> 1);
210 }
211
212 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
213 {
214         writesl(reg, data, (count+3) >> 2);
215 }
216
217 /* input block from chip to memory */
218
219 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
220 {
221         readsb(reg, data, count);
222 }
223
224
225 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
226 {
227         readsw(reg, data, (count+1) >> 1);
228 }
229
230 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
231 {
232         readsl(reg, data, (count+3) >> 2);
233 }
234
235 /* dump block from chip to null */
236
237 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
238 {
239         int i;
240         int tmp;
241
242         for (i = 0; i < count; i++)
243                 tmp = readb(reg);
244 }
245
246 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
247 {
248         int i;
249         int tmp;
250
251         count = (count + 1) >> 1;
252
253         for (i = 0; i < count; i++)
254                 tmp = readw(reg);
255 }
256
257 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
258 {
259         int i;
260         int tmp;
261
262         count = (count + 3) >> 2;
263
264         for (i = 0; i < count; i++)
265                 tmp = readl(reg);
266 }
267
268 /* dm9000_set_io
269  *
270  * select the specified set of io routines to use with the
271  * device
272  */
273
274 static void dm9000_set_io(struct board_info *db, int byte_width)
275 {
276         /* use the size of the data resource to work out what IO
277          * routines we want to use
278          */
279
280         switch (byte_width) {
281         case 1:
282                 db->dumpblk = dm9000_dumpblk_8bit;
283                 db->outblk  = dm9000_outblk_8bit;
284                 db->inblk   = dm9000_inblk_8bit;
285                 break;
286
287
288         case 3:
289                 dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n");
290         case 2:
291                 db->dumpblk = dm9000_dumpblk_16bit;
292                 db->outblk  = dm9000_outblk_16bit;
293                 db->inblk   = dm9000_inblk_16bit;
294                 break;
295
296         case 4:
297         default:
298                 db->dumpblk = dm9000_dumpblk_32bit;
299                 db->outblk  = dm9000_outblk_32bit;
300                 db->inblk   = dm9000_inblk_32bit;
301                 break;
302         }
303 }
304
305 static void dm9000_schedule_poll(board_info_t *db)
306 {
307         if (db->type == TYPE_DM9000E)
308                 schedule_delayed_work(&db->phy_poll, HZ * 2);
309 }
310
311 static int dm9000_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
312 {
313         board_info_t *dm = to_dm9000_board(dev);
314
315         if (!netif_running(dev))
316                 return -EINVAL;
317
318         return generic_mii_ioctl(&dm->mii, if_mii(req), cmd, NULL);
319 }
320
321 static unsigned int
322 dm9000_read_locked(board_info_t *db, int reg)
323 {
324         unsigned long flags;
325         unsigned int ret;
326         
327         rk2818_nand_status_mutex_lock();
328
329         spin_lock_irqsave(&db->lock, flags);    
330         ret = ior(db, reg);
331         spin_unlock_irqrestore(&db->lock, flags);
332
333         rk2818_nand_status_mutex_unlock();
334
335         return ret;
336 }
337
338 static int dm9000_wait_eeprom(board_info_t *db)
339 {
340         unsigned int status;
341         int timeout = 8;        /* wait max 8msec */
342
343         /* The DM9000 data sheets say we should be able to
344          * poll the ERRE bit in EPCR to wait for the EEPROM
345          * operation. From testing several chips, this bit
346          * does not seem to work.
347          *
348          * We attempt to use the bit, but fall back to the
349          * timeout (which is why we do not return an error
350          * on expiry) to say that the EEPROM operation has
351          * completed.
352          */
353
354         while (1) {
355                 status = dm9000_read_locked(db, DM9000_EPCR);
356
357                 if ((status & EPCR_ERRE) == 0)
358                         break;
359
360                 msleep(1);
361
362                 if (timeout-- < 0) {
363                         dev_dbg(db->dev, "timeout waiting EEPROM\n");
364                         break;
365                 }
366         }
367
368         return 0;
369 }
370
371 /*
372  *  Read a word data from EEPROM
373  */
374 static void
375 dm9000_read_eeprom(board_info_t *db, int offset, u8 *to)
376 {
377         unsigned long flags;
378
379         if (db->flags & DM9000_PLATF_NO_EEPROM) {
380                 to[0] = 0xff;
381                 to[1] = 0xff;
382                 return;
383         }
384
385         mutex_lock(&db->addr_lock);
386         
387         rk2818_nand_status_mutex_lock();
388
389         spin_lock_irqsave(&db->lock, flags);
390                 
391         iow(db, DM9000_EPAR, offset);
392         iow(db, DM9000_EPCR, EPCR_ERPRR);
393
394         spin_unlock_irqrestore(&db->lock, flags);
395
396         rk2818_nand_status_mutex_unlock();
397
398         dm9000_wait_eeprom(db);
399
400         /* delay for at-least 150uS */
401         msleep(1);
402         
403         rk2818_nand_status_mutex_lock();
404
405         spin_lock_irqsave(&db->lock, flags);
406         
407         iow(db, DM9000_EPCR, 0x0);
408
409         to[0] = ior(db, DM9000_EPDRL);
410         to[1] = ior(db, DM9000_EPDRH);
411
412         spin_unlock_irqrestore(&db->lock, flags);
413
414         rk2818_nand_status_mutex_unlock();
415
416         mutex_unlock(&db->addr_lock);
417 }
418
419 /*
420  * Write a word data to SROM
421  */
422 static void
423 dm9000_write_eeprom(board_info_t *db, int offset, u8 *data)
424 {
425         unsigned long flags;
426
427         if (db->flags & DM9000_PLATF_NO_EEPROM)
428                 return;
429
430         mutex_lock(&db->addr_lock);
431         
432         rk2818_nand_status_mutex_lock();
433
434         spin_lock_irqsave(&db->lock, flags);    
435         iow(db, DM9000_EPAR, offset);
436         iow(db, DM9000_EPDRH, data[1]);
437         iow(db, DM9000_EPDRL, data[0]);
438         iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
439         spin_unlock_irqrestore(&db->lock, flags);
440
441         rk2818_nand_status_mutex_unlock();
442
443         dm9000_wait_eeprom(db);
444
445         mdelay(1);      /* wait at least 150uS to clear */
446         
447         rk2818_nand_status_mutex_lock();
448
449         spin_lock_irqsave(&db->lock, flags);
450         iow(db, DM9000_EPCR, 0);
451         spin_unlock_irqrestore(&db->lock, flags);
452
453         rk2818_nand_status_mutex_unlock();
454
455         mutex_unlock(&db->addr_lock);
456 }
457
458 /* ethtool ops */
459
460 static void dm9000_get_drvinfo(struct net_device *dev,
461                                struct ethtool_drvinfo *info)
462 {
463         board_info_t *dm = to_dm9000_board(dev);
464
465         strcpy(info->driver, CARDNAME);
466         strcpy(info->version, DRV_VERSION);
467         strcpy(info->bus_info, to_platform_device(dm->dev)->name);
468 }
469
470 static u32 dm9000_get_msglevel(struct net_device *dev)
471 {
472         board_info_t *dm = to_dm9000_board(dev);
473
474         return dm->msg_enable;
475 }
476
477 static void dm9000_set_msglevel(struct net_device *dev, u32 value)
478 {
479         board_info_t *dm = to_dm9000_board(dev);
480
481         dm->msg_enable = value;
482 }
483
484 static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
485 {
486         board_info_t *dm = to_dm9000_board(dev);
487
488         mii_ethtool_gset(&dm->mii, cmd);
489         return 0;
490 }
491
492 static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
493 {
494         board_info_t *dm = to_dm9000_board(dev);
495
496         return mii_ethtool_sset(&dm->mii, cmd);
497 }
498
499 static int dm9000_nway_reset(struct net_device *dev)
500 {
501         board_info_t *dm = to_dm9000_board(dev);
502         return mii_nway_restart(&dm->mii);
503 }
504
505 static uint32_t dm9000_get_rx_csum(struct net_device *dev)
506 {
507         board_info_t *dm = to_dm9000_board(dev);
508         return dm->rx_csum;
509 }
510
511 static int dm9000_set_rx_csum(struct net_device *dev, uint32_t data)
512 {
513         board_info_t *dm = to_dm9000_board(dev);
514         unsigned long flags;
515
516         if (dm->can_csum) {
517                 dm->rx_csum = data;
518
519                 rk2818_nand_status_mutex_lock();
520                 
521                 spin_lock_irqsave(&dm->lock, flags);
522                 iow(dm, DM9000_RCSR, dm->rx_csum ? RCSR_CSUM : 0);
523                 spin_unlock_irqrestore(&dm->lock, flags);
524                 rk2818_nand_status_mutex_unlock();
525
526                 return 0;
527         }
528
529         return -EOPNOTSUPP;
530 }
531
532 static int dm9000_set_tx_csum(struct net_device *dev, uint32_t data)
533 {
534         board_info_t *dm = to_dm9000_board(dev);
535         int ret = -EOPNOTSUPP;
536
537         if (dm->can_csum)
538                 ret = ethtool_op_set_tx_csum(dev, data);
539         return ret;
540 }
541
542 static u32 dm9000_get_link(struct net_device *dev)
543 {
544         board_info_t *dm = to_dm9000_board(dev);
545         u32 ret;
546
547         if (dm->flags & DM9000_PLATF_EXT_PHY)
548                 ret = mii_link_ok(&dm->mii);
549         else
550                 ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0;
551
552         return ret;
553 }
554
555 #define DM_EEPROM_MAGIC         (0x444D394B)
556
557 static int dm9000_get_eeprom_len(struct net_device *dev)
558 {
559         return 128;
560 }
561
562 static int dm9000_get_eeprom(struct net_device *dev,
563                              struct ethtool_eeprom *ee, u8 *data)
564 {
565         board_info_t *dm = to_dm9000_board(dev);
566         int offset = ee->offset;
567         int len = ee->len;
568         int i;
569
570         /* EEPROM access is aligned to two bytes */
571
572         if ((len & 1) != 0 || (offset & 1) != 0)
573                 return -EINVAL;
574
575         if (dm->flags & DM9000_PLATF_NO_EEPROM)
576                 return -ENOENT;
577
578         ee->magic = DM_EEPROM_MAGIC;
579
580         for (i = 0; i < len; i += 2)
581                 dm9000_read_eeprom(dm, (offset + i) / 2, data + i);
582
583         return 0;
584 }
585
586 static int dm9000_set_eeprom(struct net_device *dev,
587                              struct ethtool_eeprom *ee, u8 *data)
588 {
589         board_info_t *dm = to_dm9000_board(dev);
590         int offset = ee->offset;
591         int len = ee->len;
592         int i;
593
594         /* EEPROM access is aligned to two bytes */
595
596         if ((len & 1) != 0 || (offset & 1) != 0)
597                 return -EINVAL;
598
599         if (dm->flags & DM9000_PLATF_NO_EEPROM)
600                 return -ENOENT;
601
602         if (ee->magic != DM_EEPROM_MAGIC)
603                 return -EINVAL;
604
605         for (i = 0; i < len; i += 2)
606                 dm9000_write_eeprom(dm, (offset + i) / 2, data + i);
607
608         return 0;
609 }
610
611 static const struct ethtool_ops dm9000_ethtool_ops = {
612         .get_drvinfo            = dm9000_get_drvinfo,
613         .get_settings           = dm9000_get_settings,
614         .set_settings           = dm9000_set_settings,
615         .get_msglevel           = dm9000_get_msglevel,
616         .set_msglevel           = dm9000_set_msglevel,
617         .nway_reset             = dm9000_nway_reset,
618         .get_link               = dm9000_get_link,
619         .get_eeprom_len         = dm9000_get_eeprom_len,
620         .get_eeprom             = dm9000_get_eeprom,
621         .set_eeprom             = dm9000_set_eeprom,
622         .get_rx_csum            = dm9000_get_rx_csum,
623         .set_rx_csum            = dm9000_set_rx_csum,
624         .get_tx_csum            = ethtool_op_get_tx_csum,
625         .set_tx_csum            = dm9000_set_tx_csum,
626 };
627
628 static void dm9000_show_carrier(board_info_t *db,
629                                 unsigned carrier, unsigned nsr)
630 {
631         struct net_device *ndev = db->ndev;
632         unsigned ncr = dm9000_read_locked(db, DM9000_NCR);
633
634         if (carrier)
635                 dev_info(db->dev, "%s: link up, %dMbps, %s-duplex, no LPA\n",
636                          ndev->name, (nsr & NSR_SPEED) ? 10 : 100,
637                          (ncr & NCR_FDX) ? "full" : "half");
638         else
639                 dev_info(db->dev, "%s: link down\n", ndev->name);
640 }
641
642 static void
643 dm9000_poll_work(struct work_struct *w)
644 {
645         struct delayed_work *dw = to_delayed_work(w);
646         board_info_t *db = container_of(dw, board_info_t, phy_poll);
647         struct net_device *ndev = db->ndev;
648
649         if (db->flags & DM9000_PLATF_SIMPLE_PHY &&
650             !(db->flags & DM9000_PLATF_EXT_PHY)) {
651                 unsigned nsr = dm9000_read_locked(db, DM9000_NSR);
652                 unsigned old_carrier = netif_carrier_ok(ndev) ? 1 : 0;
653                 unsigned new_carrier;
654
655                 new_carrier = (nsr & NSR_LINKST) ? 1 : 0;
656
657                 if (old_carrier != new_carrier) {
658                         if (netif_msg_link(db))
659                                 dm9000_show_carrier(db, new_carrier, nsr);
660
661                         if (!new_carrier)
662                                 netif_carrier_off(ndev);
663                         else
664                                 netif_carrier_on(ndev);
665                 }
666         } else
667                 mii_check_media(&db->mii, netif_msg_link(db), 0);
668         
669         if (netif_running(ndev))
670                 dm9000_schedule_poll(db);
671 }
672
673 /* dm9000_release_board
674  *
675  * release a board, and any mapped resources
676  */
677
678 static void
679 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
680 {
681         /* unmap our resources */
682
683         iounmap(db->io_addr);
684         iounmap(db->io_data);
685
686         /* release the resources */
687
688         release_resource(db->data_req);
689         kfree(db->data_req);
690
691         release_resource(db->addr_req);
692         kfree(db->addr_req);
693 }
694
695 static unsigned char dm9000_type_to_char(enum dm9000_type type)
696 {
697         switch (type) {
698         case TYPE_DM9000E: return 'e';
699         case TYPE_DM9000A: return 'a';
700         case TYPE_DM9000B: return 'b';
701         }
702
703         return '?';
704 }
705
706 /*
707  *  Set DM9000 multicast address
708  */
709 static void
710 dm9000_hash_table(struct net_device *dev)
711 {
712         board_info_t *db = netdev_priv(dev);
713         struct dev_mc_list *mcptr = dev->mc_list;
714         int mc_cnt = dev->mc_count;
715         int i, oft;
716         u32 hash_val;
717         u16 hash_table[4];
718         u8 rcr = RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN;
719         unsigned long flags;
720
721         dm9000_dbg(db, 1, "entering %s\n", __func__);
722         
723         rk2818_nand_status_mutex_lock();
724
725         spin_lock_irqsave(&db->lock, flags);
726         
727         for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
728                 iow(db, oft, dev->dev_addr[i]);
729
730         /* Clear Hash Table */
731         for (i = 0; i < 4; i++)
732                 hash_table[i] = 0x0;
733
734         /* broadcast address */
735         hash_table[3] = 0x8000;
736
737         if (dev->flags & IFF_PROMISC)
738                 rcr |= RCR_PRMSC;
739
740         if (dev->flags & IFF_ALLMULTI)
741                 rcr |= RCR_ALL;
742
743         /* the multicast address in Hash Table : 64 bits */
744         for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
745                 hash_val = ether_crc_le(6, mcptr->dmi_addr) & 0x3f;
746                 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
747         }
748
749         /* Write the hash table to MAC MD table */
750         for (i = 0, oft = DM9000_MAR; i < 4; i++) {
751                 iow(db, oft++, hash_table[i]);
752                 iow(db, oft++, hash_table[i] >> 8);
753         }
754
755         iow(db, DM9000_RCR, rcr);
756         spin_unlock_irqrestore(&db->lock, flags);
757         
758         rk2818_nand_status_mutex_unlock();
759 }
760
761 /*
762  * Initilize dm9000 board
763  */
764 static void
765 dm9000_init_dm9000(struct net_device *dev)
766 {
767         board_info_t *db = netdev_priv(dev);
768         unsigned int imr;
769
770         dm9000_dbg(db, 1, "entering %s\n", __func__);
771
772         /* I/O mode */
773         db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
774
775         /* Checksum mode */
776         dm9000_set_rx_csum(dev, db->rx_csum);
777
778         /* GPIO0 on pre-activate PHY */
779         iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
780         iow(db, DM9000_GPCR, GPCR_GEP_CNTL);    /* Let GPIO0 output */
781         iow(db, DM9000_GPR, 0); /* Enable PHY */
782
783         if (db->flags & DM9000_PLATF_EXT_PHY)
784                 iow(db, DM9000_NCR, NCR_EXT_PHY);
785
786         /* Program operating register */
787         iow(db, DM9000_TCR, 0);         /* TX Polling clear */
788         iow(db, DM9000_BPTR, 0x3f);     /* Less 3Kb, 200us */
789         iow(db, DM9000_FCR, 0xff);      /* Flow Control */
790         iow(db, DM9000_SMCR, 0);        /* Special Mode */
791         /* clear TX status */
792         iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
793         iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
794
795         /* Set address filter table */
796         dm9000_hash_table(dev);
797
798         imr = IMR_PAR | IMR_PTM | IMR_PRM;
799         if (db->type != TYPE_DM9000E)
800                 imr |= IMR_LNKCHNG;
801
802         db->imr_all = imr;
803
804         /* Enable TX/RX interrupt mask */
805         iow(db, DM9000_IMR, imr);
806
807         /* Init Driver variable */
808         db->tx_pkt_cnt = 0;
809         db->queue_pkt_len = 0;
810         dev->trans_start = 0;
811 }
812
813 /* Our watchdog timed out. Called by the networking layer */
814 static void dm9000_timeout(struct net_device *dev)
815 {
816         board_info_t *db = netdev_priv(dev);
817         u8 reg_save;
818         unsigned long flags;
819
820         /* Save previous register address */
821         reg_save = readb(db->io_addr);
822         
823         rk2818_nand_status_mutex_lock();
824         
825         spin_lock_irqsave(&db->lock, flags);
826
827         netif_stop_queue(dev);
828         dm9000_reset(db);
829         dm9000_init_dm9000(dev);
830         /* We can accept TX packets again */
831         dev->trans_start = jiffies;
832         netif_wake_queue(dev);
833
834         /* Restore previous register address */
835         writeb(reg_save, db->io_addr);
836         spin_unlock_irqrestore(&db->lock, flags);
837         
838         rk2818_nand_status_mutex_unlock();
839 }
840
841 static void dm9000_send_packet(struct net_device *dev,
842                                int ip_summed,
843                                u16 pkt_len)
844 {
845         board_info_t *dm = to_dm9000_board(dev);
846
847         /* The DM9000 is not smart enough to leave fragmented packets alone. */
848         if (dm->ip_summed != ip_summed) {
849                 if (ip_summed == CHECKSUM_NONE)
850                         iow(dm, DM9000_TCCR, 0);
851                 else
852                         iow(dm, DM9000_TCCR, TCCR_IP | TCCR_UDP | TCCR_TCP);
853                 dm->ip_summed = ip_summed;
854         }
855
856         /* Set TX length to DM9000 */
857         iow(dm, DM9000_TXPLL, pkt_len);
858         iow(dm, DM9000_TXPLH, pkt_len >> 8);
859
860         /* Issue TX polling command */
861         iow(dm, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
862 }
863
864 /*
865  *  Hardware start transmission.
866  *  Send a packet to media from the upper layer.
867  */
868 static int
869 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
870 {
871         unsigned long flags;
872         board_info_t *db = netdev_priv(dev);
873
874         dm9000_dbg(db, 3, "%s:\n", __func__);
875
876         if (db->tx_pkt_cnt > 1) {
877                 dev_dbg(db->dev, "netdev tx busy\n");
878                 return NETDEV_TX_BUSY;
879         }
880
881         if (!rk2818_nand_status_mutex_trylock()) {
882                 dev_dbg(db->dev, "fun:%s, nand busy\n", __func__);
883                 return NETDEV_TX_BUSY;
884         }
885         spin_lock_irqsave(&db->lock, flags);
886         
887         /* Move data to DM9000 TX RAM */
888         writeb(DM9000_MWCMD, db->io_addr);
889
890         (db->outblk)(db->io_data, skb->data, skb->len);
891         dev->stats.tx_bytes += skb->len;
892
893         db->tx_pkt_cnt++;
894         /* TX control: First packet immediately send, second packet queue */
895         if (db->tx_pkt_cnt == 1) {
896                 dm9000_send_packet(dev, skb->ip_summed, skb->len);
897         } else {
898                 /* Second packet */
899                 db->queue_pkt_len = skb->len;
900                 db->queue_ip_summed = skb->ip_summed;
901                 netif_stop_queue(dev);
902         }
903
904         spin_unlock_irqrestore(&db->lock, flags);
905         
906         rk2818_nand_status_mutex_unlock();
907
908         /* free this SKB */
909         dev_kfree_skb(skb);
910
911         return NETDEV_TX_OK;
912 }
913
914 /*
915  * DM9000 interrupt handler
916  * receive the packet to upper layer, free the transmitted packet
917  */
918
919 static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
920 {
921         int tx_status = ior(db, DM9000_NSR);    /* Got TX status */
922
923         if (tx_status & (NSR_TX2END | NSR_TX1END)) {
924                 /* One packet sent complete */
925                 db->tx_pkt_cnt--;
926                 dev->stats.tx_packets++;
927
928                 if (netif_msg_tx_done(db))
929                         dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
930
931                 /* Queue packet check & send */
932                 if (db->tx_pkt_cnt > 0)
933                         dm9000_send_packet(dev, db->queue_ip_summed,
934                                            db->queue_pkt_len);
935                 netif_wake_queue(dev);
936         }
937 }
938
939 struct dm9000_rxhdr {
940         u8      RxPktReady;
941         u8      RxStatus;
942         __le16  RxLen;
943 } __attribute__((__packed__));
944
945 /*
946  *  Received a packet and pass to upper layer
947  */
948 static void
949 dm9000_rx(struct net_device *dev)
950 {
951         board_info_t *db = netdev_priv(dev);
952         struct dm9000_rxhdr rxhdr;
953         struct sk_buff *skb;
954         u8 rxbyte, *rdptr;
955         bool GoodPacket;
956         int RxLen;
957
958         /* Check packet ready or not */
959         do {
960                 ior(db, DM9000_MRCMDX); /* Dummy read */
961
962                 udelay(1);//add by lyx@20100713,or dm9000_rx will be error in high frequence
963
964                 #if 1
965                 /* Get most updated data */             
966                 rxbyte = ior(db, DM9000_MRCMDX);        /* Dummy read */
967                 #else
968                 rxbyte = readb(db->io_data);
969                 #endif
970                 
971                 /* Status check: this byte must be 0 or 1 */
972                 if (rxbyte & DM9000_PKT_ERR) {
973                         dev_warn(db->dev, "status check fail: %d\n", rxbyte);
974                         #if 0
975                         iow(db, DM9000_RCR, 0x00);      /* Stop Device */
976                         iow(db, DM9000_IMR, IMR_PAR);   /* Stop INT request */
977                         #else
978                         dm9000_reset(db);
979                         #endif                  
980                         return;
981                 }
982
983                 if (!(rxbyte & DM9000_PKT_RDY)) {
984                         //printk("packet not ready to receive\n");
985                         return;
986                 }
987                 
988                 /* A packet ready now  & Get status/length */
989                 GoodPacket = true;
990                 writeb(DM9000_MRCMD, db->io_addr);
991
992                 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
993
994                 RxLen = le16_to_cpu(rxhdr.RxLen);
995
996                 if (netif_msg_rx_status(db))
997                         dev_dbg(db->dev, "RX: status %02x, length %04x\n",
998                                 rxhdr.RxStatus, RxLen);
999
1000                 /* Packet Status check */
1001                 if (RxLen < 0x40) {
1002                         GoodPacket = false;
1003                         if (netif_msg_rx_err(db))
1004                                 dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
1005                 }
1006
1007                 if (RxLen > DM9000_PKT_MAX) {
1008                         dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
1009                 }
1010
1011                 /* rxhdr.RxStatus is identical to RSR register. */
1012                 if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
1013                                       RSR_PLE | RSR_RWTO |
1014                                       RSR_LCS | RSR_RF)) {
1015                         GoodPacket = false;
1016                         if (rxhdr.RxStatus & RSR_FOE) {
1017                                 if (netif_msg_rx_err(db))
1018                                         dev_dbg(db->dev, "fifo error\n");
1019                                 dev->stats.rx_fifo_errors++;
1020                         }
1021                         if (rxhdr.RxStatus & RSR_CE) {
1022                                 if (netif_msg_rx_err(db))
1023                                         dev_dbg(db->dev, "crc error\n");
1024                                 dev->stats.rx_crc_errors++;
1025                         }
1026                         if (rxhdr.RxStatus & RSR_RF) {
1027                                 if (netif_msg_rx_err(db))
1028                                         dev_dbg(db->dev, "length error\n");
1029                                 dev->stats.rx_length_errors++;
1030                         }
1031                 }
1032
1033                 /* Move data from DM9000 */
1034                 if (GoodPacket
1035                     && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
1036                         skb_reserve(skb, 2);
1037                         rdptr = (u8 *) skb_put(skb, RxLen - 4);
1038
1039                         /* Read received packet from RX SRAM */
1040
1041                         (db->inblk)(db->io_data, rdptr, RxLen);
1042                         dev->stats.rx_bytes += RxLen;
1043
1044                         /* Pass to upper layer */
1045                         skb->protocol = eth_type_trans(skb, dev);
1046                         if (db->rx_csum) {
1047                                 if ((((rxbyte & 0x1c) << 3) & rxbyte) == 0)
1048                                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1049                                 else
1050                                         skb->ip_summed = CHECKSUM_NONE;
1051                         }
1052                         netif_rx(skb);
1053                         dev->stats.rx_packets++;
1054
1055                 } else {
1056                         /* need to dump the packet's data */
1057
1058                         (db->dumpblk)(db->io_data, RxLen);
1059                 }
1060         } while (rxbyte & DM9000_PKT_RDY);
1061 }
1062
1063 #ifdef CONFIG_DM9000_USE_NAND_CONTROL
1064 static void dm9000_interrupt_work(struct work_struct *work)
1065 {
1066         board_info_t *db = container_of(work, board_info_t, dm9k_work); 
1067         struct net_device *dev = db->dev_id;
1068         int int_status;
1069         unsigned long flags;
1070         u8 reg_save;
1071         
1072         //printk("entering %s\n", __FUNCTION__);
1073         
1074         /* A real interrupt coming */
1075
1076         /* holders of db->lock must always block IRQs */
1077         
1078         rk2818_nand_status_mutex_lock();
1079         
1080         spin_lock_irqsave(&db->lock, flags);
1081
1082         /* Save previous register address */
1083         reg_save = readb(db->io_addr);
1084
1085         /* Disable all interrupts */
1086         iow(db, DM9000_IMR, IMR_PAR);
1087
1088         /* Got DM9000 interrupt status */
1089         int_status = ior(db, DM9000_ISR);       /* Got ISR */
1090         iow(db, DM9000_ISR, int_status);        /* Clear ISR status */
1091
1092         if (netif_msg_intr(db))
1093                 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1094
1095         /* Received the coming packet */
1096         if (int_status & ISR_PRS)
1097                 dm9000_rx(dev);
1098
1099         /* Trnasmit Interrupt check */
1100         if (int_status & ISR_PTS)
1101                 dm9000_tx_done(dev, db);
1102
1103         if (db->type != TYPE_DM9000E) {
1104                 if (int_status & ISR_LNKCHNG) {
1105                         /* fire a link-change request */
1106                         schedule_delayed_work(&db->phy_poll, 1);
1107                 }
1108         }
1109
1110         /* Re-enable interrupt mask */
1111         iow(db, DM9000_IMR, db->imr_all);
1112
1113         /* Restore previous register address */
1114         writeb(reg_save, db->io_addr);
1115
1116         spin_unlock_irqrestore(&db->lock, flags);
1117         
1118         rk2818_nand_status_mutex_unlock();
1119
1120         enable_irq(dev->irq);
1121         
1122 }
1123
1124 static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
1125 {
1126         struct net_device *dev = dev_id;
1127         board_info_t *db = netdev_priv(dev);
1128
1129         //printk("enter : %s\n", __FUNCTION__);
1130         
1131         db->dev_id = dev_id;
1132         disable_irq_nosync(irq);
1133         queue_work(db->dm9000_wq, &(db->dm9k_work));
1134         
1135         return IRQ_HANDLED;
1136 }
1137 #else
1138 static irqreturn_t dm9000_interrupt(int irq, void *dev_id)
1139 {
1140         struct net_device *dev = dev_id;
1141         board_info_t *db = netdev_priv(dev);
1142         int int_status;
1143         unsigned long flags;
1144         u8 reg_save;
1145
1146         dm9000_dbg(db, 3, "entering %s\n", __func__);
1147
1148         /* A real interrupt coming */
1149
1150         /* holders of db->lock must always block IRQs */        
1151         spin_lock_irqsave(&db->lock, flags);
1152
1153         /* Save previous register address */
1154         reg_save = readb(db->io_addr);
1155
1156         /* Disable all interrupts */
1157         iow(db, DM9000_IMR, IMR_PAR);
1158
1159         /* Got DM9000 interrupt status */
1160         int_status = ior(db, DM9000_ISR);       /* Got ISR */
1161         iow(db, DM9000_ISR, int_status);        /* Clear ISR status */
1162
1163         if (netif_msg_intr(db))
1164                 dev_dbg(db->dev, "interrupt status %02x\n", int_status);
1165
1166         /* Received the coming packet */
1167         if (int_status & ISR_PRS)
1168                 dm9000_rx(dev);
1169
1170         /* Trnasmit Interrupt check */
1171         if (int_status & ISR_PTS)
1172                 dm9000_tx_done(dev, db);
1173
1174         if (db->type != TYPE_DM9000E) {
1175                 if (int_status & ISR_LNKCHNG) {
1176                         /* fire a link-change request */
1177                         schedule_delayed_work(&db->phy_poll, 1);
1178                 }
1179         }
1180
1181         /* Re-enable interrupt mask */
1182         iow(db, DM9000_IMR, db->imr_all);
1183
1184         /* Restore previous register address */
1185         writeb(reg_save, db->io_addr);
1186
1187         spin_unlock_irqrestore(&db->lock, flags);
1188         
1189         return IRQ_HANDLED;
1190 }
1191 #endif
1192
1193 #ifdef CONFIG_NET_POLL_CONTROLLER
1194 /*
1195  *Used by netconsole
1196  */
1197 static void dm9000_poll_controller(struct net_device *dev)
1198 {
1199         disable_irq(dev->irq);
1200         dm9000_interrupt(dev->irq, dev);
1201         enable_irq(dev->irq);
1202 }
1203 #endif
1204
1205 /*
1206  *  Open the interface.
1207  *  The interface is opened whenever "ifconfig" actives it.
1208  */
1209 static int
1210 dm9000_open(struct net_device *dev)
1211 {
1212         board_info_t *db = netdev_priv(dev);
1213         unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
1214
1215         #ifdef CONFIG_DM9000_USE_NAND_CONTROL
1216         db->dm9000_wq = create_workqueue("dm9000 wq");
1217         INIT_WORK(&(db->dm9k_work), dm9000_interrupt_work);
1218         #endif
1219         
1220         if (netif_msg_ifup(db))
1221                 dev_dbg(db->dev, "enabling %s\n", dev->name);
1222
1223         /* If there is no IRQ type specified, default to something that
1224          * may work, and tell the user that this is a problem */
1225
1226         if (irqflags == IRQF_TRIGGER_NONE)
1227                 dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
1228
1229         //irqflags |= IRQF_SHARED;
1230
1231         if (request_irq(dev->irq, dm9000_interrupt, irqflags, dev->name, dev))
1232                 return -EAGAIN;
1233
1234         /* Initialize DM9000 board */
1235         dm9000_reset(db);
1236         dm9000_init_dm9000(dev);
1237
1238         /* Init driver variable */
1239         db->dbug_cnt = 0;
1240
1241         mii_check_media(&db->mii, netif_msg_link(db), 1);
1242         netif_start_queue(dev);
1243         
1244         dm9000_schedule_poll(db);
1245
1246         return 0;
1247 }
1248
1249 /*
1250  * Sleep, either by using msleep() or if we are suspending, then
1251  * use mdelay() to sleep.
1252  */
1253 static void dm9000_msleep(board_info_t *db, unsigned int ms)
1254 {
1255         if (db->in_suspend)
1256                 mdelay(ms);
1257         else
1258                 msleep(ms);
1259 }
1260
1261 /*
1262  *   Read a word from phyxcer
1263  */
1264 static int
1265 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1266 {
1267         board_info_t *db = netdev_priv(dev);
1268         unsigned long flags;
1269         unsigned int reg_save;
1270         int ret;
1271
1272         mutex_lock(&db->addr_lock);
1273
1274         rk2818_nand_status_mutex_lock();
1275
1276         spin_lock_irqsave(&db->lock,flags);
1277         
1278         /* Save previous register address */
1279         reg_save = readb(db->io_addr);
1280
1281         /* Fill the phyxcer register into REG_0C */
1282         iow(db, DM9000_EPAR, DM9000_PHY | reg);
1283
1284         iow(db, DM9000_EPCR, EPCR_ERPRR | EPCR_EPOS);   /* Issue phyxcer read command */
1285
1286         writeb(reg_save, db->io_addr);
1287         spin_unlock_irqrestore(&db->lock,flags);
1288         
1289         rk2818_nand_status_mutex_unlock();
1290
1291         dm9000_msleep(db, 1);           /* Wait read complete */
1292
1293         rk2818_nand_status_mutex_lock();
1294
1295         spin_lock_irqsave(&db->lock,flags);
1296         
1297         reg_save = readb(db->io_addr);
1298
1299         iow(db, DM9000_EPCR, 0x0);      /* Clear phyxcer read command */
1300
1301         /* The read data keeps on REG_0D & REG_0E */
1302         ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1303
1304         /* restore the previous address */
1305         writeb(reg_save, db->io_addr);
1306         spin_unlock_irqrestore(&db->lock,flags);
1307         
1308         rk2818_nand_status_mutex_unlock();
1309
1310         mutex_unlock(&db->addr_lock);
1311
1312         dm9000_dbg(db, 5, "phy_read[%02x] -> %04x\n", reg, ret);
1313         return ret;
1314 }
1315
1316 /*
1317  *   Write a word to phyxcer
1318  */
1319 static void
1320 dm9000_phy_write(struct net_device *dev,
1321                  int phyaddr_unused, int reg, int value)
1322 {
1323         board_info_t *db = netdev_priv(dev);
1324         unsigned long flags;
1325         unsigned long reg_save;
1326
1327         dm9000_dbg(db, 5, "phy_write[%02x] = %04x\n", reg, value);
1328         mutex_lock(&db->addr_lock);
1329
1330         rk2818_nand_status_mutex_lock();
1331
1332         spin_lock_irqsave(&db->lock,flags);
1333         
1334         /* Save previous register address */
1335         reg_save = readb(db->io_addr);
1336
1337         /* Fill the phyxcer register into REG_0C */
1338         iow(db, DM9000_EPAR, DM9000_PHY | reg);
1339
1340         /* Fill the written data into REG_0D & REG_0E */
1341         iow(db, DM9000_EPDRL, value);
1342         iow(db, DM9000_EPDRH, value >> 8);
1343
1344         iow(db, DM9000_EPCR, EPCR_EPOS | EPCR_ERPRW);   /* Issue phyxcer write command */
1345
1346         writeb(reg_save, db->io_addr);
1347         spin_unlock_irqrestore(&db->lock, flags);
1348         
1349         rk2818_nand_status_mutex_unlock();
1350
1351         dm9000_msleep(db, 1);           /* Wait write complete */
1352
1353         rk2818_nand_status_mutex_lock();
1354
1355         spin_lock_irqsave(&db->lock,flags);
1356         
1357         reg_save = readb(db->io_addr);
1358
1359         iow(db, DM9000_EPCR, 0x0);      /* Clear phyxcer write command */
1360
1361         /* restore the previous address */
1362         writeb(reg_save, db->io_addr);
1363
1364         spin_unlock_irqrestore(&db->lock, flags);
1365         
1366         rk2818_nand_status_mutex_unlock();
1367         mutex_unlock(&db->addr_lock);
1368 }
1369
1370 static void
1371 dm9000_shutdown(struct net_device *dev)
1372 {
1373         board_info_t *db = netdev_priv(dev);
1374
1375         /* RESET device */
1376         dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
1377         iow(db, DM9000_GPR, 0x01);      /* Power-Down PHY */
1378         iow(db, DM9000_IMR, IMR_PAR);   /* Disable all interrupt */
1379         iow(db, DM9000_RCR, 0x00);      /* Disable RX */
1380 }
1381
1382 /*
1383  * Stop the interface.
1384  * The interface is stopped when it is brought.
1385  */
1386 static int
1387 dm9000_stop(struct net_device *ndev)
1388 {
1389         board_info_t *db = netdev_priv(ndev);
1390
1391         if (netif_msg_ifdown(db))
1392                 dev_dbg(db->dev, "shutting down %s\n", ndev->name);
1393
1394         cancel_delayed_work_sync(&db->phy_poll);
1395
1396         netif_stop_queue(ndev);
1397         netif_carrier_off(ndev);
1398
1399         /* free interrupt */
1400         free_irq(ndev->irq, ndev);
1401
1402         dm9000_shutdown(ndev);
1403
1404         #ifdef CONFIG_DM9000_USE_NAND_CONTROL
1405         destroy_workqueue(db->dm9000_wq);
1406         #endif
1407
1408         return 0;
1409 }
1410
1411 static const struct net_device_ops dm9000_netdev_ops = {
1412         .ndo_open               = dm9000_open,
1413         .ndo_stop               = dm9000_stop,
1414         .ndo_start_xmit         = dm9000_start_xmit,
1415         .ndo_tx_timeout         = dm9000_timeout,
1416         .ndo_set_multicast_list = dm9000_hash_table,
1417         .ndo_do_ioctl           = dm9000_ioctl,
1418         .ndo_change_mtu         = eth_change_mtu,
1419         .ndo_validate_addr      = eth_validate_addr,
1420         .ndo_set_mac_address    = eth_mac_addr,
1421 #ifdef CONFIG_NET_POLL_CONTROLLER
1422         .ndo_poll_controller    = dm9000_poll_controller,
1423 #endif
1424 };
1425
1426 /*
1427  * Search DM9000 board, allocate space and register it
1428  */
1429 static int __devinit
1430 dm9000_probe(struct platform_device *pdev)
1431 {
1432         struct dm9000_plat_data *pdata = pdev->dev.platform_data;
1433         struct board_info *db;  /* Point a board information structure */
1434         struct net_device *ndev;
1435         const unsigned char *mac_src;
1436         int ret = 0;
1437         int iosize;
1438         int i;
1439         u32 id_val;
1440
1441         /* Init network device */
1442         ndev = alloc_etherdev(sizeof(struct board_info));
1443         if (!ndev) {
1444                 dev_err(&pdev->dev, "could not allocate device.\n");
1445                 return -ENOMEM;
1446         }
1447
1448         SET_NETDEV_DEV(ndev, &pdev->dev);
1449
1450         dev_dbg(&pdev->dev, "dm9000_probe()\n");
1451
1452         /* setup board info structure */
1453         db = netdev_priv(ndev);
1454
1455         db->dev = &pdev->dev;
1456         db->ndev = ndev;
1457
1458         spin_lock_init(&db->lock);
1459         mutex_init(&db->addr_lock);
1460
1461         INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
1462
1463         db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1464         db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1465         db->irq_res  = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1466
1467         if (db->addr_res == NULL || db->data_res == NULL ||
1468             db->irq_res == NULL) {
1469                 dev_err(db->dev, "insufficient resources\n");
1470                 ret = -ENOENT;
1471                 goto out;
1472         }
1473
1474         iosize = resource_size(db->addr_res);
1475         db->addr_req = request_mem_region(db->addr_res->start, iosize,
1476                                           pdev->name);
1477
1478         if (db->addr_req == NULL) {
1479                 dev_err(db->dev, "cannot claim address reg area\n");
1480                 ret = -EIO;
1481                 goto out;
1482         }
1483
1484         db->io_addr = ioremap(db->addr_res->start, iosize);
1485
1486         if (db->io_addr == NULL) {
1487                 dev_err(db->dev, "failed to ioremap address reg\n");
1488                 ret = -EINVAL;
1489                 goto out;
1490         }
1491
1492         iosize = resource_size(db->data_res);
1493         db->data_req = request_mem_region(db->data_res->start, iosize,
1494                                           pdev->name);
1495
1496         if (db->data_req == NULL) {
1497                 dev_err(db->dev, "cannot claim data reg area\n");
1498                 ret = -EIO;
1499                 goto out;
1500         }
1501
1502         db->io_data = ioremap(db->data_res->start, iosize);
1503
1504         if (db->io_data == NULL) {
1505                 dev_err(db->dev, "failed to ioremap data reg\n");
1506                 ret = -EINVAL;
1507                 goto out;
1508         }
1509
1510         /* fill in parameters for net-dev structure */
1511         ndev->base_addr = (unsigned long)db->io_addr;
1512
1513         //io init for dm9000 , modify by lyx@20100809
1514         if (pdata && pdata->io_init) {
1515                 if (pdata->io_init()) {
1516                         ret = -EINVAL;
1517                         goto out;
1518                 }
1519         }
1520         if (pdata && pdata->get_irq_num) {
1521                 ndev->irq = pdata->get_irq_num();
1522         }
1523         else {
1524                 ndev->irq = db->irq_res->start;
1525         }
1526         
1527         /* ensure at least we have a default set of IO routines */
1528         dm9000_set_io(db, iosize);
1529
1530         /* check to see if anything is being over-ridden */
1531         if (pdata != NULL) {
1532                 /* check to see if the driver wants to over-ride the
1533                  * default IO width */
1534
1535                 if (pdata->flags & DM9000_PLATF_8BITONLY)
1536                         dm9000_set_io(db, 1);
1537
1538                 if (pdata->flags & DM9000_PLATF_16BITONLY)
1539                         dm9000_set_io(db, 2);
1540
1541                 if (pdata->flags & DM9000_PLATF_32BITONLY)
1542                         dm9000_set_io(db, 4);
1543
1544                 /* check to see if there are any IO routine
1545                  * over-rides */
1546
1547                 if (pdata->inblk != NULL)
1548                         db->inblk = pdata->inblk;
1549
1550                 if (pdata->outblk != NULL)
1551                         db->outblk = pdata->outblk;
1552
1553                 if (pdata->dumpblk != NULL)
1554                         db->dumpblk = pdata->dumpblk;
1555
1556                 db->flags = pdata->flags;
1557         }
1558
1559 #ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
1560         db->flags |= DM9000_PLATF_SIMPLE_PHY;
1561 #endif
1562
1563         dm9000_reset(db);
1564
1565         /* try multiple times, DM9000 sometimes gets the read wrong */
1566         for (i = 0; i < 8; i++) {
1567                 id_val  = ior(db, DM9000_VIDL);
1568                 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
1569                 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
1570                 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
1571
1572                 if (id_val == DM9000_ID)
1573                         break;
1574                 dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
1575         }
1576
1577         if (id_val != DM9000_ID) {
1578                 dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
1579                 ret = -ENODEV;
1580                 goto out;
1581         }
1582
1583         /* Identify what type of DM9000 we are working on */
1584
1585         id_val = ior(db, DM9000_CHIPR);
1586         dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
1587
1588         switch (id_val) {
1589         case CHIPR_DM9000A:
1590                 db->type = TYPE_DM9000A;
1591                 break;
1592         case CHIPR_DM9000B:
1593                 db->type = TYPE_DM9000B;
1594                 break;
1595         default:
1596                 dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
1597                 db->type = TYPE_DM9000E;
1598         }
1599
1600         /* dm9000a/b are capable of hardware checksum offload */
1601         if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
1602                 db->can_csum = 1;
1603                 db->rx_csum = 1;
1604                 ndev->features |= NETIF_F_IP_CSUM;
1605         }
1606
1607         /* from this point we assume that we have found a DM9000 */
1608
1609         /* driver system function */
1610         ether_setup(ndev);
1611
1612         ndev->netdev_ops        = &dm9000_netdev_ops;
1613         ndev->watchdog_timeo    = msecs_to_jiffies(watchdog);
1614         ndev->ethtool_ops       = &dm9000_ethtool_ops;
1615
1616         db->msg_enable       = NETIF_MSG_LINK;
1617         db->mii.phy_id_mask  = 0x1f;
1618         db->mii.reg_num_mask = 0x1f;
1619         db->mii.force_media  = 0;
1620         db->mii.full_duplex  = 0;
1621         db->mii.dev          = ndev;
1622         db->mii.mdio_read    = dm9000_phy_read;
1623         db->mii.mdio_write   = dm9000_phy_write;
1624
1625         mac_src = "eeprom";
1626
1627         /* try reading the node address from the attached EEPROM */
1628         for (i = 0; i < 6; i += 2)
1629                 dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
1630
1631         if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
1632                 mac_src = "platform data";
1633                 memcpy(ndev->dev_addr, pdata->dev_addr, 6);
1634         }
1635
1636         if (!is_valid_ether_addr(ndev->dev_addr)) {
1637                 /* try reading from mac */
1638                 
1639                 mac_src = "chip";
1640                 for (i = 0; i < 6; i++)
1641                         ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
1642         }
1643
1644         if (!is_valid_ether_addr(ndev->dev_addr))
1645                 dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
1646                          "set using ifconfig\n", ndev->name);
1647
1648         platform_set_drvdata(pdev, ndev);
1649         ret = register_netdev(ndev);
1650
1651         if (ret == 0)
1652                 printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
1653                        ndev->name, dm9000_type_to_char(db->type),
1654                        db->io_addr, db->io_data, ndev->irq,
1655                        ndev->dev_addr, mac_src);
1656
1657         dm9000_shutdown(ndev);//add by lyx@20100713, reduce power consume
1658
1659         return 0;
1660
1661 out:
1662         dev_err(db->dev, "not found (%d).\n", ret);
1663
1664         dm9000_release_board(pdev, db);
1665         free_netdev(ndev);
1666
1667         return ret;
1668 }
1669
1670 static int
1671 dm9000_drv_suspend(struct device *dev)
1672 {
1673         struct platform_device *pdev = to_platform_device(dev);
1674         struct net_device *ndev = platform_get_drvdata(pdev);
1675         board_info_t *db;
1676
1677         if (ndev) {
1678                 db = netdev_priv(ndev);
1679                 db->in_suspend = 1;
1680
1681                 if (netif_running(ndev)) {
1682                         netif_device_detach(ndev);
1683                         dm9000_shutdown(ndev);
1684                 }
1685         }
1686         return 0;
1687 }
1688
1689 static int
1690 dm9000_drv_resume(struct device *dev)
1691 {
1692         struct platform_device *pdev = to_platform_device(dev);
1693         struct net_device *ndev = platform_get_drvdata(pdev);
1694         board_info_t *db = netdev_priv(ndev);
1695
1696         if (ndev) {
1697
1698                 if (netif_running(ndev)) {
1699                         dm9000_reset(db);
1700                         dm9000_init_dm9000(ndev);
1701
1702                         netif_device_attach(ndev);
1703                 }
1704
1705                 db->in_suspend = 0;
1706         }
1707         return 0;
1708 }
1709
1710 static struct dev_pm_ops dm9000_drv_pm_ops = {
1711         .suspend        = dm9000_drv_suspend,
1712         .resume         = dm9000_drv_resume,
1713 };
1714
1715 static int __devexit
1716 dm9000_drv_remove(struct platform_device *pdev)
1717 {
1718         struct net_device *ndev = platform_get_drvdata(pdev);
1719         struct dm9000_plat_data *pdata = pdev->dev.platform_data;
1720
1721         //deinit io for dm9000
1722         if (pdata && pdata->io_deinit)
1723                 pdata->io_deinit();
1724
1725         platform_set_drvdata(pdev, NULL);
1726
1727         unregister_netdev(ndev);
1728         dm9000_release_board(pdev, (board_info_t *) netdev_priv(ndev));
1729         free_netdev(ndev);              /* free device structure */
1730
1731         dev_dbg(&pdev->dev, "released and freed device\n");
1732         return 0;
1733 }
1734
1735 static struct platform_driver dm9000_driver = {
1736         .driver = {
1737                 .name    = "dm9000",
1738                 .owner   = THIS_MODULE,
1739                 .pm      = &dm9000_drv_pm_ops,
1740         },
1741         .probe   = dm9000_probe,
1742         .remove  = __devexit_p(dm9000_drv_remove),
1743 };
1744
1745 static int __init
1746 dm9000_init(void)
1747 {
1748         printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
1749
1750         return platform_driver_register(&dm9000_driver);
1751 }
1752
1753 static void __exit
1754 dm9000_cleanup(void)
1755 {
1756         platform_driver_unregister(&dm9000_driver);
1757 }
1758
1759 module_init(dm9000_init);
1760 module_exit(dm9000_cleanup);
1761
1762 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1763 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1764 MODULE_LICENSE("GPL");
1765 MODULE_ALIAS("platform:dm9000");