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