MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / net / arm / p2001_eth.c
blobfa5e976e74fbd1f054857dfc0dba7fb22993d741
1 /*
2 * linux/drivers/char/p2001_eth.c
4 * Driver for P2001 ethernet unit
6 * Copyright (C) 2004 Tobias Lorenz
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Version 1.0: First working version
25 * Version 1.1: mdio ioctl calls
26 * Version 1.2: statistics counter
27 * Version 1.3: skbuff direct instead of buffer-copy for sending
28 * Version 1.4: ethtool calls
29 * Version 1.5: interrupt driven transmit with transmit buffer ring
30 * Version 1.6: support for all interfaces with phy connected
31 * Version 1.7: generic mii infrastructure
32 * Version 1.8: automatic MDIO CLK calculation
33 * Version 1.9: added request_mem_region
34 * Version 1.10: bug fix for main isp
35 * Version 1.11: removed all READ_REG/WRITE_REG
36 * Version 1.12: transmit timeout, no boot via eth1
37 * Version 1.13: initialisation fix, which results in transmission errors,
38 * some pings are 10 times longer than the regular ones (glitches)
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/string.h>
44 #include <linux/timer.h>
45 #include <linux/errno.h>
46 #include <linux/ioport.h>
47 #include <linux/slab.h>
48 #include <linux/interrupt.h>
49 #include <linux/netdevice.h>
50 #include <linux/init.h>
51 #include <linux/mii.h>
52 #include <linux/etherdevice.h>
53 #include <linux/skbuff.h>
54 #include <linux/delay.h>
55 #include <linux/ethtool.h>
56 #include <linux/crc32.h>
58 #include <asm/processor.h> /* Processor type for cache alignment. */
59 #include <asm/bitops.h>
60 #include <asm/io.h>
61 #include <asm/uaccess.h> /* User space memory access functions */
65 /**************************************************************************
66 * Definitions
67 **************************************************************************/
68 static const char *version =
69 "p2001_eth.c:v1.13 10/13/2004 Tobias Lorenz (tobias.lorenz@gmx.net)\n";
71 static const char p2001_eth_name[] = "P2001 eth";
73 /* Hardware lookup table */
74 struct {
75 unsigned int nr; /* ethernet unit number / dma channel number */
76 unsigned int base_addr; /* device I/O address */
77 unsigned int irq; /* device data IRQ number (error IRQ +1) */
78 unsigned int phy_id; /* assigned phy address */
79 unsigned char mac_hw_addr[6]; /* fixed MAC address */
80 } p2001_eth_dev_list[4] = {
81 { 0, (unsigned int)P2001_EU0, IRQ_EU0_DATA, 0, {0x00,0x09,0x4F,0x00,0x00,0x02} },
82 { 1, (unsigned int)P2001_EU1, IRQ_EU1_DATA, 1, {0x00,0x09,0x4F,0x00,0x00,0x03} },
83 { 2, (unsigned int)P2001_EU2, IRQ_EU2_DATA, 2, {0x00,0x09,0x4F,0x00,0x00,0x04} },
84 { 3, (unsigned int)P2001_EU3, IRQ_EU3_DATA, 3, {0x00,0x09,0x4F,0x00,0x00,0x05} },
87 /* DMA descriptions and buffers */
88 #define NUM_RX_DESC 16 /* Number of RX descriptor registers. */
89 #define NUM_TX_DESC 16 /* Number of TX descriptor registers. */
90 #define DMA_BUF_SIZE 2048 /* Buffer size */
92 /* Drivers private structure */
93 struct p2001_eth_private {
94 struct net_device_stats stats;
96 /* DMA descriptors and buffers */
97 DMA_DSC rxd[NUM_RX_DESC] __attribute__ ((aligned(16)));
98 DMA_DSC txd[NUM_TX_DESC] __attribute__ ((aligned(16)));
99 char rxb[NUM_RX_DESC * DMA_BUF_SIZE] __attribute__ ((aligned(16)));
100 struct sk_buff *txb[NUM_TX_DESC];
101 /* producer/comsumer pointers for Tx/Rx ring */
102 int cur_tx, dirty_tx;
103 int cur_rx, dirty_rx;
105 /* Device selectors */
106 unsigned int nr; /* NR/DMA channel: 0..3 */
107 char adapter_name[11]; /* P2001 ethx\0 */
109 spinlock_t lock;
111 /* The Tx queue is full. */
112 unsigned int tx_full;
114 /* MII interface info */
115 struct mii_if_info mii;
118 /* mdio handling */
119 static void mdio_hard_reset(void);
120 static int mdio_read(struct net_device *dev, int phy_id, int location);
121 static void mdio_write(struct net_device *dev, int phy_id, int location, int val);
123 /* net_device functions */
124 static struct net_device_stats * p2001_eth_get_stats(struct net_device *dev);
125 static int p2001_eth_open(struct net_device *dev);
126 static int p2001_eth_stop(struct net_device *dev);
127 static int p2001_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *dev);
128 static void p2001_eth_tx_timeout(struct net_device *dev);
129 static int p2001_eth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
131 /* interrupt routines */
132 static irqreturn_t p2001_eth_data_interrupt(int irq, struct net_device *dev, struct pt_regs *regs);
133 static irqreturn_t p2001_eth_error_interrupt(int irq, struct net_device *dev, struct pt_regs *regs);
135 /* ethtool ops */
136 static struct ethtool_ops p2001_eth_ethtool_ops;
138 /* driver functions (pci_driver, isa_driver) */
139 struct net_device * __init p2001_eth_probe(int unit);
140 static void __devexit p2001_eth_remove(struct net_device *dev);
144 /**************************************************************************
145 * PHY MANAGEMENT UNIT - Read/write
146 **************************************************************************/
149 * mdio_hard_reset - hardware reset all MII PHYs and set MDIO CLK
151 static void mdio_hard_reset()
153 /* GPIO24/25: TX_ER2/TX_ER0 */
154 /* GPIO26/27: PHY_RESET/TX_ER1 */
155 P2001_GPIO->PIN_MUX |= 0x0018;
156 // 31-16: 0000 1111 0000 0000
157 P2001_GPIO->GPIO2_En |= 0x0400;
159 P2001_GPIO->GPIO2_Out |= 0x04000000;
160 P2001_GPIO->GPIO2_Out &= ~0x0400;
161 mdelay(500);
162 P2001_GPIO->GPIO2_Out |= 0x0400;
164 /* set management unit clock divisor */
165 // max. MDIO CLK = 2.048 MHz (EU.doc)
166 // max. MDIO CLK = 8.000 MHz (LXT971A)
167 // sysclk/(2*(n+1)) = MDIO CLK <= 2.048 MHz
168 // n >= sysclk/4.096 MHz - 1
169 P2001_MU->MU_DIV = (CONFIG_SYSCLK/4096000)-1; // 2.048 MHz
170 //asm("nop \n nop");
175 * mdio_read - read MII PHY register
176 * @dev: the net device to read
177 * @regadr: the phy register id to read
179 * Read MII registers through MDIO and MDC
180 * using MDIO management frame structure and protocol(defined by ISO/IEC).
182 static int mdio_read(struct net_device *dev, int phy_id, int location)
184 do {
185 /* Warten bis Hardware inaktiv (MIU = "0") */
186 while (P2001_MU->MU_CNTL & 0x8000)
187 barrier();
189 /* Schreiben MU_CNTL */
190 P2001_MU->MU_CNTL = location + (phy_id<<5) + (2<<10);
192 /* Warten bis Hardware aktiv (MIU = "1") */
193 while ((P2001_MU->MU_CNTL & 0x8000) == 0)
194 barrier();
195 //asm("nop \r\n nop");
197 /* Warten bis Hardware inaktiv (MIU = "0") */
198 while (P2001_MU->MU_CNTL & 0x8000)
199 barrier();
201 /* Fehler, wenn MDIO Read Error (MRE = "1") */
202 } while (P2001_MU->MU_CNTL & 0x4000);
204 /* Lesen MU_DATA */
205 return P2001_MU->MU_DATA;
210 * mdio_write - write MII PHY register
211 * @dev: the net device to write
212 * @regadr: the phy regiester id to write
213 * @value: the register value to write with
215 * Write MII registers with @value through MDIO and MDC
216 * using MDIO management frame structure and protocol(defined by ISO/IEC)
218 static void mdio_write(struct net_device *dev, int phy_id, int location, int val)
220 /* Warten bis Hardware inaktiv (MIU = "0") */
221 while (P2001_MU->MU_CNTL & 0x8000)
222 barrier();
224 /* Schreiben MU_DATA */
225 P2001_MU->MU_DATA = val;
227 /* Schreiben MU_CNTL */
228 P2001_MU->MU_CNTL = location + (phy_id<<5) + (1<<10);
230 /* Warten bis Hardware aktiv (MIU = "1") */
231 while ((P2001_MU->MU_CNTL & 0x8000) == 0)
232 barrier();
233 //asm("nop \r\n nop");
235 /* Warten bis Hardware inaktiv (MIU = "0") */
236 while (P2001_MU->MU_CNTL & 0x8000)
237 barrier();
241 // mdio_write(dev, priv->mii.phy_id, MII_BMCR, BMCR_RESET);
245 /**************************************************************************
246 * GET_STATS - Get read/write statistics
247 **************************************************************************/
250 * p2001_eth_get_stats - Get p2001 read/write statistics
251 * @dev: the net device to get statistics for
253 * get tx/rx statistics for p2001
255 static struct net_device_stats * p2001_eth_get_stats(struct net_device *dev)
257 struct p2001_eth_private *priv = dev->priv;
259 return &priv->stats;
264 /**************************************************************************
265 * OPEN - Open network device
266 **************************************************************************/
269 * p2001_eth_open - open p2001 ethernet device
270 * @dev: the net device to open
272 * Do some initialization and start net interface.
273 * enable interrupts and set timer.
275 static int p2001_eth_open(struct net_device *dev)
277 struct p2001_eth_private *priv = dev->priv;
278 P2001_ETH_regs_ptr EU = (P2001_ETH_regs_ptr) dev->base_addr;
279 int i, ret;
281 // printk("%s: p2001_eth_open\n", dev->name);
283 /* request data and error interrupts */
284 ret = request_irq(dev->irq, (void *) &p2001_eth_data_interrupt, 0, dev->name, dev);
285 if (ret)
286 return ret;
287 ret = request_irq(dev->irq+1, (void *) &p2001_eth_error_interrupt, 0, dev->name, dev);
288 if (ret)
289 return ret;
291 /* set rx filter (physical mac address) */
292 EU->RMAC_PHYU =
293 (dev->dev_addr[0]<< 8) +
294 (dev->dev_addr[1]<< 0);
295 EU->RMAC_PHYL =
296 (dev->dev_addr[2]<<24) +
297 (dev->dev_addr[3]<<16) +
298 (dev->dev_addr[4]<<8 ) +
299 (dev->dev_addr[5]<<0 );
301 /* initialize the tx descriptor ring */
302 priv->tx_full = 0;
303 priv->cur_tx = 0;
304 priv->dirty_tx = 0;
305 for (i = 0; i < NUM_TX_DESC; i++) {
306 priv->txd[i].stat = 0; // DSC0
307 priv->txd[i].cntl = 0; // DSC1
308 priv->txd[i].buf = 0; // DSC2 BUFFER (EU-TX data)
309 priv->txd[i].next = &priv->txd[(i+1) % NUM_TX_DESC]; // DSC3 NEXTDSC @next/@first
311 EU->TMAC_DMA_DESC = &priv->txd[0];
313 /* initialize the rx descriptor ring */
314 priv->cur_rx = 0;
315 priv->dirty_rx = 0;
316 for (i = 0; i < NUM_RX_DESC; i++) {
317 priv->rxd[i].stat = (1<<31) | (1<<30) | (1<<29); // DSC0 OWN|START|END
318 priv->rxd[i].cntl = (1<<30) | (1<<23); // DSC1 INT|RECEIVE
319 priv->rxd[i].cntl |= priv->nr << 16; // DSC1 CHANNEL
320 priv->rxd[i].cntl |= DMA_BUF_SIZE; // DSC1 LEN
321 priv->rxd[i].buf = &priv->rxb[i*DMA_BUF_SIZE]; // DSC2 BUFFER (EU-RX data)
322 priv->rxd[i].next = &priv->rxd[(i+1) % NUM_RX_DESC]; // DSC3 NEXTDSC @next/@first
324 EU->RMAC_DMA_DESC = &priv->rxd[0];
326 /* set transmitter mode */
327 EU->TMAC_CNTL = (1<<4) | /* COI: Collision ignore */
328 //(1<<3) | /* CSI: Carrier Sense ignore */
329 (1<<2); /* ATP: Automatic Transmit Padding */
331 /* set receive mode */
332 EU->RMAC_CNTL = (1<<3) | /* BROAD: Broadcast packets */
333 (1<<1); /* PHY : Packets to out MAC address */
335 /* enable receiver */
336 EU->RMAC_DMA_EN = 1;
338 netif_start_queue(dev);
340 return 0;
345 /**************************************************************************
346 * STOP - Close network device
347 **************************************************************************/
350 * p2001_eth_stop - close p2001 ethernet device
351 * @dev: the net device to be closed
353 * Disable interrupts, stop the Tx and Rx Status Machine
354 * free Tx and RX socket buffer
356 static int p2001_eth_stop(struct net_device *dev)
358 struct p2001_eth_private *priv = dev->priv;
359 P2001_ETH_regs_ptr EU = (P2001_ETH_regs_ptr) dev->base_addr;
360 struct sk_buff *skb;
361 unsigned int i;
363 // printk("%s: p2001_eth_stop\n", dev->name);
365 netif_stop_queue(dev);
367 /* Stop the chip's Tx and Rx Status Machine */
368 EU->TMAC_DMA_EN = 0;
369 EU->RMAC_DMA_EN = 0;
371 free_irq(dev->irq, dev);
372 free_irq(dev->irq+1, dev);
374 /* Free Tx skbuff */
375 for (i = 0; i < NUM_TX_DESC; i++) {
376 skb = priv->txb[i];
377 if (skb) {
378 dev_kfree_skb(skb);
379 priv->txb[i] = 0;
384 /* Green! Put the chip in low-power mode. */
386 return 0;
391 /**************************************************************************
392 * HARD START XMIT - Force start sending packets
393 **************************************************************************/
396 * p2001_eth_hard_start_xmit - start transmit routine
397 * @skb: socket buffer pointer to put the data being transmitted
398 * @dev: the net device to transmit with
400 * Set the transmit buffer descriptor,
401 * and write TxENA to enable transmit state machine.
402 * tell upper layer if the buffer is full
404 static int p2001_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
406 struct p2001_eth_private *priv = dev->priv;
407 P2001_ETH_regs_ptr EU = (P2001_ETH_regs_ptr) dev->base_addr;
408 unsigned int entry;
409 unsigned long flags;
410 unsigned int index_cur_tx, index_dirty_tx;
411 unsigned int count_dirty_tx;
413 spin_lock_irqsave(&priv->lock, flags);
414 EU->TMAC_DMA_EN = 0; /* clear run bit */
416 // printk("%s: p2001_eth_hard_start_xmit: size=%d\n", dev->name, skb->len);
418 /* Calculate the next Tx descriptor entry. */
419 entry = priv->cur_tx % NUM_TX_DESC;
420 priv->txb[entry] = skb;
422 /* set the transmit buffer descriptor and enable Transmit State Machine */
423 priv->txd[entry].stat = (1<<31) | (1<<30) | (1<<29); // DSC0 OWN|START|END
424 priv->txd[entry].cntl = priv->nr << 16; // DSC1 CHANNEL
425 priv->txd[entry].cntl |= (1<<30); // DSC1 INT
426 priv->txd[entry].cntl |= skb->len; // DSC1 LEN
427 priv->txd[entry].buf = skb->data; // DSC2 BUFFER (EU-TX data)
429 priv->cur_tx++;
430 index_cur_tx = priv->cur_tx;
431 index_dirty_tx = priv->dirty_tx;
433 for (count_dirty_tx = 0; index_cur_tx != index_dirty_tx; index_dirty_tx++)
434 count_dirty_tx++;
436 // printk("%s: entry=%d, cur_tx=%d, dirty_tx=%d, count_dirty_tx=%d\n", dev->name,
437 // entry, priv->cur_tx % NUM_TX_DESC, priv->dirty_tx % NUM_TX_DESC, count_dirty_tx);
439 EU->TMAC_DMA_DESC = &priv->txd[priv->dirty_tx % NUM_TX_DESC];
441 EU->TMAC_DMA_EN = 1; /* set run bit */
442 spin_unlock_irqrestore(&priv->lock, flags);
444 dev->trans_start = jiffies;
446 // printk(KERN_INFO "%s: Queued Tx packet at %p size %d to slot %d.\n",
447 // dev->name, skb->data, (int)skb->len, entry);
449 return 0;
454 /**************************************************************************
455 * DO_IOCTL - Process MII i/o control command
456 **************************************************************************/
459 * p2001_eth_do_ioctl - process MII i/o control command
460 * @dev: the net device to command for
461 * @rq: parameter for command
462 * @cmd: the i/o command
464 * Process MII command like read/write MII register
466 static int p2001_eth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
468 struct p2001_eth_private *priv = dev->priv;
469 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
471 return generic_mii_ioctl(&priv->mii, data, cmd, NULL);
476 /**************************************************************************
477 * TX_TIMEOUT - Transmit timeout routine
478 **************************************************************************/
481 * p2001_eth_tx_timeout - transmit timeout routine
482 * @dev: the net device to command for
484 * print transmit timeout status
485 * disable interrupts and do some tasks
487 static void p2001_eth_tx_timeout(struct net_device *dev)
489 // struct p2001_eth_private *priv = dev->priv;
491 printk(KERN_INFO "%s: Transmit timeout\n", dev->name);
496 /**************************************************************************
497 * TX - interrupt transmit routine
498 **************************************************************************/
501 * p2001_eth_tx - finish up transmission of packets
502 * @net_dev: the net device to be transmitted on
504 * Check for error condition and free socket buffer etc
505 * schedule for more transmission as needed
506 * Note: This fucntion is called by interrupt handler,
507 * don't do "too much" work here
510 static void p2001_eth_tx(struct net_device *dev)
512 struct p2001_eth_private *priv = dev->priv;
513 P2001_ETH_regs_ptr EU = (P2001_ETH_regs_ptr) dev->base_addr;
514 unsigned int index_cur_tx, index_dirty_tx;
515 unsigned int count_dirty_tx;
517 // printk("%s: p2001_eth_tx\n", dev->name);
518 for (; priv->dirty_tx != priv->cur_tx; priv->dirty_tx++) {
519 struct sk_buff *skb;
520 unsigned int entry;
521 unsigned int status;
523 entry = priv->dirty_tx % NUM_TX_DESC;
524 status = priv->txd[entry].stat;
526 if (status & (1<<31)) { // OWN
527 /* The packet is not transmitted yet (owned by hardware) !
528 Note: the interrupt is generated only when Tx Machine
529 is idle, so this is an almost impossible case */
530 // printk("%s: p2001_eth_tx: nothing more to do\n", dev->name);
531 break;
534 // if (status & 0x0000000f) { // CRS|ED|OWC|EC
535 if (status & 0x00000007) { // ED|OWC|EC
536 /* packet unsuccessfully transmitted */
537 printk("%s: Transmit error, Tx status %8.8x.\n", dev->name, status);
538 priv->stats.tx_errors++;
539 // if (status & (1<<3)) // CRS
540 // priv->stats.tx_carrier_errors++;
541 if (status & (1<<1)) // OWC
542 priv->stats.tx_window_errors++;
543 } else {
544 /* packet successfully transmitted */
545 // printk("%s: p2001_eth_tx: success\n", dev->name);
546 priv->stats.collisions += (status & 0x00000f00) >> 8;
547 priv->stats.tx_bytes += priv->txd[entry].cntl & 0xffff;
548 priv->stats.tx_packets++;
550 /* Free the original skb. */
551 skb = priv->txb[entry];
552 dev_kfree_skb_irq(skb);
553 priv->txb[entry] = 0;
554 priv->txd[entry].stat = 0; // DSC0
555 priv->txd[entry].cntl = 0; // DSC1
556 priv->txd[entry].buf = 0; // DSC2 BUFFER (EU-TX data)
559 if (priv->tx_full && netif_queue_stopped(dev)) {
560 index_cur_tx = priv->cur_tx;
561 index_dirty_tx = priv->dirty_tx;
563 for (count_dirty_tx = 0; index_cur_tx != index_dirty_tx; index_dirty_tx++)
564 count_dirty_tx++;
566 if (count_dirty_tx < NUM_TX_DESC - 4) {
567 /* The ring is no longer full, clear tx_full and
568 schedule more transmissions by netif_wake_queue(dev) */
569 priv->tx_full = 0;
570 netif_wake_queue(dev);
574 EU->TMAC_DMA_STAT |= (1<<8);
579 /**************************************************************************
580 * RX - interrupt receive routine
581 **************************************************************************/
584 * p2001_eth_rx - p2001_eth receive routine
585 * @dev: the net device which receives data
587 * Process receive interrupt events,
588 * put buffer to higher layer and refill buffer pool
589 * Note: This fucntion is called by interrupt handler,
590 * don't do "too much" work here
592 static void p2001_eth_rx(struct net_device *dev)
594 struct p2001_eth_private *priv = dev->priv;
595 P2001_ETH_regs_ptr EU = (P2001_ETH_regs_ptr) dev->base_addr;
596 unsigned int entry;
597 unsigned int status;
598 struct sk_buff *skb;
599 unsigned int pkt_len;
601 // printk("%s: p2001_eth_rx\n", dev->name);
602 while(1) {
603 entry = priv->cur_rx % NUM_RX_DESC;
604 status = priv->rxd[entry].stat;
605 if (status & (1<<31))
606 break;
608 if (status & 0x07c00000) { // NOBYTE|CRCERR|COL|ISE|ILEN
609 /* corrupted packet received */
610 printk(KERN_INFO "%s: Corrupted packet "
611 "received, buffer status = 0x%8.8x.\n",
612 dev->name, status);
613 priv->stats.rx_errors++;
614 } else {
615 /* give the socket buffer to the upper layers */
616 pkt_len = priv->rxd[entry].cntl & 0xffff;
617 skb = dev_alloc_skb(pkt_len);
618 if (skb == NULL) {
619 printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
620 break;
623 skb->dev = dev;
624 skb_reserve(skb, 2); /* 16 byte align the IP fields. */
626 eth_copy_and_sum(skb, priv->rxd[entry].buf, pkt_len, 0);
627 skb_put(skb, pkt_len);
629 skb->protocol = eth_type_trans(skb, dev);
630 netif_rx(skb);
632 /* some network statistics */
633 dev->last_rx = jiffies;
634 priv->stats.rx_bytes += pkt_len;
635 priv->stats.rx_packets++;
638 /* disable receiver */
639 // FIXME: is that ok? it can produce grave errors.
640 EU->RMAC_DMA_EN = 0; /* clear run bit */
641 EU->RMAC_DMA_STAT = EU->RMAC_DMA_STAT;
643 /* return the descriptor and buffer to receive ring */
644 priv->rxd[entry].stat = (1<<31) | (1<<30) | (1<<29); // DSC0 OWN|START|END
645 priv->rxd[entry].cntl = (1<<30) | (1<<23); // DSC1 INT|RECEIVE
646 priv->rxd[entry].cntl |= priv->nr << 16; // DSC1 CHANNEL
647 priv->rxd[entry].cntl |= DMA_BUF_SIZE; // DSC1 LEN
649 /* enable receiver */
650 EU->RMAC_DMA_EN = 0x01; /* set run bit */
652 priv->cur_rx++;
658 /**************************************************************************
659 * INTERRUPT - Interrupt routines
660 **************************************************************************/
663 * p2001_eth_data_interrupt - p2001_eth data interrupt handler
664 * @irq: the irq number
665 * @dev: the client data object
666 * @regs: snapshot of processor context
668 * The interrupt handler does all of the Rx thread work,
669 * and cleans up after the Tx thread
671 static irqreturn_t p2001_eth_data_interrupt(int irq, struct net_device *dev, struct pt_regs *regs)
673 struct p2001_eth_private *priv = dev->priv;
674 P2001_ETH_regs_ptr EU = (P2001_ETH_regs_ptr) dev->base_addr;
675 int boguscnt = 10; // max_interrupt_work
676 unsigned int handled = 0;
677 unsigned int rx_status;
678 unsigned int tx_status;
680 // printk("%s: p2001_eth_data_interrupt: start\n", dev->name);
682 spin_lock(&priv->lock);
684 do {
685 /* Rx interrupt */
686 rx_status = EU->RMAC_DMA_STAT;
687 if (rx_status & (1<<8)) {
688 // usally there is only one interrupt for multiple receives
689 p2001_eth_rx(dev);
690 handled = 1;
693 /* Tx interrupt */
694 tx_status = EU->TMAC_DMA_STAT;
695 if (tx_status & (1<<8)) {
696 // usally there is only one interrupt for multiple transmits
697 p2001_eth_tx(dev);
698 handled = 1;
700 } while (--boguscnt && ((rx_status & (1<<8)) | (tx_status & (1<<8))));
702 if (!handled) {
703 printk(KERN_INFO "%s: p2001_eth_data_interrupt: interrupt not handled\n",
704 dev->name);
705 printk(KERN_INFO "%s: p2001_eth_data_interrupt: (rx=%#8.8x tx=%#8.8x)\n",
706 dev->name, rx_status, tx_status);
707 handled = 1;
710 spin_unlock(&priv->lock);
711 return IRQ_RETVAL(handled);
716 * p2001_eth_error_interrupt - p2001_eth error interrupt handler
717 * @irq: the irq number
718 * @dev: the client data object
719 * @regs: snapshot of processor context
721 * The interrupt handler does all error tasks
723 static irqreturn_t p2001_eth_error_interrupt(int irq, struct net_device *dev, struct pt_regs *regs)
725 struct p2001_eth_private *priv = dev->priv;
726 P2001_ETH_regs_ptr EU = (P2001_ETH_regs_ptr) dev->base_addr;
727 unsigned int handled = 1;
729 spin_lock(&priv->lock);
730 if (EU->RMAC_DMA_STAT) {
731 printk("%s: p2001_eth_error_interrupt: rmac_dma_stat=%#8.8x\n", dev->name, EU->RMAC_DMA_STAT);
732 EU->RMAC_DMA_STAT |= (1<<7);
734 if (EU->TMAC_DMA_STAT) {
735 printk("%s: p2001_eth_error_interrupt: tmac_dma_stat=%#8.8x\n", dev->name, EU->TMAC_DMA_STAT);
736 EU->TMAC_DMA_STAT |= (1<<7);
738 spin_unlock(&priv->lock);
739 return IRQ_RETVAL(handled);
744 /**************************************************************************
745 * PROBE - Look for an adapter, this routine's visible to the outside
746 **************************************************************************/
749 * p2001_eth_probe - Probe for p2001 ethernet device
750 * @unit: the p2001 ethernet unit number
752 * Check and probe for p2001 net device.
753 * Get mac address and assign p2001-specific entries in the device structure.
755 struct net_device * __init p2001_eth_probe(int unit)
757 struct net_device *dev;
758 struct p2001_eth_private *priv;
759 int i, err;
761 dev = alloc_etherdev(sizeof(struct p2001_eth_private));
762 if (!dev)
763 return ERR_PTR(-ENOMEM);
764 SET_MODULE_OWNER(dev);
766 /* Configure unit specific variables */
767 priv = dev->priv;
768 dev->base_addr = p2001_eth_dev_list[unit].base_addr;
769 dev->irq = p2001_eth_dev_list[unit].irq;
770 priv->nr = p2001_eth_dev_list[unit].nr;
771 sprintf(priv->adapter_name, "%s%i", p2001_eth_name, unit);
772 request_mem_region(dev->base_addr, 0x1000, priv->adapter_name);
773 spin_lock_init(&priv->lock);
775 /* The p2001_eth-specific entries in the device structure. */
776 // init
777 dev->get_stats = &p2001_eth_get_stats;
778 // get_wireless_stats
779 dev->ethtool_ops = &p2001_eth_ethtool_ops;
780 // uninit
781 // destructor
782 dev->open = &p2001_eth_open;
783 dev->stop = &p2001_eth_stop;
784 dev->hard_start_xmit = &p2001_eth_hard_start_xmit;
785 // poll
786 // hard_header
787 // rebuild_header
788 // set_multicast_list
789 // set_mac_address
790 dev->do_ioctl = &p2001_eth_do_ioctl;
791 // set_config
792 // hard_header_cache
793 // header_cache_update
794 // change_mtu
795 dev->tx_timeout = &p2001_eth_tx_timeout;
796 // vlan_rx_register
797 // vlan_rx_add_vid
798 // vlan_rx_kill_vid
799 // hard_header_parse
800 // neigh_setup
801 // accept_fastpath
802 // poll_controller
803 // last_stats
804 ether_setup(dev);
806 err = register_netdev(dev);
807 if (err)
808 goto err_out;
809 // printk("%s: p2001_eth_probe\n", dev->name);
811 /* Set MAC filter */
812 memcpy(dev->dev_addr, p2001_eth_dev_list[unit].mac_hw_addr, ETH_ALEN);
813 //random_ether_addr(dev->dev_addr);
815 /* MII setup */
816 priv->mii.phy_id = p2001_eth_dev_list[unit].phy_id;
817 priv->mii.phy_id_mask = 0x1F;
818 priv->mii.reg_num_mask = 0x1F;
819 priv->mii.dev = dev;
820 priv->mii.mdio_read = mdio_read;
821 priv->mii.mdio_write = mdio_write;
823 /* print some information about our NIC */
824 printk(KERN_INFO "%s: ADDR %#lx, IRQ %d/%d, MAC ", dev->name, dev->base_addr, dev->irq, dev->irq+1);
825 for (i = 0; i < 5; i++)
826 printk("%2.2x:", (u8)dev->dev_addr[i]);
827 printk("%2.2x.\n", dev->dev_addr[i]);
829 printk(KERN_INFO "%s: phy_addr = %d\n", dev->name, priv->mii.phy_id);
830 printk(KERN_INFO "%s: phy ID = 0x%08x\n", dev->name,
831 (mdio_read(dev, priv->mii.phy_id, MII_PHYSID2) << 16) |
832 mdio_read(dev, priv->mii.phy_id, MII_PHYSID1));
834 netif_carrier_on(dev);
836 return dev;
838 //err_out_unregister:
839 // unregister_netdev(dev);
840 err_out:
841 release_mem_region(dev->base_addr, 0x1000);
842 free_netdev(dev);
843 return ERR_PTR(err);
848 /**************************************************************************
849 * REMOVE - Remove an adapter, this routine's visible to the outside
850 **************************************************************************/
852 static void __devexit p2001_eth_remove(struct net_device *dev)
854 // printk("%s: p2001_eth_remove\n", dev->name);
859 /**************************************************************************
860 * GET_DRVINFO - Return information about driver
861 **************************************************************************/
864 * p2001_eth_get_drvinfo - Return information about driver
865 * @dev: the net device to probe
866 * @info: container for info returned
868 * Process ethtool command such as "ethtool -i" to show information
870 static void p2001_eth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
872 strcpy(info->driver, p2001_eth_name);
873 strcpy(info->version, version);
874 sprintf(info->bus_info, "ADDR 0x%lx", dev->base_addr);
877 static struct ethtool_ops p2001_eth_ethtool_ops = {
878 .get_drvinfo = p2001_eth_get_drvinfo,
883 /**************************************************************************
884 * Module functions
885 **************************************************************************/
886 static struct net_device *p2001_eth_dev[4];
889 * init_module:
891 * When the driver is loaded as a module this function is called. We fake up
892 * a device structure with the base I/O and interrupt set as if it were being
893 * called from Space.c. This minimises the extra code that would otherwise
894 * be required.
896 * Returns 0 for success or -EIO if a card is not found. Returning an error
897 * here also causes the module to be unloaded
899 static int __init p2001_eth_init_module(void)
901 int i;
903 // printk("p2001_eth_init_module\n");
904 printk(version);
905 mdio_hard_reset();
907 // for (i = 0; i < 4; i++) {
908 for (i = 0; i < 2; i++) {
909 p2001_eth_dev[i] = p2001_eth_probe(i);
910 if (!p2001_eth_dev[i])
911 return (unsigned int)p2001_eth_dev[i];
914 return 0;
919 * cleanup_module:
921 * The module is being unloaded. We unhook our network device from the system
922 * and then free up the resources we took when the card was found.
924 static void __exit p2001_eth_cleanup_module(void)
926 // printk("p2001_eth_cleanup_module\n");
929 module_init(p2001_eth_init_module);
930 module_exit(p2001_eth_cleanup_module);
932 MODULE_AUTHOR("Tobias Lorenz");
933 MODULE_DESCRIPTION("P2001 ethernet unit driver");
934 MODULE_LICENSE("GPL");