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/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));
33 if (available_len < len)
36 for (; len > 0; len--, kdata++) {
37 *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 pr_debug("%s: %s(): telling MIPSNET txDataCount(%d)\n",
50 dev->name, __FUNCTION__, skb->len);
52 outl(skb->len, mipsnet_reg_address(dev, txDataCount));
54 pr_debug("%s: %s(): sending data to MIPSNET txDataBuffer(%d)\n",
55 dev->name, __FUNCTION__, skb->len);
57 for (; count_to_go; buf_ptr++, count_to_go--) {
58 outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer));
61 dev->stats.tx_packets++;
62 dev->stats.tx_bytes += skb->len;
67 static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev)
69 pr_debug("%s:%s(): transmitting %d bytes\n",
70 dev->name, __FUNCTION__, skb->len);
72 /* Only one packet at a time. Once TXDONE interrupt is serviced, the
73 * queue will be restarted.
75 netif_stop_queue(dev);
76 mipsnet_put_todevice(dev, skb);
81 static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count)
86 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
87 dev->stats.rx_dropped++;
92 if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
95 skb->protocol = eth_type_trans(skb, dev);
96 skb->ip_summed = CHECKSUM_UNNECESSARY;
98 pr_debug("%s:%s(): pushing RXed data to kernel\n",
99 dev->name, __FUNCTION__);
102 dev->stats.rx_packets++;
103 dev->stats.rx_bytes += len;
108 static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
110 struct net_device *dev = dev_id;
112 irqreturn_t retval = IRQ_NONE;
113 uint64_t interruptFlags;
115 if (irq == dev->irq) {
116 pr_debug("%s:%s(): irq %d for device\n",
117 dev->name, __FUNCTION__, irq);
119 retval = IRQ_HANDLED;
122 inl(mipsnet_reg_address(dev, interruptControl));
123 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
124 __FUNCTION__, interruptFlags);
126 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
127 pr_debug("%s:%s(): got TXDone\n",
128 dev->name, __FUNCTION__);
129 outl(MIPSNET_INTCTL_TXDONE,
130 mipsnet_reg_address(dev, interruptControl));
131 // only one packet at a time, we are done.
132 netif_wake_queue(dev);
133 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
134 pr_debug("%s:%s(): got RX data\n",
135 dev->name, __FUNCTION__);
136 mipsnet_get_fromdev(dev,
137 inl(mipsnet_reg_address(dev, rxDataCount)));
138 pr_debug("%s:%s(): clearing RX int\n",
139 dev->name, __FUNCTION__);
140 outl(MIPSNET_INTCTL_RXDONE,
141 mipsnet_reg_address(dev, interruptControl));
143 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
144 pr_debug("%s:%s(): got test interrupt\n",
145 dev->name, __FUNCTION__);
146 // TESTBIT is cleared on read.
147 // And takes effect after a write with 0
148 outl(0, mipsnet_reg_address(dev, interruptControl));
150 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
151 dev->name, __FUNCTION__, interruptFlags);
152 // Maybe shared IRQ, just ignore, no clearing.
157 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
158 dev->name, __FUNCTION__, irq);
162 } //mipsnet_interrupt()
164 static int mipsnet_open(struct net_device *dev)
167 pr_debug("%s: mipsnet_open\n", dev->name);
169 err = request_irq(dev->irq, &mipsnet_interrupt,
170 IRQF_SHARED, dev->name, (void *) dev);
173 pr_debug("%s: %s(): can't get irq %d\n",
174 dev->name, __FUNCTION__, dev->irq);
175 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
179 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
180 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
183 netif_start_queue(dev);
185 // test interrupt handler
186 outl(MIPSNET_INTCTL_TESTBIT,
187 mipsnet_reg_address(dev, interruptControl));
193 static int mipsnet_close(struct net_device *dev)
195 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
196 netif_stop_queue(dev);
200 static void mipsnet_set_mclist(struct net_device *dev)
202 // we don't do anything
206 static int __init mipsnet_probe(struct device *dev)
208 struct net_device *netdev;
211 netdev = alloc_etherdev(0);
217 dev_set_drvdata(dev, netdev);
219 netdev->open = mipsnet_open;
220 netdev->stop = mipsnet_close;
221 netdev->hard_start_xmit = mipsnet_xmit;
222 netdev->set_multicast_list = mipsnet_set_mclist;
225 * TODO: probe for these or load them from PARAM
227 netdev->base_addr = 0x4200;
228 netdev->irq = MIPS_CPU_IRQ_BASE + MIPSCPU_INT_MB0 +
229 inl(mipsnet_reg_address(netdev, interruptInfo));
231 // Get the io region now, get irq on open()
232 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
233 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
234 "for dev is not availble.\n", netdev->name,
235 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
237 goto out_free_netdev;
241 * Lacking any better mechanism to allocate a MAC address we use a
244 random_ether_addr(netdev->dev_addr);
246 err = register_netdev(netdev);
248 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
249 goto out_free_region;
255 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
264 static int __devexit mipsnet_device_remove(struct device *device)
266 struct net_device *dev = dev_get_drvdata(device);
268 unregister_netdev(dev);
269 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
271 dev_set_drvdata(device, NULL);
276 static struct device_driver mipsnet_driver = {
277 .name = mipsnet_string,
278 .bus = &platform_bus_type,
279 .probe = mipsnet_probe,
280 .remove = __devexit_p(mipsnet_device_remove),
283 static int __init mipsnet_init_module(void)
287 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
288 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
290 err = driver_register(&mipsnet_driver);
292 printk(KERN_ERR "Driver registration failed\n");
297 static void __exit mipsnet_exit_module(void)
299 pr_debug("MIPSNet Ethernet driver exiting\n");
301 driver_unregister(&mipsnet_driver);
304 module_init(mipsnet_init_module);
305 module_exit(mipsnet_exit_module);