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