]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/dm9000.c
[NET] drivers/net: statistics cleanup #1 -- save memory and shrink code
[net-next-2.6.git] / drivers / net / dm9000.c
1 /*
2  *   dm9000.c: Version 1.2 03/18/2003
3  *
4  *         A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
5  *      Copyright (C) 1997  Sten Wang
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version 2
10  *      of the License, or (at your option) any later version.
11  *
12  *      This program is distributed in the hope that it will be useful,
13  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *      GNU General Public License for more details.
16  *
17  *   (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
18  *
19  * V0.11        06/20/2001      REG_0A bit3=1, default enable BP with DA match
20  *      06/22/2001      Support DM9801 progrmming
21  *                      E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
22  *                      E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
23  *                              R17 = (R17 & 0xfff0) | NF + 3
24  *                      E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
25  *                              R17 = (R17 & 0xfff0) | NF
26  *
27  * v1.00                modify by simon 2001.9.5
28  *                         change for kernel 2.4.x
29  *
30  * v1.1   11/09/2001            fix force mode bug
31  *
32  * v1.2   03/18/2003       Weilun Huang <weilun_huang@davicom.com.tw>:
33  *                      Fixed phy reset.
34  *                      Added tx/rx 32 bit mode.
35  *                      Cleaned up for kernel merge.
36  *
37  *        03/03/2004    Sascha Hauer <s.hauer@pengutronix.de>
38  *                      Port to 2.6 kernel
39  *
40  *        24-Sep-2004   Ben Dooks <ben@simtec.co.uk>
41  *                      Cleanup of code to remove ifdefs
42  *                      Allowed platform device data to influence access width
43  *                      Reformatting areas of code
44  *
45  *        17-Mar-2005   Sascha Hauer <s.hauer@pengutronix.de>
46  *                      * removed 2.4 style module parameters
47  *                      * removed removed unused stat counter and fixed
48  *                        net_device_stats
49  *                      * introduced tx_timeout function
50  *                      * reworked locking
51  *
52  *        01-Jul-2005   Ben Dooks <ben@simtec.co.uk>
53  *                      * fixed spinlock call without pointer
54  *                      * ensure spinlock is initialised
55  */
56
57 #include <linux/module.h>
58 #include <linux/ioport.h>
59 #include <linux/netdevice.h>
60 #include <linux/etherdevice.h>
61 #include <linux/init.h>
62 #include <linux/skbuff.h>
63 #include <linux/spinlock.h>
64 #include <linux/crc32.h>
65 #include <linux/mii.h>
66 #include <linux/dm9000.h>
67 #include <linux/delay.h>
68 #include <linux/platform_device.h>
69
70 #include <asm/delay.h>
71 #include <asm/irq.h>
72 #include <asm/io.h>
73
74 #include "dm9000.h"
75
76 /* Board/System/Debug information/definition ---------------- */
77
78 #define DM9000_PHY              0x40    /* PHY address 0x01 */
79
80 #define CARDNAME "dm9000"
81 #define PFX CARDNAME ": "
82
83 #define DM9000_TIMER_WUT  jiffies+(HZ*2)        /* timer wakeup time : 2 second */
84
85 #define DM9000_DEBUG 0
86
87 #if DM9000_DEBUG > 2
88 #define PRINTK3(args...)  printk(CARDNAME ": " args)
89 #else
90 #define PRINTK3(args...)  do { } while(0)
91 #endif
92
93 #if DM9000_DEBUG > 1
94 #define PRINTK2(args...)  printk(CARDNAME ": " args)
95 #else
96 #define PRINTK2(args...)  do { } while(0)
97 #endif
98
99 #if DM9000_DEBUG > 0
100 #define PRINTK1(args...)  printk(CARDNAME ": " args)
101 #define PRINTK(args...)   printk(CARDNAME ": " args)
102 #else
103 #define PRINTK1(args...)  do { } while(0)
104 #define PRINTK(args...)   printk(KERN_DEBUG args)
105 #endif
106
107 #ifdef CONFIG_BLACKFIN
108 #define readsb  insb
109 #define readsw  insw
110 #define readsl  insl
111 #define writesb outsb
112 #define writesw outsw
113 #define writesl outsl
114 #define DM9000_IRQ_FLAGS        (IRQF_SHARED | IRQF_TRIGGER_HIGH)
115 #else
116 #define DM9000_IRQ_FLAGS        IRQF_SHARED
117 #endif
118
119 /*
120  * Transmit timeout, default 5 seconds.
121  */
122 static int watchdog = 5000;
123 module_param(watchdog, int, 0400);
124 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
125
126 /* Structure/enum declaration ------------------------------- */
127 typedef struct board_info {
128
129         void __iomem *io_addr;  /* Register I/O base address */
130         void __iomem *io_data;  /* Data I/O address */
131         u16 irq;                /* IRQ */
132
133         u16 tx_pkt_cnt;
134         u16 queue_pkt_len;
135         u16 queue_start_addr;
136         u16 dbug_cnt;
137         u8 io_mode;             /* 0:word, 2:byte */
138         u8 phy_addr;
139
140         void (*inblk)(void __iomem *port, void *data, int length);
141         void (*outblk)(void __iomem *port, void *data, int length);
142         void (*dumpblk)(void __iomem *port, int length);
143
144         struct resource *addr_res;   /* resources found */
145         struct resource *data_res;
146         struct resource *addr_req;   /* resources requested */
147         struct resource *data_req;
148         struct resource *irq_res;
149
150         struct timer_list timer;
151         unsigned char srom[128];
152         spinlock_t lock;
153
154         struct mii_if_info mii;
155         u32 msg_enable;
156 } board_info_t;
157
158 /* function declaration ------------------------------------- */
159 static int dm9000_probe(struct platform_device *);
160 static int dm9000_open(struct net_device *);
161 static int dm9000_start_xmit(struct sk_buff *, struct net_device *);
162 static int dm9000_stop(struct net_device *);
163
164
165 static void dm9000_timer(unsigned long);
166 static void dm9000_init_dm9000(struct net_device *);
167
168 static irqreturn_t dm9000_interrupt(int, void *);
169
170 static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg);
171 static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg,
172                            int value);
173 static u16 read_srom_word(board_info_t *, int);
174 static void dm9000_rx(struct net_device *);
175 static void dm9000_hash_table(struct net_device *);
176
177 //#define DM9000_PROGRAM_EEPROM
178 #ifdef DM9000_PROGRAM_EEPROM
179 static void program_eeprom(board_info_t * db);
180 #endif
181 /* DM9000 network board routine ---------------------------- */
182
183 static void
184 dm9000_reset(board_info_t * db)
185 {
186         PRINTK1("dm9000x: resetting\n");
187         /* RESET device */
188         writeb(DM9000_NCR, db->io_addr);
189         udelay(200);
190         writeb(NCR_RST, db->io_data);
191         udelay(200);
192 }
193
194 /*
195  *   Read a byte from I/O port
196  */
197 static u8
198 ior(board_info_t * db, int reg)
199 {
200         writeb(reg, db->io_addr);
201         return readb(db->io_data);
202 }
203
204 /*
205  *   Write a byte to I/O port
206  */
207
208 static void
209 iow(board_info_t * db, int reg, int value)
210 {
211         writeb(reg, db->io_addr);
212         writeb(value, db->io_data);
213 }
214
215 /* routines for sending block to chip */
216
217 static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count)
218 {
219         writesb(reg, data, count);
220 }
221
222 static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count)
223 {
224         writesw(reg, data, (count+1) >> 1);
225 }
226
227 static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count)
228 {
229         writesl(reg, data, (count+3) >> 2);
230 }
231
232 /* input block from chip to memory */
233
234 static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count)
235 {
236         readsb(reg, data, count);
237 }
238
239
240 static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count)
241 {
242         readsw(reg, data, (count+1) >> 1);
243 }
244
245 static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count)
246 {
247         readsl(reg, data, (count+3) >> 2);
248 }
249
250 /* dump block from chip to null */
251
252 static void dm9000_dumpblk_8bit(void __iomem *reg, int count)
253 {
254         int i;
255         int tmp;
256
257         for (i = 0; i < count; i++)
258                 tmp = readb(reg);
259 }
260
261 static void dm9000_dumpblk_16bit(void __iomem *reg, int count)
262 {
263         int i;
264         int tmp;
265
266         count = (count + 1) >> 1;
267
268         for (i = 0; i < count; i++)
269                 tmp = readw(reg);
270 }
271
272 static void dm9000_dumpblk_32bit(void __iomem *reg, int count)
273 {
274         int i;
275         int tmp;
276
277         count = (count + 3) >> 2;
278
279         for (i = 0; i < count; i++)
280                 tmp = readl(reg);
281 }
282
283 /* dm9000_set_io
284  *
285  * select the specified set of io routines to use with the
286  * device
287  */
288
289 static void dm9000_set_io(struct board_info *db, int byte_width)
290 {
291         /* use the size of the data resource to work out what IO
292          * routines we want to use
293          */
294
295         switch (byte_width) {
296         case 1:
297                 db->dumpblk = dm9000_dumpblk_8bit;
298                 db->outblk  = dm9000_outblk_8bit;
299                 db->inblk   = dm9000_inblk_8bit;
300                 break;
301
302         case 2:
303                 db->dumpblk = dm9000_dumpblk_16bit;
304                 db->outblk  = dm9000_outblk_16bit;
305                 db->inblk   = dm9000_inblk_16bit;
306                 break;
307
308         case 3:
309                 printk(KERN_ERR PFX ": 3 byte IO, falling back to 16bit\n");
310                 db->dumpblk = dm9000_dumpblk_16bit;
311                 db->outblk  = dm9000_outblk_16bit;
312                 db->inblk   = dm9000_inblk_16bit;
313                 break;
314
315         case 4:
316         default:
317                 db->dumpblk = dm9000_dumpblk_32bit;
318                 db->outblk  = dm9000_outblk_32bit;
319                 db->inblk   = dm9000_inblk_32bit;
320                 break;
321         }
322 }
323
324
325 /* Our watchdog timed out. Called by the networking layer */
326 static void dm9000_timeout(struct net_device *dev)
327 {
328         board_info_t *db = (board_info_t *) dev->priv;
329         u8 reg_save;
330         unsigned long flags;
331
332         /* Save previous register address */
333         reg_save = readb(db->io_addr);
334         spin_lock_irqsave(&db->lock,flags);
335
336         netif_stop_queue(dev);
337         dm9000_reset(db);
338         dm9000_init_dm9000(dev);
339         /* We can accept TX packets again */
340         dev->trans_start = jiffies;
341         netif_wake_queue(dev);
342
343         /* Restore previous register address */
344         writeb(reg_save, db->io_addr);
345         spin_unlock_irqrestore(&db->lock,flags);
346 }
347
348 #ifdef CONFIG_NET_POLL_CONTROLLER
349 /*
350  *Used by netconsole
351  */
352 static void dm9000_poll_controller(struct net_device *dev)
353 {
354         disable_irq(dev->irq);
355         dm9000_interrupt(dev->irq,dev);
356         enable_irq(dev->irq);
357 }
358 #endif
359
360 /* dm9000_release_board
361  *
362  * release a board, and any mapped resources
363  */
364
365 static void
366 dm9000_release_board(struct platform_device *pdev, struct board_info *db)
367 {
368         if (db->data_res == NULL) {
369                 if (db->addr_res != NULL)
370                         release_mem_region((unsigned long)db->io_addr, 4);
371                 return;
372         }
373
374         /* unmap our resources */
375
376         iounmap(db->io_addr);
377         iounmap(db->io_data);
378
379         /* release the resources */
380
381         if (db->data_req != NULL) {
382                 release_resource(db->data_req);
383                 kfree(db->data_req);
384         }
385
386         if (db->addr_req != NULL) {
387                 release_resource(db->addr_req);
388                 kfree(db->addr_req);
389         }
390 }
391
392 #define res_size(_r) (((_r)->end - (_r)->start) + 1)
393
394 /*
395  * Search DM9000 board, allocate space and register it
396  */
397 static int
398 dm9000_probe(struct platform_device *pdev)
399 {
400         struct dm9000_plat_data *pdata = pdev->dev.platform_data;
401         struct board_info *db;  /* Point a board information structure */
402         struct net_device *ndev;
403         unsigned long base;
404         int ret = 0;
405         int iosize;
406         int i;
407         u32 id_val;
408
409         /* Init network device */
410         ndev = alloc_etherdev(sizeof (struct board_info));
411         if (!ndev) {
412                 printk("%s: could not allocate device.\n", CARDNAME);
413                 return -ENOMEM;
414         }
415
416         SET_NETDEV_DEV(ndev, &pdev->dev);
417
418         PRINTK2("dm9000_probe()");
419
420         /* setup board info structure */
421         db = (struct board_info *) ndev->priv;
422         memset(db, 0, sizeof (*db));
423
424         spin_lock_init(&db->lock);
425
426         if (pdev->num_resources < 2) {
427                 ret = -ENODEV;
428                 goto out;
429         } else if (pdev->num_resources == 2) {
430                 base = pdev->resource[0].start;
431
432                 if (!request_mem_region(base, 4, ndev->name)) {
433                         ret = -EBUSY;
434                         goto out;
435                 }
436
437                 ndev->base_addr = base;
438                 ndev->irq = pdev->resource[1].start;
439                 db->io_addr = (void __iomem *)base;
440                 db->io_data = (void __iomem *)(base + 4);
441
442                 /* ensure at least we have a default set of IO routines */
443                 dm9000_set_io(db, 2);
444
445         } else {
446                 db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
447                 db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
448                 db->irq_res  = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
449
450                 if (db->addr_res == NULL || db->data_res == NULL ||
451                     db->irq_res == NULL) {
452                         printk(KERN_ERR PFX "insufficient resources\n");
453                         ret = -ENOENT;
454                         goto out;
455                 }
456
457                 i = res_size(db->addr_res);
458                 db->addr_req = request_mem_region(db->addr_res->start, i,
459                                                   pdev->name);
460
461                 if (db->addr_req == NULL) {
462                         printk(KERN_ERR PFX "cannot claim address reg area\n");
463                         ret = -EIO;
464                         goto out;
465                 }
466
467                 db->io_addr = ioremap(db->addr_res->start, i);
468
469                 if (db->io_addr == NULL) {
470                         printk(KERN_ERR "failed to ioremap address reg\n");
471                         ret = -EINVAL;
472                         goto out;
473                 }
474
475                 iosize = res_size(db->data_res);
476                 db->data_req = request_mem_region(db->data_res->start, iosize,
477                                                   pdev->name);
478
479                 if (db->data_req == NULL) {
480                         printk(KERN_ERR PFX "cannot claim data reg area\n");
481                         ret = -EIO;
482                         goto out;
483                 }
484
485                 db->io_data = ioremap(db->data_res->start, iosize);
486
487                 if (db->io_data == NULL) {
488                         printk(KERN_ERR "failed to ioremap data reg\n");
489                         ret = -EINVAL;
490                         goto out;
491                 }
492
493                 /* fill in parameters for net-dev structure */
494
495                 ndev->base_addr = (unsigned long)db->io_addr;
496                 ndev->irq       = db->irq_res->start;
497
498                 /* ensure at least we have a default set of IO routines */
499                 dm9000_set_io(db, iosize);
500         }
501
502         /* check to see if anything is being over-ridden */
503         if (pdata != NULL) {
504                 /* check to see if the driver wants to over-ride the
505                  * default IO width */
506
507                 if (pdata->flags & DM9000_PLATF_8BITONLY)
508                         dm9000_set_io(db, 1);
509
510                 if (pdata->flags & DM9000_PLATF_16BITONLY)
511                         dm9000_set_io(db, 2);
512
513                 if (pdata->flags & DM9000_PLATF_32BITONLY)
514                         dm9000_set_io(db, 4);
515
516                 /* check to see if there are any IO routine
517                  * over-rides */
518
519                 if (pdata->inblk != NULL)
520                         db->inblk = pdata->inblk;
521
522                 if (pdata->outblk != NULL)
523                         db->outblk = pdata->outblk;
524
525                 if (pdata->dumpblk != NULL)
526                         db->dumpblk = pdata->dumpblk;
527         }
528
529         dm9000_reset(db);
530
531         /* try two times, DM9000 sometimes gets the first read wrong */
532         for (i = 0; i < 2; i++) {
533                 id_val  = ior(db, DM9000_VIDL);
534                 id_val |= (u32)ior(db, DM9000_VIDH) << 8;
535                 id_val |= (u32)ior(db, DM9000_PIDL) << 16;
536                 id_val |= (u32)ior(db, DM9000_PIDH) << 24;
537
538                 if (id_val == DM9000_ID)
539                         break;
540                 printk("%s: read wrong id 0x%08x\n", CARDNAME, id_val);
541         }
542
543         if (id_val != DM9000_ID) {
544                 printk("%s: wrong id: 0x%08x\n", CARDNAME, id_val);
545                 goto release;
546         }
547
548         /* from this point we assume that we have found a DM9000 */
549
550         /* driver system function */
551         ether_setup(ndev);
552
553         ndev->open               = &dm9000_open;
554         ndev->hard_start_xmit    = &dm9000_start_xmit;
555         ndev->tx_timeout         = &dm9000_timeout;
556         ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
557         ndev->stop               = &dm9000_stop;
558         ndev->set_multicast_list = &dm9000_hash_table;
559 #ifdef CONFIG_NET_POLL_CONTROLLER
560         ndev->poll_controller    = &dm9000_poll_controller;
561 #endif
562
563 #ifdef DM9000_PROGRAM_EEPROM
564         program_eeprom(db);
565 #endif
566         db->msg_enable       = NETIF_MSG_LINK;
567         db->mii.phy_id_mask  = 0x1f;
568         db->mii.reg_num_mask = 0x1f;
569         db->mii.force_media  = 0;
570         db->mii.full_duplex  = 0;
571         db->mii.dev          = ndev;
572         db->mii.mdio_read    = dm9000_phy_read;
573         db->mii.mdio_write   = dm9000_phy_write;
574
575         /* Read SROM content */
576         for (i = 0; i < 64; i++)
577                 ((u16 *) db->srom)[i] = read_srom_word(db, i);
578
579         /* Set Node Address */
580         for (i = 0; i < 6; i++)
581                 ndev->dev_addr[i] = db->srom[i];
582
583         if (!is_valid_ether_addr(ndev->dev_addr)) {
584                 /* try reading from mac */
585
586                 for (i = 0; i < 6; i++)
587                         ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
588         }
589
590         if (!is_valid_ether_addr(ndev->dev_addr))
591                 printk("%s: Invalid ethernet MAC address.  Please "
592                        "set using ifconfig\n", ndev->name);
593
594         platform_set_drvdata(pdev, ndev);
595         ret = register_netdev(ndev);
596
597         if (ret == 0) {
598                 printk("%s: dm9000 at %p,%p IRQ %d MAC: ",
599                        ndev->name,  db->io_addr, db->io_data, ndev->irq);
600                 for (i = 0; i < 5; i++)
601                         printk("%02x:", ndev->dev_addr[i]);
602                 printk("%02x\n", ndev->dev_addr[5]);
603         }
604         return 0;
605
606  release:
607  out:
608         printk("%s: not found (%d).\n", CARDNAME, ret);
609
610         dm9000_release_board(pdev, db);
611         free_netdev(ndev);
612
613         return ret;
614 }
615
616 /*
617  *  Open the interface.
618  *  The interface is opened whenever "ifconfig" actives it.
619  */
620 static int
621 dm9000_open(struct net_device *dev)
622 {
623         board_info_t *db = (board_info_t *) dev->priv;
624
625         PRINTK2("entering dm9000_open\n");
626
627         if (request_irq(dev->irq, &dm9000_interrupt, DM9000_IRQ_FLAGS, dev->name, dev))
628                 return -EAGAIN;
629
630         /* Initialize DM9000 board */
631         dm9000_reset(db);
632         dm9000_init_dm9000(dev);
633
634         /* Init driver variable */
635         db->dbug_cnt = 0;
636
637         /* set and active a timer process */
638         init_timer(&db->timer);
639         db->timer.expires  = DM9000_TIMER_WUT;
640         db->timer.data     = (unsigned long) dev;
641         db->timer.function = &dm9000_timer;
642         add_timer(&db->timer);
643
644         mii_check_media(&db->mii, netif_msg_link(db), 1);
645         netif_start_queue(dev);
646
647         return 0;
648 }
649
650 /*
651  * Initilize dm9000 board
652  */
653 static void
654 dm9000_init_dm9000(struct net_device *dev)
655 {
656         board_info_t *db = (board_info_t *) dev->priv;
657
658         PRINTK1("entering %s\n",__FUNCTION__);
659
660         /* I/O mode */
661         db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */
662
663         /* GPIO0 on pre-activate PHY */
664         iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */
665         iow(db, DM9000_GPCR, GPCR_GEP_CNTL);    /* Let GPIO0 output */
666         iow(db, DM9000_GPR, 0); /* Enable PHY */
667
668         /* Program operating register */
669         iow(db, DM9000_TCR, 0);         /* TX Polling clear */
670         iow(db, DM9000_BPTR, 0x3f);     /* Less 3Kb, 200us */
671         iow(db, DM9000_FCR, 0xff);      /* Flow Control */
672         iow(db, DM9000_SMCR, 0);        /* Special Mode */
673         /* clear TX status */
674         iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
675         iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */
676
677         /* Set address filter table */
678         dm9000_hash_table(dev);
679
680         /* Activate DM9000 */
681         iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
682         /* Enable TX/RX interrupt mask */
683         iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
684
685         /* Init Driver variable */
686         db->tx_pkt_cnt = 0;
687         db->queue_pkt_len = 0;
688         dev->trans_start = 0;
689 }
690
691 /*
692  *  Hardware start transmission.
693  *  Send a packet to media from the upper layer.
694  */
695 static int
696 dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
697 {
698         unsigned long flags;
699         board_info_t *db = (board_info_t *) dev->priv;
700
701         PRINTK3("dm9000_start_xmit\n");
702
703         if (db->tx_pkt_cnt > 1)
704                 return 1;
705
706         spin_lock_irqsave(&db->lock, flags);
707
708         /* Move data to DM9000 TX RAM */
709         writeb(DM9000_MWCMD, db->io_addr);
710
711         (db->outblk)(db->io_data, skb->data, skb->len);
712         dev->stats.tx_bytes += skb->len;
713
714         db->tx_pkt_cnt++;
715         /* TX control: First packet immediately send, second packet queue */
716         if (db->tx_pkt_cnt == 1) {
717                 /* Set TX length to DM9000 */
718                 iow(db, DM9000_TXPLL, skb->len & 0xff);
719                 iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff);
720
721                 /* Issue TX polling command */
722                 iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
723
724                 dev->trans_start = jiffies;     /* save the time stamp */
725         } else {
726                 /* Second packet */
727                 db->queue_pkt_len = skb->len;
728                 netif_stop_queue(dev);
729         }
730
731         spin_unlock_irqrestore(&db->lock, flags);
732
733         /* free this SKB */
734         dev_kfree_skb(skb);
735
736         return 0;
737 }
738
739 static void
740 dm9000_shutdown(struct net_device *dev)
741 {
742         board_info_t *db = (board_info_t *) dev->priv;
743
744         /* RESET device */
745         dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */
746         iow(db, DM9000_GPR, 0x01);      /* Power-Down PHY */
747         iow(db, DM9000_IMR, IMR_PAR);   /* Disable all interrupt */
748         iow(db, DM9000_RCR, 0x00);      /* Disable RX */
749 }
750
751 /*
752  * Stop the interface.
753  * The interface is stopped when it is brought.
754  */
755 static int
756 dm9000_stop(struct net_device *ndev)
757 {
758         board_info_t *db = (board_info_t *) ndev->priv;
759
760         PRINTK1("entering %s\n",__FUNCTION__);
761
762         /* deleted timer */
763         del_timer(&db->timer);
764
765         netif_stop_queue(ndev);
766         netif_carrier_off(ndev);
767
768         /* free interrupt */
769         free_irq(ndev->irq, ndev);
770
771         dm9000_shutdown(ndev);
772
773         return 0;
774 }
775
776 /*
777  * DM9000 interrupt handler
778  * receive the packet to upper layer, free the transmitted packet
779  */
780
781 static void
782 dm9000_tx_done(struct net_device *dev, board_info_t * db)
783 {
784         int tx_status = ior(db, DM9000_NSR);    /* Got TX status */
785
786         if (tx_status & (NSR_TX2END | NSR_TX1END)) {
787                 /* One packet sent complete */
788                 db->tx_pkt_cnt--;
789                 dev->stats.tx_packets++;
790
791                 /* Queue packet check & send */
792                 if (db->tx_pkt_cnt > 0) {
793                         iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff);
794                         iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff);
795                         iow(db, DM9000_TCR, TCR_TXREQ);
796                         dev->trans_start = jiffies;
797                 }
798                 netif_wake_queue(dev);
799         }
800 }
801
802 static irqreturn_t
803 dm9000_interrupt(int irq, void *dev_id)
804 {
805         struct net_device *dev = dev_id;
806         board_info_t *db;
807         int int_status;
808         u8 reg_save;
809
810         PRINTK3("entering %s\n",__FUNCTION__);
811
812         if (!dev) {
813                 PRINTK1("dm9000_interrupt() without DEVICE arg\n");
814                 return IRQ_HANDLED;
815         }
816
817         /* A real interrupt coming */
818         db = (board_info_t *) dev->priv;
819         spin_lock(&db->lock);
820
821         /* Save previous register address */
822         reg_save = readb(db->io_addr);
823
824         /* Disable all interrupts */
825         iow(db, DM9000_IMR, IMR_PAR);
826
827         /* Got DM9000 interrupt status */
828         int_status = ior(db, DM9000_ISR);       /* Got ISR */
829         iow(db, DM9000_ISR, int_status);        /* Clear ISR status */
830
831         /* Received the coming packet */
832         if (int_status & ISR_PRS)
833                 dm9000_rx(dev);
834
835         /* Trnasmit Interrupt check */
836         if (int_status & ISR_PTS)
837                 dm9000_tx_done(dev, db);
838
839         /* Re-enable interrupt mask */
840         iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM);
841
842         /* Restore previous register address */
843         writeb(reg_save, db->io_addr);
844
845         spin_unlock(&db->lock);
846
847         return IRQ_HANDLED;
848 }
849
850 /*
851  *  A periodic timer routine
852  *  Dynamic media sense, allocated Rx buffer...
853  */
854 static void
855 dm9000_timer(unsigned long data)
856 {
857         struct net_device *dev = (struct net_device *) data;
858         board_info_t *db = (board_info_t *) dev->priv;
859
860         PRINTK3("dm9000_timer()\n");
861
862         mii_check_media(&db->mii, netif_msg_link(db), 0);
863
864         /* Set timer again */
865         db->timer.expires = DM9000_TIMER_WUT;
866         add_timer(&db->timer);
867 }
868
869 struct dm9000_rxhdr {
870         u16     RxStatus;
871         u16     RxLen;
872 } __attribute__((__packed__));
873
874 /*
875  *  Received a packet and pass to upper layer
876  */
877 static void
878 dm9000_rx(struct net_device *dev)
879 {
880         board_info_t *db = (board_info_t *) dev->priv;
881         struct dm9000_rxhdr rxhdr;
882         struct sk_buff *skb;
883         u8 rxbyte, *rdptr;
884         bool GoodPacket;
885         int RxLen;
886
887         /* Check packet ready or not */
888         do {
889                 ior(db, DM9000_MRCMDX); /* Dummy read */
890
891                 /* Get most updated data */
892                 rxbyte = readb(db->io_data);
893
894                 /* Status check: this byte must be 0 or 1 */
895                 if (rxbyte > DM9000_PKT_RDY) {
896                         printk("status check failed: %d\n", rxbyte);
897                         iow(db, DM9000_RCR, 0x00);      /* Stop Device */
898                         iow(db, DM9000_ISR, IMR_PAR);   /* Stop INT request */
899                         return;
900                 }
901
902                 if (rxbyte != DM9000_PKT_RDY)
903                         return;
904
905                 /* A packet ready now  & Get status/length */
906                 GoodPacket = true;
907                 writeb(DM9000_MRCMD, db->io_addr);
908
909                 (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
910
911                 RxLen = rxhdr.RxLen;
912
913                 /* Packet Status check */
914                 if (RxLen < 0x40) {
915                         GoodPacket = false;
916                         PRINTK1("Bad Packet received (runt)\n");
917                 }
918
919                 if (RxLen > DM9000_PKT_MAX) {
920                         PRINTK1("RST: RX Len:%x\n", RxLen);
921                 }
922
923                 if (rxhdr.RxStatus & 0xbf00) {
924                         GoodPacket = false;
925                         if (rxhdr.RxStatus & 0x100) {
926                                 PRINTK1("fifo error\n");
927                                 dev->stats.rx_fifo_errors++;
928                         }
929                         if (rxhdr.RxStatus & 0x200) {
930                                 PRINTK1("crc error\n");
931                                 dev->stats.rx_crc_errors++;
932                         }
933                         if (rxhdr.RxStatus & 0x8000) {
934                                 PRINTK1("length error\n");
935                                 dev->stats.rx_length_errors++;
936                         }
937                 }
938
939                 /* Move data from DM9000 */
940                 if (GoodPacket
941                     && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
942                         skb_reserve(skb, 2);
943                         rdptr = (u8 *) skb_put(skb, RxLen - 4);
944
945                         /* Read received packet from RX SRAM */
946
947                         (db->inblk)(db->io_data, rdptr, RxLen);
948                         dev->stats.rx_bytes += RxLen;
949
950                         /* Pass to upper layer */
951                         skb->protocol = eth_type_trans(skb, dev);
952                         netif_rx(skb);
953                         dev->stats.rx_packets++;
954
955                 } else {
956                         /* need to dump the packet's data */
957
958                         (db->dumpblk)(db->io_data, RxLen);
959                 }
960         } while (rxbyte == DM9000_PKT_RDY);
961 }
962
963 /*
964  *  Read a word data from SROM
965  */
966 static u16
967 read_srom_word(board_info_t * db, int offset)
968 {
969         iow(db, DM9000_EPAR, offset);
970         iow(db, DM9000_EPCR, EPCR_ERPRR);
971         mdelay(8);              /* according to the datasheet 200us should be enough,
972                                    but it doesn't work */
973         iow(db, DM9000_EPCR, 0x0);
974         return (ior(db, DM9000_EPDRL) + (ior(db, DM9000_EPDRH) << 8));
975 }
976
977 #ifdef DM9000_PROGRAM_EEPROM
978 /*
979  * Write a word data to SROM
980  */
981 static void
982 write_srom_word(board_info_t * db, int offset, u16 val)
983 {
984         iow(db, DM9000_EPAR, offset);
985         iow(db, DM9000_EPDRH, ((val >> 8) & 0xff));
986         iow(db, DM9000_EPDRL, (val & 0xff));
987         iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW);
988         mdelay(8);              /* same shit */
989         iow(db, DM9000_EPCR, 0);
990 }
991
992 /*
993  * Only for development:
994  * Here we write static data to the eeprom in case
995  * we don't have valid content on a new board
996  */
997 static void
998 program_eeprom(board_info_t * db)
999 {
1000         u16 eeprom[] = { 0x0c00, 0x007f, 0x1300,        /* MAC Address */
1001                 0x0000,         /* Autoload: accept nothing */
1002                 0x0a46, 0x9000, /* Vendor / Product ID */
1003                 0x0000,         /* pin control */
1004                 0x0000,
1005         };                      /* Wake-up mode control */
1006         int i;
1007         for (i = 0; i < 8; i++)
1008                 write_srom_word(db, i, eeprom[i]);
1009 }
1010 #endif
1011
1012
1013 /*
1014  *  Calculate the CRC valude of the Rx packet
1015  *  flag = 1 : return the reverse CRC (for the received packet CRC)
1016  *         0 : return the normal CRC (for Hash Table index)
1017  */
1018
1019 static unsigned long
1020 cal_CRC(unsigned char *Data, unsigned int Len, u8 flag)
1021 {
1022
1023        u32 crc = ether_crc_le(Len, Data);
1024
1025        if (flag)
1026                return ~crc;
1027
1028        return crc;
1029 }
1030
1031 /*
1032  *  Set DM9000 multicast address
1033  */
1034 static void
1035 dm9000_hash_table(struct net_device *dev)
1036 {
1037         board_info_t *db = (board_info_t *) dev->priv;
1038         struct dev_mc_list *mcptr = dev->mc_list;
1039         int mc_cnt = dev->mc_count;
1040         u32 hash_val;
1041         u16 i, oft, hash_table[4];
1042         unsigned long flags;
1043
1044         PRINTK2("dm9000_hash_table()\n");
1045
1046         spin_lock_irqsave(&db->lock,flags);
1047
1048         for (i = 0, oft = 0x10; i < 6; i++, oft++)
1049                 iow(db, oft, dev->dev_addr[i]);
1050
1051         /* Clear Hash Table */
1052         for (i = 0; i < 4; i++)
1053                 hash_table[i] = 0x0;
1054
1055         /* broadcast address */
1056         hash_table[3] = 0x8000;
1057
1058         /* the multicast address in Hash Table : 64 bits */
1059         for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) {
1060                 hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f;
1061                 hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16);
1062         }
1063
1064         /* Write the hash table to MAC MD table */
1065         for (i = 0, oft = 0x16; i < 4; i++) {
1066                 iow(db, oft++, hash_table[i] & 0xff);
1067                 iow(db, oft++, (hash_table[i] >> 8) & 0xff);
1068         }
1069
1070         spin_unlock_irqrestore(&db->lock,flags);
1071 }
1072
1073
1074 /*
1075  *   Read a word from phyxcer
1076  */
1077 static int
1078 dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg)
1079 {
1080         board_info_t *db = (board_info_t *) dev->priv;
1081         unsigned long flags;
1082         unsigned int reg_save;
1083         int ret;
1084
1085         spin_lock_irqsave(&db->lock,flags);
1086
1087         /* Save previous register address */
1088         reg_save = readb(db->io_addr);
1089
1090         /* Fill the phyxcer register into REG_0C */
1091         iow(db, DM9000_EPAR, DM9000_PHY | reg);
1092
1093         iow(db, DM9000_EPCR, 0xc);      /* Issue phyxcer read command */
1094         udelay(100);            /* Wait read complete */
1095         iow(db, DM9000_EPCR, 0x0);      /* Clear phyxcer read command */
1096
1097         /* The read data keeps on REG_0D & REG_0E */
1098         ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL);
1099
1100         /* restore the previous address */
1101         writeb(reg_save, db->io_addr);
1102
1103         spin_unlock_irqrestore(&db->lock,flags);
1104
1105         return ret;
1106 }
1107
1108 /*
1109  *   Write a word to phyxcer
1110  */
1111 static void
1112 dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value)
1113 {
1114         board_info_t *db = (board_info_t *) dev->priv;
1115         unsigned long flags;
1116         unsigned long reg_save;
1117
1118         spin_lock_irqsave(&db->lock,flags);
1119
1120         /* Save previous register address */
1121         reg_save = readb(db->io_addr);
1122
1123         /* Fill the phyxcer register into REG_0C */
1124         iow(db, DM9000_EPAR, DM9000_PHY | reg);
1125
1126         /* Fill the written data into REG_0D & REG_0E */
1127         iow(db, DM9000_EPDRL, (value & 0xff));
1128         iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
1129
1130         iow(db, DM9000_EPCR, 0xa);      /* Issue phyxcer write command */
1131         udelay(500);            /* Wait write complete */
1132         iow(db, DM9000_EPCR, 0x0);      /* Clear phyxcer write command */
1133
1134         /* restore the previous address */
1135         writeb(reg_save, db->io_addr);
1136
1137         spin_unlock_irqrestore(&db->lock,flags);
1138 }
1139
1140 static int
1141 dm9000_drv_suspend(struct platform_device *dev, pm_message_t state)
1142 {
1143         struct net_device *ndev = platform_get_drvdata(dev);
1144
1145         if (ndev) {
1146                 if (netif_running(ndev)) {
1147                         netif_device_detach(ndev);
1148                         dm9000_shutdown(ndev);
1149                 }
1150         }
1151         return 0;
1152 }
1153
1154 static int
1155 dm9000_drv_resume(struct platform_device *dev)
1156 {
1157         struct net_device *ndev = platform_get_drvdata(dev);
1158         board_info_t *db = (board_info_t *) ndev->priv;
1159
1160         if (ndev) {
1161
1162                 if (netif_running(ndev)) {
1163                         dm9000_reset(db);
1164                         dm9000_init_dm9000(ndev);
1165
1166                         netif_device_attach(ndev);
1167                 }
1168         }
1169         return 0;
1170 }
1171
1172 static int
1173 dm9000_drv_remove(struct platform_device *pdev)
1174 {
1175         struct net_device *ndev = platform_get_drvdata(pdev);
1176
1177         platform_set_drvdata(pdev, NULL);
1178
1179         unregister_netdev(ndev);
1180         dm9000_release_board(pdev, (board_info_t *) ndev->priv);
1181         free_netdev(ndev);              /* free device structure */
1182
1183         PRINTK1("clean_module() exit\n");
1184
1185         return 0;
1186 }
1187
1188 static struct platform_driver dm9000_driver = {
1189         .driver = {
1190                 .name    = "dm9000",
1191                 .owner   = THIS_MODULE,
1192         },
1193         .probe   = dm9000_probe,
1194         .remove  = dm9000_drv_remove,
1195         .suspend = dm9000_drv_suspend,
1196         .resume  = dm9000_drv_resume,
1197 };
1198
1199 static int __init
1200 dm9000_init(void)
1201 {
1202         printk(KERN_INFO "%s Ethernet Driver\n", CARDNAME);
1203
1204         return platform_driver_register(&dm9000_driver);        /* search board and register */
1205 }
1206
1207 static void __exit
1208 dm9000_cleanup(void)
1209 {
1210         platform_driver_unregister(&dm9000_driver);
1211 }
1212
1213 module_init(dm9000_init);
1214 module_exit(dm9000_cleanup);
1215
1216 MODULE_AUTHOR("Sascha Hauer, Ben Dooks");
1217 MODULE_DESCRIPTION("Davicom DM9000 network driver");
1218 MODULE_LICENSE("GPL");