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 char mipsnet_string[] = "mipsnet";
32 * Copy data from the MIPSNET rx data port
34 static int ioiocpy_frommipsnet(struct net_device *dev, unsigned char *kdata,
37 uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount));
38 if (available_len < len)
41 for (; len > 0; len--, kdata++) {
42 *kdata = inb(mipsnet_reg_address(dev, rxDataBuffer));
45 return inl(mipsnet_reg_address(dev, rxDataCount));
48 static inline ssize_t mipsnet_put_todevice(struct net_device *dev,
51 int count_to_go = skb->len;
52 char *buf_ptr = skb->data;
53 struct mipsnet_priv *mp = netdev_priv(dev);
55 pr_debug("%s: %s(): telling MIPSNET txDataCount(%d)\n",
56 dev->name, __FUNCTION__, skb->len);
58 outl(skb->len, mipsnet_reg_address(dev, txDataCount));
60 pr_debug("%s: %s(): sending data to MIPSNET txDataBuffer(%d)\n",
61 dev->name, __FUNCTION__, skb->len);
63 for (; count_to_go; buf_ptr++, count_to_go--) {
64 outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer));
67 mp->stats.tx_packets++;
68 mp->stats.tx_bytes += skb->len;
73 static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev)
75 pr_debug("%s:%s(): transmitting %d bytes\n",
76 dev->name, __FUNCTION__, skb->len);
78 /* Only one packet at a time. Once TXDONE interrupt is serviced, the
79 * queue will be restarted.
81 netif_stop_queue(dev);
82 mipsnet_put_todevice(dev, skb);
87 static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count)
91 struct mipsnet_priv *mp = netdev_priv(dev);
93 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
94 mp->stats.rx_dropped++;
99 if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
102 skb->protocol = eth_type_trans(skb, dev);
103 skb->ip_summed = CHECKSUM_UNNECESSARY;
105 pr_debug("%s:%s(): pushing RXed data to kernel\n",
106 dev->name, __FUNCTION__);
109 mp->stats.rx_packets++;
110 mp->stats.rx_bytes += len;
115 static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
117 struct net_device *dev = dev_id;
119 irqreturn_t retval = IRQ_NONE;
120 uint64_t interruptFlags;
122 if (irq == dev->irq) {
123 pr_debug("%s:%s(): irq %d for device\n",
124 dev->name, __FUNCTION__, irq);
126 retval = IRQ_HANDLED;
129 inl(mipsnet_reg_address(dev, interruptControl));
130 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
131 __FUNCTION__, interruptFlags);
133 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
134 pr_debug("%s:%s(): got TXDone\n",
135 dev->name, __FUNCTION__);
136 outl(MIPSNET_INTCTL_TXDONE,
137 mipsnet_reg_address(dev, interruptControl));
138 // only one packet at a time, we are done.
139 netif_wake_queue(dev);
140 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
141 pr_debug("%s:%s(): got RX data\n",
142 dev->name, __FUNCTION__);
143 mipsnet_get_fromdev(dev,
144 inl(mipsnet_reg_address(dev, rxDataCount)));
145 pr_debug("%s:%s(): clearing RX int\n",
146 dev->name, __FUNCTION__);
147 outl(MIPSNET_INTCTL_RXDONE,
148 mipsnet_reg_address(dev, interruptControl));
150 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
151 pr_debug("%s:%s(): got test interrupt\n",
152 dev->name, __FUNCTION__);
153 // TESTBIT is cleared on read.
154 // And takes effect after a write with 0
155 outl(0, mipsnet_reg_address(dev, interruptControl));
157 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
158 dev->name, __FUNCTION__, interruptFlags);
159 // Maybe shared IRQ, just ignore, no clearing.
164 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
165 dev->name, __FUNCTION__, irq);
169 } //mipsnet_interrupt()
171 static int mipsnet_open(struct net_device *dev)
174 pr_debug("%s: mipsnet_open\n", dev->name);
176 err = request_irq(dev->irq, &mipsnet_interrupt,
177 IRQF_SHARED, dev->name, (void *) dev);
180 pr_debug("%s: %s(): can't get irq %d\n",
181 dev->name, __FUNCTION__, dev->irq);
182 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
186 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
187 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
190 netif_start_queue(dev);
192 // test interrupt handler
193 outl(MIPSNET_INTCTL_TESTBIT,
194 mipsnet_reg_address(dev, interruptControl));
200 static int mipsnet_close(struct net_device *dev)
202 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
203 netif_stop_queue(dev);
207 static struct net_device_stats *mipsnet_get_stats(struct net_device *dev)
209 struct mipsnet_priv *mp = netdev_priv(dev);
214 static void mipsnet_set_mclist(struct net_device *dev)
216 // we don't do anything
220 static int __init mipsnet_probe(struct device *dev)
222 struct net_device *netdev;
225 netdev = alloc_etherdev(sizeof(struct mipsnet_priv));
231 dev_set_drvdata(dev, netdev);
233 netdev->open = mipsnet_open;
234 netdev->stop = mipsnet_close;
235 netdev->hard_start_xmit = mipsnet_xmit;
236 netdev->get_stats = mipsnet_get_stats;
237 netdev->set_multicast_list = mipsnet_set_mclist;
240 * TODO: probe for these or load them from PARAM
242 netdev->base_addr = 0x4200;
243 netdev->irq = MIPS_CPU_IRQ_BASE + MIPSCPU_INT_MB0 +
244 inl(mipsnet_reg_address(netdev, interruptInfo));
246 // Get the io region now, get irq on open()
247 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
248 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
249 "for dev is not availble.\n", netdev->name,
250 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
252 goto out_free_netdev;
256 * Lacking any better mechanism to allocate a MAC address we use a
259 random_ether_addr(netdev->dev_addr);
261 err = register_netdev(netdev);
263 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
264 goto out_free_region;
270 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
279 static int __devexit mipsnet_device_remove(struct device *device)
281 struct net_device *dev = dev_get_drvdata(device);
283 unregister_netdev(dev);
284 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
286 dev_set_drvdata(device, NULL);
291 static struct device_driver mipsnet_driver = {
292 .name = mipsnet_string,
293 .bus = &platform_bus_type,
294 .probe = mipsnet_probe,
295 .remove = __devexit_p(mipsnet_device_remove),
298 static int __init mipsnet_init_module(void)
302 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
303 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
305 err = driver_register(&mipsnet_driver);
307 printk(KERN_ERR "Driver registration failed\n");
312 static void __exit mipsnet_exit_module(void)
314 pr_debug("MIPSNet Ethernet driver exiting\n");
316 driver_unregister(&mipsnet_driver);
319 module_init(mipsnet_init_module);
320 module_exit(mipsnet_exit_module);