2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
9 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/platform_device.h>
16 #include <asm/mips-boards/simint.h>
18 #include "mipsnet.h" /* actual device IO mapping */
20 #define MIPSNET_VERSION "2005-06-20"
22 #define mipsnet_reg_address(dev, field) (dev->base_addr + field_offset(field))
24 static char mipsnet_string
[] = "mipsnet";
27 * Copy data from the MIPSNET rx data port
29 static int ioiocpy_frommipsnet(struct net_device
*dev
, unsigned char *kdata
,
32 uint32_t available_len
= inl(mipsnet_reg_address(dev
, rxDataCount
));
34 if (available_len
< len
)
37 for (; len
> 0; len
--, kdata
++)
38 *kdata
= inb(mipsnet_reg_address(dev
, rxDataBuffer
));
40 return inl(mipsnet_reg_address(dev
, rxDataCount
));
43 static inline ssize_t
mipsnet_put_todevice(struct net_device
*dev
,
46 int count_to_go
= skb
->len
;
47 char *buf_ptr
= skb
->data
;
49 outl(skb
->len
, mipsnet_reg_address(dev
, txDataCount
));
51 for (; count_to_go
; buf_ptr
++, count_to_go
--)
52 outb(*buf_ptr
, mipsnet_reg_address(dev
, txDataBuffer
));
54 dev
->stats
.tx_packets
++;
55 dev
->stats
.tx_bytes
+= skb
->len
;
60 static int mipsnet_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
63 * Only one packet at a time. Once TXDONE interrupt is serviced, the
64 * queue will be restarted.
66 netif_stop_queue(dev
);
67 mipsnet_put_todevice(dev
, skb
);
72 static inline ssize_t
mipsnet_get_fromdev(struct net_device
*dev
, size_t count
)
77 skb
= alloc_skb(len
+ 2, GFP_KERNEL
);
79 dev
->stats
.rx_dropped
++;
84 if (ioiocpy_frommipsnet(dev
, skb_put(skb
, len
), len
))
87 skb
->protocol
= eth_type_trans(skb
, dev
);
88 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
92 dev
->stats
.rx_packets
++;
93 dev
->stats
.rx_bytes
+= len
;
98 static irqreturn_t
mipsnet_interrupt(int irq
, void *dev_id
)
100 struct net_device
*dev
= dev_id
;
102 irqreturn_t retval
= IRQ_NONE
;
103 uint64_t interruptFlags
;
105 if (irq
== dev
->irq
) {
106 retval
= IRQ_HANDLED
;
109 inl(mipsnet_reg_address(dev
, interruptControl
));
111 if (interruptFlags
& MIPSNET_INTCTL_TXDONE
) {
112 outl(MIPSNET_INTCTL_TXDONE
,
113 mipsnet_reg_address(dev
, interruptControl
));
114 /* only one packet at a time, we are done. */
115 netif_wake_queue(dev
);
116 } else if (interruptFlags
& MIPSNET_INTCTL_RXDONE
) {
117 mipsnet_get_fromdev(dev
,
118 inl(mipsnet_reg_address(dev
, rxDataCount
)));
119 outl(MIPSNET_INTCTL_RXDONE
,
120 mipsnet_reg_address(dev
, interruptControl
));
122 } else if (interruptFlags
& MIPSNET_INTCTL_TESTBIT
) {
124 * TESTBIT is cleared on read.
125 * And takes effect after a write with 0
127 outl(0, mipsnet_reg_address(dev
, interruptControl
));
129 /* Maybe shared IRQ, just ignore, no clearing. */
134 printk(KERN_INFO
"%s: %s(): irq %d for unknown device\n",
135 dev
->name
, __FUNCTION__
, irq
);
141 static int mipsnet_open(struct net_device
*dev
)
145 err
= request_irq(dev
->irq
, &mipsnet_interrupt
,
146 IRQF_SHARED
, dev
->name
, (void *) dev
);
149 release_region(dev
->base_addr
, MIPSNET_IO_EXTENT
);
153 netif_start_queue(dev
);
155 /* test interrupt handler */
156 outl(MIPSNET_INTCTL_TESTBIT
,
157 mipsnet_reg_address(dev
, interruptControl
));
163 static int mipsnet_close(struct net_device
*dev
)
165 netif_stop_queue(dev
);
170 static void mipsnet_set_mclist(struct net_device
*dev
)
174 static int __init
mipsnet_probe(struct device
*dev
)
176 struct net_device
*netdev
;
179 netdev
= alloc_etherdev(0);
185 dev_set_drvdata(dev
, netdev
);
187 netdev
->open
= mipsnet_open
;
188 netdev
->stop
= mipsnet_close
;
189 netdev
->hard_start_xmit
= mipsnet_xmit
;
190 netdev
->set_multicast_list
= mipsnet_set_mclist
;
193 * TODO: probe for these or load them from PARAM
195 netdev
->base_addr
= 0x4200;
196 netdev
->irq
= MIPS_CPU_IRQ_BASE
+ MIPSCPU_INT_MB0
+
197 inl(mipsnet_reg_address(netdev
, interruptInfo
));
199 /* Get the io region now, get irq on open() */
200 if (!request_region(netdev
->base_addr
, MIPSNET_IO_EXTENT
, "mipsnet")) {
202 goto out_free_netdev
;
206 * Lacking any better mechanism to allocate a MAC address we use a
209 random_ether_addr(netdev
->dev_addr
);
211 err
= register_netdev(netdev
);
213 printk(KERN_ERR
"MIPSNet: failed to register netdev.\n");
214 goto out_free_region
;
220 release_region(netdev
->base_addr
, MIPSNET_IO_EXTENT
);
229 static int __devexit
mipsnet_device_remove(struct device
*device
)
231 struct net_device
*dev
= dev_get_drvdata(device
);
233 unregister_netdev(dev
);
234 release_region(dev
->base_addr
, MIPSNET_IO_EXTENT
);
236 dev_set_drvdata(device
, NULL
);
241 static struct device_driver mipsnet_driver
= {
242 .name
= mipsnet_string
,
243 .bus
= &platform_bus_type
,
244 .probe
= mipsnet_probe
,
245 .remove
= __devexit_p(mipsnet_device_remove
),
248 static int __init
mipsnet_init_module(void)
252 printk(KERN_INFO
"MIPSNet Ethernet driver. Version: %s. "
253 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION
);
255 err
= driver_register(&mipsnet_driver
);
257 printk(KERN_ERR
"Driver registration failed\n");
262 static void __exit
mipsnet_exit_module(void)
264 driver_unregister(&mipsnet_driver
);
267 module_init(mipsnet_init_module
);
268 module_exit(mipsnet_exit_module
);