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/etherdevice.h>
14 #include <linux/netdevice.h>
15 #include <linux/platform_device.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;
118 static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
120 struct net_device *dev = dev_id;
122 irqreturn_t retval = IRQ_NONE;
123 uint64_t interruptFlags;
125 if (irq == dev->irq) {
126 pr_debug("%s:%s(): irq %d for device\n",
127 dev->name, __FUNCTION__, irq);
129 retval = IRQ_HANDLED;
132 inl(mipsnet_reg_address(dev, interruptControl));
133 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
134 __FUNCTION__, interruptFlags);
136 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
137 pr_debug("%s:%s(): got TXDone\n",
138 dev->name, __FUNCTION__);
139 outl(MIPSNET_INTCTL_TXDONE,
140 mipsnet_reg_address(dev, interruptControl));
141 // only one packet at a time, we are done.
142 netif_wake_queue(dev);
143 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
144 pr_debug("%s:%s(): got RX data\n",
145 dev->name, __FUNCTION__);
146 mipsnet_get_fromdev(dev,
147 inl(mipsnet_reg_address(dev, rxDataCount)));
148 pr_debug("%s:%s(): clearing RX int\n",
149 dev->name, __FUNCTION__);
150 outl(MIPSNET_INTCTL_RXDONE,
151 mipsnet_reg_address(dev, interruptControl));
153 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
154 pr_debug("%s:%s(): got test interrupt\n",
155 dev->name, __FUNCTION__);
156 // TESTBIT is cleared on read.
157 // And takes effect after a write with 0
158 outl(0, mipsnet_reg_address(dev, interruptControl));
160 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
161 dev->name, __FUNCTION__, interruptFlags);
162 // Maybe shared IRQ, just ignore, no clearing.
167 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
168 dev->name, __FUNCTION__, irq);
172 } //mipsnet_interrupt()
174 static int mipsnet_open(struct net_device *dev)
177 pr_debug("%s: mipsnet_open\n", dev->name);
179 err = request_irq(dev->irq, &mipsnet_interrupt,
180 IRQF_SHARED, dev->name, (void *) dev);
183 pr_debug("%s: %s(): can't get irq %d\n",
184 dev->name, __FUNCTION__, dev->irq);
185 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
189 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
190 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
193 netif_start_queue(dev);
195 // test interrupt handler
196 outl(MIPSNET_INTCTL_TESTBIT,
197 mipsnet_reg_address(dev, interruptControl));
203 static int mipsnet_close(struct net_device *dev)
205 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
206 netif_stop_queue(dev);
210 static struct net_device_stats *mipsnet_get_stats(struct net_device *dev)
212 struct mipsnet_priv *mp = netdev_priv(dev);
217 static void mipsnet_set_mclist(struct net_device *dev)
219 // we don't do anything
223 static int __init mipsnet_probe(struct device *dev)
225 struct net_device *netdev;
228 netdev = alloc_etherdev(sizeof(struct mipsnet_priv));
234 dev_set_drvdata(dev, netdev);
236 netdev->open = mipsnet_open;
237 netdev->stop = mipsnet_close;
238 netdev->hard_start_xmit = mipsnet_xmit;
239 netdev->get_stats = mipsnet_get_stats;
240 netdev->set_multicast_list = mipsnet_set_mclist;
243 * TODO: probe for these or load them from PARAM
245 netdev->base_addr = 0x4200;
246 netdev->irq = MIPSCPU_INT_BASE + MIPSCPU_INT_MB0 +
247 inl(mipsnet_reg_address(netdev, interruptInfo));
249 // Get the io region now, get irq on open()
250 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
251 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
252 "for dev is not availble.\n", netdev->name,
253 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
255 goto out_free_netdev;
259 * Lacking any better mechanism to allocate a MAC address we use a
262 random_ether_addr(netdev->dev_addr);
264 err = register_netdev(netdev);
266 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
267 goto out_free_region;
273 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
282 static int __devexit mipsnet_device_remove(struct device *device)
284 struct net_device *dev = dev_get_drvdata(device);
286 unregister_netdev(dev);
287 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
289 dev_set_drvdata(device, NULL);
294 static struct device_driver mipsnet_driver = {
295 .name = mipsnet_string,
296 .bus = &platform_bus_type,
297 .probe = mipsnet_probe,
298 .remove = __devexit_p(mipsnet_device_remove),
301 static void mipsnet_platform_release(struct device *device)
303 struct platform_device *pldev;
306 pldev = to_platform_device(device);
310 static int __init mipsnet_init_module(void)
312 struct platform_device *pldev;
315 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
316 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
318 if (driver_register(&mipsnet_driver)) {
319 printk(KERN_ERR "Driver registration failed\n");
324 if (!(pldev = kmalloc (sizeof (*pldev), GFP_KERNEL))) {
326 goto out_unregister_driver;
329 memset (pldev, 0, sizeof (*pldev));
330 pldev->name = mipsnet_string;
332 pldev->dev.release = mipsnet_platform_release;
334 if (platform_device_register(pldev)) {
339 if (!pldev->dev.driver) {
341 * The driver was not bound to this device, there was
342 * no hardware at this address. Unregister it, as the
343 * release fuction will take care of freeing the
344 * allocated structure
346 platform_device_unregister (pldev);
349 mips_plat_dev = pldev;
356 out_unregister_driver:
357 driver_unregister(&mipsnet_driver);
362 static void __exit mipsnet_exit_module(void)
364 pr_debug("MIPSNet Ethernet driver exiting\n");
366 driver_unregister(&mipsnet_driver);
369 module_init(mipsnet_init_module);
370 module_exit(mipsnet_exit_module);