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>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/sched.h>
14 #include <linux/etherdevice.h>
15 #include <linux/netdevice.h>
17 #include <asm/mips-boards/simint.h>
19 #include "mipsnet.h" /* actual device IO mapping */
21 #define MIPSNET_VERSION "2005-06-20"
23 #define mipsnet_reg_address(dev, field) (dev->base_addr + field_offset(field))
26 struct net_device_stats stats;
29 static struct platform_device *mips_plat_dev;
31 static char mipsnet_string[] = "mipsnet";
34 * Copy data from the MIPSNET rx data port
36 static int ioiocpy_frommipsnet(struct net_device *dev, unsigned char *kdata,
39 uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount));
40 if (available_len < len)
43 for (; len > 0; len--, kdata++) {
44 *kdata = inb(mipsnet_reg_address(dev, rxDataBuffer));
47 return inl(mipsnet_reg_address(dev, rxDataCount));
50 static inline ssize_t mipsnet_put_todevice(struct net_device *dev,
53 int count_to_go = skb->len;
54 char *buf_ptr = skb->data;
55 struct mipsnet_priv *mp = netdev_priv(dev);
57 pr_debug("%s: %s(): telling MIPSNET txDataCount(%d)\n",
58 dev->name, __FUNCTION__, skb->len);
60 outl(skb->len, mipsnet_reg_address(dev, txDataCount));
62 pr_debug("%s: %s(): sending data to MIPSNET txDataBuffer(%d)\n",
63 dev->name, __FUNCTION__, skb->len);
65 for (; count_to_go; buf_ptr++, count_to_go--) {
66 outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer));
69 mp->stats.tx_packets++;
70 mp->stats.tx_bytes += skb->len;
75 static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev)
77 pr_debug("%s:%s(): transmitting %d bytes\n",
78 dev->name, __FUNCTION__, skb->len);
80 /* Only one packet at a time. Once TXDONE interrupt is serviced, the
81 * queue will be restarted.
83 netif_stop_queue(dev);
84 mipsnet_put_todevice(dev, skb);
89 static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count)
93 struct mipsnet_priv *mp = netdev_priv(dev);
95 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
96 mp->stats.rx_dropped++;
101 if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
105 skb->protocol = eth_type_trans(skb, dev);
106 skb->ip_summed = CHECKSUM_UNNECESSARY;
108 pr_debug("%s:%s(): pushing RXed data to kernel\n",
109 dev->name, __FUNCTION__);
112 mp->stats.rx_packets++;
113 mp->stats.rx_bytes += len;
119 mipsnet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
121 struct net_device *dev = dev_id;
123 irqreturn_t retval = IRQ_NONE;
124 uint64_t interruptFlags;
126 if (irq == dev->irq) {
127 pr_debug("%s:%s(): irq %d for device\n",
128 dev->name, __FUNCTION__, irq);
130 retval = IRQ_HANDLED;
133 inl(mipsnet_reg_address(dev, interruptControl));
134 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
135 __FUNCTION__, interruptFlags);
137 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
138 pr_debug("%s:%s(): got TXDone\n",
139 dev->name, __FUNCTION__);
140 outl(MIPSNET_INTCTL_TXDONE,
141 mipsnet_reg_address(dev, interruptControl));
142 // only one packet at a time, we are done.
143 netif_wake_queue(dev);
144 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
145 pr_debug("%s:%s(): got RX data\n",
146 dev->name, __FUNCTION__);
147 mipsnet_get_fromdev(dev,
148 inl(mipsnet_reg_address(dev, rxDataCount)));
149 pr_debug("%s:%s(): clearing RX int\n",
150 dev->name, __FUNCTION__);
151 outl(MIPSNET_INTCTL_RXDONE,
152 mipsnet_reg_address(dev, interruptControl));
154 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
155 pr_debug("%s:%s(): got test interrupt\n",
156 dev->name, __FUNCTION__);
157 // TESTBIT is cleared on read.
158 // And takes effect after a write with 0
159 outl(0, mipsnet_reg_address(dev, interruptControl));
161 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
162 dev->name, __FUNCTION__, interruptFlags);
163 // Maybe shared IRQ, just ignore, no clearing.
168 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
169 dev->name, __FUNCTION__, irq);
173 } //mipsnet_interrupt()
175 static int mipsnet_open(struct net_device *dev)
178 pr_debug("%s: mipsnet_open\n", dev->name);
180 err = request_irq(dev->irq, &mipsnet_interrupt,
181 SA_SHIRQ, dev->name, (void *) dev);
184 pr_debug("%s: %s(): can't get irq %d\n",
185 dev->name, __FUNCTION__, dev->irq);
186 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
190 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
191 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
194 netif_start_queue(dev);
196 // test interrupt handler
197 outl(MIPSNET_INTCTL_TESTBIT,
198 mipsnet_reg_address(dev, interruptControl));
204 static int mipsnet_close(struct net_device *dev)
206 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
207 netif_stop_queue(dev);
211 static struct net_device_stats *mipsnet_get_stats(struct net_device *dev)
213 struct mipsnet_priv *mp = netdev_priv(dev);
218 static void mipsnet_set_mclist(struct net_device *dev)
220 // we don't do anything
224 static int __init mipsnet_probe(struct device *dev)
226 struct net_device *netdev;
229 netdev = alloc_etherdev(sizeof(struct mipsnet_priv));
235 dev_set_drvdata(dev, netdev);
237 netdev->open = mipsnet_open;
238 netdev->stop = mipsnet_close;
239 netdev->hard_start_xmit = mipsnet_xmit;
240 netdev->get_stats = mipsnet_get_stats;
241 netdev->set_multicast_list = mipsnet_set_mclist;
244 * TODO: probe for these or load them from PARAM
246 netdev->base_addr = 0x4200;
247 netdev->irq = MIPSCPU_INT_BASE + MIPSCPU_INT_MB0 +
248 inl(mipsnet_reg_address(netdev, interruptInfo));
250 // Get the io region now, get irq on open()
251 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
252 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
253 "for dev is not availble.\n", netdev->name,
254 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
256 goto out_free_netdev;
260 * Lacking any better mechanism to allocate a MAC address we use a
263 random_ether_addr(netdev->dev_addr);
265 err = register_netdev(netdev);
267 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
268 goto out_free_region;
274 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
283 static int __devexit mipsnet_device_remove(struct device *device)
285 struct net_device *dev = dev_get_drvdata(device);
287 unregister_netdev(dev);
288 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
290 dev_set_drvdata(device, NULL);
295 static struct device_driver mipsnet_driver = {
296 .name = mipsnet_string,
297 .bus = &platform_bus_type,
298 .probe = mipsnet_probe,
299 .remove = __devexit_p(mipsnet_device_remove),
302 static void mipsnet_platform_release(struct device *device)
304 struct platform_device *pldev;
307 pldev = to_platform_device(device);
311 static int __init mipsnet_init_module(void)
313 struct platform_device *pldev;
316 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
317 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
319 if (driver_register(&mipsnet_driver)) {
320 printk(KERN_ERR "Driver registration failed\n");
325 if (!(pldev = kmalloc (sizeof (*pldev), GFP_KERNEL))) {
327 goto out_unregister_driver;
330 memset (pldev, 0, sizeof (*pldev));
331 pldev->name = mipsnet_string;
333 pldev->dev.release = mipsnet_platform_release;
335 if (platform_device_register(pldev)) {
340 if (!pldev->dev.driver) {
342 * The driver was not bound to this device, there was
343 * no hardware at this address. Unregister it, as the
344 * release fuction will take care of freeing the
345 * allocated structure
347 platform_device_unregister (pldev);
350 mips_plat_dev = pldev;
357 out_unregister_driver:
358 driver_unregister(&mipsnet_driver);
363 static void __exit mipsnet_exit_module(void)
365 pr_debug("MIPSNet Ethernet driver exiting\n");
367 driver_unregister(&mipsnet_driver);
370 module_init(mipsnet_init_module);
371 module_exit(mipsnet_exit_module);