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