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