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