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