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>
16 #include <linux/platform_device.h>
18 #include <asm/mips-boards/simint.h>
20 #include "mipsnet.h" /* actual device IO mapping */
22 #define MIPSNET_VERSION "2005-06-20"
24 #define mipsnet_reg_address(dev, field) (dev->base_addr + field_offset(field))
27 struct net_device_stats stats;
30 static struct platform_device *mips_plat_dev;
32 static char mipsnet_string[] = "mipsnet";
35 * Copy data from the MIPSNET rx data port
37 static int ioiocpy_frommipsnet(struct net_device *dev, unsigned char *kdata,
40 uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount));
41 if (available_len < len)
44 for (; len > 0; len--, kdata++) {
45 *kdata = inb(mipsnet_reg_address(dev, rxDataBuffer));
48 return inl(mipsnet_reg_address(dev, rxDataCount));
51 static inline ssize_t mipsnet_put_todevice(struct net_device *dev,
54 int count_to_go = skb->len;
55 char *buf_ptr = skb->data;
56 struct mipsnet_priv *mp = netdev_priv(dev);
58 pr_debug("%s: %s(): telling MIPSNET txDataCount(%d)\n",
59 dev->name, __FUNCTION__, skb->len);
61 outl(skb->len, mipsnet_reg_address(dev, txDataCount));
63 pr_debug("%s: %s(): sending data to MIPSNET txDataBuffer(%d)\n",
64 dev->name, __FUNCTION__, skb->len);
66 for (; count_to_go; buf_ptr++, count_to_go--) {
67 outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer));
70 mp->stats.tx_packets++;
71 mp->stats.tx_bytes += skb->len;
76 static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev)
78 pr_debug("%s:%s(): transmitting %d bytes\n",
79 dev->name, __FUNCTION__, skb->len);
81 /* Only one packet at a time. Once TXDONE interrupt is serviced, the
82 * queue will be restarted.
84 netif_stop_queue(dev);
85 mipsnet_put_todevice(dev, skb);
90 static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count)
94 struct mipsnet_priv *mp = netdev_priv(dev);
96 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
97 mp->stats.rx_dropped++;
102 if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
106 skb->protocol = eth_type_trans(skb, dev);
107 skb->ip_summed = CHECKSUM_UNNECESSARY;
109 pr_debug("%s:%s(): pushing RXed data to kernel\n",
110 dev->name, __FUNCTION__);
113 mp->stats.rx_packets++;
114 mp->stats.rx_bytes += len;
120 mipsnet_interrupt(int irq, void *dev_id, struct pt_regs *regs)
122 struct net_device *dev = dev_id;
124 irqreturn_t retval = IRQ_NONE;
125 uint64_t interruptFlags;
127 if (irq == dev->irq) {
128 pr_debug("%s:%s(): irq %d for device\n",
129 dev->name, __FUNCTION__, irq);
131 retval = IRQ_HANDLED;
134 inl(mipsnet_reg_address(dev, interruptControl));
135 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
136 __FUNCTION__, interruptFlags);
138 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
139 pr_debug("%s:%s(): got TXDone\n",
140 dev->name, __FUNCTION__);
141 outl(MIPSNET_INTCTL_TXDONE,
142 mipsnet_reg_address(dev, interruptControl));
143 // only one packet at a time, we are done.
144 netif_wake_queue(dev);
145 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
146 pr_debug("%s:%s(): got RX data\n",
147 dev->name, __FUNCTION__);
148 mipsnet_get_fromdev(dev,
149 inl(mipsnet_reg_address(dev, rxDataCount)));
150 pr_debug("%s:%s(): clearing RX int\n",
151 dev->name, __FUNCTION__);
152 outl(MIPSNET_INTCTL_RXDONE,
153 mipsnet_reg_address(dev, interruptControl));
155 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
156 pr_debug("%s:%s(): got test interrupt\n",
157 dev->name, __FUNCTION__);
158 // TESTBIT is cleared on read.
159 // And takes effect after a write with 0
160 outl(0, mipsnet_reg_address(dev, interruptControl));
162 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
163 dev->name, __FUNCTION__, interruptFlags);
164 // Maybe shared IRQ, just ignore, no clearing.
169 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
170 dev->name, __FUNCTION__, irq);
174 } //mipsnet_interrupt()
176 static int mipsnet_open(struct net_device *dev)
179 pr_debug("%s: mipsnet_open\n", dev->name);
181 err = request_irq(dev->irq, &mipsnet_interrupt,
182 IRQF_SHARED, dev->name, (void *) dev);
185 pr_debug("%s: %s(): can't get irq %d\n",
186 dev->name, __FUNCTION__, dev->irq);
187 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
191 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
192 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
195 netif_start_queue(dev);
197 // test interrupt handler
198 outl(MIPSNET_INTCTL_TESTBIT,
199 mipsnet_reg_address(dev, interruptControl));
205 static int mipsnet_close(struct net_device *dev)
207 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
208 netif_stop_queue(dev);
212 static struct net_device_stats *mipsnet_get_stats(struct net_device *dev)
214 struct mipsnet_priv *mp = netdev_priv(dev);
219 static void mipsnet_set_mclist(struct net_device *dev)
221 // we don't do anything
225 static int __init mipsnet_probe(struct device *dev)
227 struct net_device *netdev;
230 netdev = alloc_etherdev(sizeof(struct mipsnet_priv));
236 dev_set_drvdata(dev, netdev);
238 netdev->open = mipsnet_open;
239 netdev->stop = mipsnet_close;
240 netdev->hard_start_xmit = mipsnet_xmit;
241 netdev->get_stats = mipsnet_get_stats;
242 netdev->set_multicast_list = mipsnet_set_mclist;
245 * TODO: probe for these or load them from PARAM
247 netdev->base_addr = 0x4200;
248 netdev->irq = MIPSCPU_INT_BASE + MIPSCPU_INT_MB0 +
249 inl(mipsnet_reg_address(netdev, interruptInfo));
251 // Get the io region now, get irq on open()
252 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
253 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
254 "for dev is not availble.\n", netdev->name,
255 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
257 goto out_free_netdev;
261 * Lacking any better mechanism to allocate a MAC address we use a
264 random_ether_addr(netdev->dev_addr);
266 err = register_netdev(netdev);
268 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
269 goto out_free_region;
275 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
284 static int __devexit mipsnet_device_remove(struct device *device)
286 struct net_device *dev = dev_get_drvdata(device);
288 unregister_netdev(dev);
289 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
291 dev_set_drvdata(device, NULL);
296 static struct device_driver mipsnet_driver = {
297 .name = mipsnet_string,
298 .bus = &platform_bus_type,
299 .probe = mipsnet_probe,
300 .remove = __devexit_p(mipsnet_device_remove),
303 static void mipsnet_platform_release(struct device *device)
305 struct platform_device *pldev;
308 pldev = to_platform_device(device);
312 static int __init mipsnet_init_module(void)
314 struct platform_device *pldev;
317 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
318 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
320 if (driver_register(&mipsnet_driver)) {
321 printk(KERN_ERR "Driver registration failed\n");
326 if (!(pldev = kmalloc (sizeof (*pldev), GFP_KERNEL))) {
328 goto out_unregister_driver;
331 memset (pldev, 0, sizeof (*pldev));
332 pldev->name = mipsnet_string;
334 pldev->dev.release = mipsnet_platform_release;
336 if (platform_device_register(pldev)) {
341 if (!pldev->dev.driver) {
343 * The driver was not bound to this device, there was
344 * no hardware at this address. Unregister it, as the
345 * release fuction will take care of freeing the
346 * allocated structure
348 platform_device_unregister (pldev);
351 mips_plat_dev = pldev;
358 out_unregister_driver:
359 driver_unregister(&mipsnet_driver);
364 static void __exit mipsnet_exit_module(void)
366 pr_debug("MIPSNet Ethernet driver exiting\n");
368 driver_unregister(&mipsnet_driver);
371 module_init(mipsnet_init_module);
372 module_exit(mipsnet_exit_module);