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))
104 skb->protocol = eth_type_trans(skb, dev);
105 skb->ip_summed = CHECKSUM_UNNECESSARY;
107 pr_debug("%s:%s(): pushing RXed data to kernel\n",
108 dev->name, __FUNCTION__);
111 mp->stats.rx_packets++;
112 mp->stats.rx_bytes += len;
117 static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
119 struct net_device *dev = dev_id;
121 irqreturn_t retval = IRQ_NONE;
122 uint64_t interruptFlags;
124 if (irq == dev->irq) {
125 pr_debug("%s:%s(): irq %d for device\n",
126 dev->name, __FUNCTION__, irq);
128 retval = IRQ_HANDLED;
131 inl(mipsnet_reg_address(dev, interruptControl));
132 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
133 __FUNCTION__, interruptFlags);
135 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
136 pr_debug("%s:%s(): got TXDone\n",
137 dev->name, __FUNCTION__);
138 outl(MIPSNET_INTCTL_TXDONE,
139 mipsnet_reg_address(dev, interruptControl));
140 // only one packet at a time, we are done.
141 netif_wake_queue(dev);
142 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
143 pr_debug("%s:%s(): got RX data\n",
144 dev->name, __FUNCTION__);
145 mipsnet_get_fromdev(dev,
146 inl(mipsnet_reg_address(dev, rxDataCount)));
147 pr_debug("%s:%s(): clearing RX int\n",
148 dev->name, __FUNCTION__);
149 outl(MIPSNET_INTCTL_RXDONE,
150 mipsnet_reg_address(dev, interruptControl));
152 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
153 pr_debug("%s:%s(): got test interrupt\n",
154 dev->name, __FUNCTION__);
155 // TESTBIT is cleared on read.
156 // And takes effect after a write with 0
157 outl(0, mipsnet_reg_address(dev, interruptControl));
159 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
160 dev->name, __FUNCTION__, interruptFlags);
161 // Maybe shared IRQ, just ignore, no clearing.
166 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
167 dev->name, __FUNCTION__, irq);
171 } //mipsnet_interrupt()
173 static int mipsnet_open(struct net_device *dev)
176 pr_debug("%s: mipsnet_open\n", dev->name);
178 err = request_irq(dev->irq, &mipsnet_interrupt,
179 IRQF_SHARED, dev->name, (void *) dev);
182 pr_debug("%s: %s(): can't get irq %d\n",
183 dev->name, __FUNCTION__, dev->irq);
184 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
188 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
189 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
192 netif_start_queue(dev);
194 // test interrupt handler
195 outl(MIPSNET_INTCTL_TESTBIT,
196 mipsnet_reg_address(dev, interruptControl));
202 static int mipsnet_close(struct net_device *dev)
204 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
205 netif_stop_queue(dev);
209 static struct net_device_stats *mipsnet_get_stats(struct net_device *dev)
211 struct mipsnet_priv *mp = netdev_priv(dev);
216 static void mipsnet_set_mclist(struct net_device *dev)
218 // we don't do anything
222 static int __init mipsnet_probe(struct device *dev)
224 struct net_device *netdev;
227 netdev = alloc_etherdev(sizeof(struct mipsnet_priv));
233 dev_set_drvdata(dev, netdev);
235 netdev->open = mipsnet_open;
236 netdev->stop = mipsnet_close;
237 netdev->hard_start_xmit = mipsnet_xmit;
238 netdev->get_stats = mipsnet_get_stats;
239 netdev->set_multicast_list = mipsnet_set_mclist;
242 * TODO: probe for these or load them from PARAM
244 netdev->base_addr = 0x4200;
245 netdev->irq = MIPSCPU_INT_BASE + MIPSCPU_INT_MB0 +
246 inl(mipsnet_reg_address(netdev, interruptInfo));
248 // Get the io region now, get irq on open()
249 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
250 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
251 "for dev is not availble.\n", netdev->name,
252 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
254 goto out_free_netdev;
258 * Lacking any better mechanism to allocate a MAC address we use a
261 random_ether_addr(netdev->dev_addr);
263 err = register_netdev(netdev);
265 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
266 goto out_free_region;
272 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
281 static int __devexit mipsnet_device_remove(struct device *device)
283 struct net_device *dev = dev_get_drvdata(device);
285 unregister_netdev(dev);
286 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
288 dev_set_drvdata(device, NULL);
293 static struct device_driver mipsnet_driver = {
294 .name = mipsnet_string,
295 .bus = &platform_bus_type,
296 .probe = mipsnet_probe,
297 .remove = __devexit_p(mipsnet_device_remove),
300 static void mipsnet_platform_release(struct device *device)
302 struct platform_device *pldev;
305 pldev = to_platform_device(device);
309 static int __init mipsnet_init_module(void)
311 struct platform_device *pldev;
314 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
315 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
317 if (driver_register(&mipsnet_driver)) {
318 printk(KERN_ERR "Driver registration failed\n");
323 if (!(pldev = kmalloc (sizeof (*pldev), GFP_KERNEL))) {
325 goto out_unregister_driver;
328 memset (pldev, 0, sizeof (*pldev));
329 pldev->name = mipsnet_string;
331 pldev->dev.release = mipsnet_platform_release;
333 if (platform_device_register(pldev)) {
338 if (!pldev->dev.driver) {
340 * The driver was not bound to this device, there was
341 * no hardware at this address. Unregister it, as the
342 * release fuction will take care of freeing the
343 * allocated structure
345 platform_device_unregister (pldev);
348 mips_plat_dev = pldev;
355 out_unregister_driver:
356 driver_unregister(&mipsnet_driver);
361 static void __exit mipsnet_exit_module(void)
363 pr_debug("MIPSNet Ethernet driver exiting\n");
365 driver_unregister(&mipsnet_driver);
368 module_init(mipsnet_init_module);
369 module_exit(mipsnet_exit_module);