pull master
[hh.org.git] / drivers / net / mipsnet.c
blobc9469985bd713b61e4fc6d987aed10ec3bf51d34
1 /*
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
4 * for more details.
5 */
7 #define DEBUG
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>
17 #include <asm/io.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))
26 struct mipsnet_priv {
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,
38 int len)
40 uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount));
41 if (available_len < len)
42 return -EFAULT;
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,
52 struct sk_buff *skb)
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;
73 return 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);
87 return 0;
90 static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count)
92 struct sk_buff *skb;
93 size_t len = count;
94 struct mipsnet_priv *mp = netdev_priv(dev);
96 if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) {
97 mp->stats.rx_dropped++;
98 return -ENOMEM;
101 skb_reserve(skb, 2);
102 if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
103 return -EFAULT;
105 skb->dev = dev;
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__);
111 netif_rx(skb);
113 mp->stats.rx_packets++;
114 mp->stats.rx_bytes += len;
116 return count;
119 static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
121 struct net_device *dev = dev_id;
123 irqreturn_t retval = IRQ_NONE;
124 uint64_t interruptFlags;
126 if (irq == dev->irq) {
127 pr_debug("%s:%s(): irq %d for device\n",
128 dev->name, __FUNCTION__, irq);
130 retval = IRQ_HANDLED;
132 interruptFlags =
133 inl(mipsnet_reg_address(dev, interruptControl));
134 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
135 __FUNCTION__, interruptFlags);
137 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
138 pr_debug("%s:%s(): got TXDone\n",
139 dev->name, __FUNCTION__);
140 outl(MIPSNET_INTCTL_TXDONE,
141 mipsnet_reg_address(dev, interruptControl));
142 // only one packet at a time, we are done.
143 netif_wake_queue(dev);
144 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
145 pr_debug("%s:%s(): got RX data\n",
146 dev->name, __FUNCTION__);
147 mipsnet_get_fromdev(dev,
148 inl(mipsnet_reg_address(dev, rxDataCount)));
149 pr_debug("%s:%s(): clearing RX int\n",
150 dev->name, __FUNCTION__);
151 outl(MIPSNET_INTCTL_RXDONE,
152 mipsnet_reg_address(dev, interruptControl));
154 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
155 pr_debug("%s:%s(): got test interrupt\n",
156 dev->name, __FUNCTION__);
157 // TESTBIT is cleared on read.
158 // And takes effect after a write with 0
159 outl(0, mipsnet_reg_address(dev, interruptControl));
160 } else {
161 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
162 dev->name, __FUNCTION__, interruptFlags);
163 // Maybe shared IRQ, just ignore, no clearing.
164 retval = IRQ_NONE;
167 } else {
168 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
169 dev->name, __FUNCTION__, irq);
170 retval = IRQ_NONE;
172 return retval;
173 } //mipsnet_interrupt()
175 static int mipsnet_open(struct net_device *dev)
177 int err;
178 pr_debug("%s: mipsnet_open\n", dev->name);
180 err = request_irq(dev->irq, &mipsnet_interrupt,
181 IRQF_SHARED, dev->name, (void *) dev);
183 if (err) {
184 pr_debug("%s: %s(): can't get irq %d\n",
185 dev->name, __FUNCTION__, dev->irq);
186 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
187 return err;
190 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
191 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
194 netif_start_queue(dev);
196 // test interrupt handler
197 outl(MIPSNET_INTCTL_TESTBIT,
198 mipsnet_reg_address(dev, interruptControl));
201 return 0;
204 static int mipsnet_close(struct net_device *dev)
206 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
207 netif_stop_queue(dev);
208 return 0;
211 static struct net_device_stats *mipsnet_get_stats(struct net_device *dev)
213 struct mipsnet_priv *mp = netdev_priv(dev);
215 return &mp->stats;
218 static void mipsnet_set_mclist(struct net_device *dev)
220 // we don't do anything
221 return;
224 static int __init mipsnet_probe(struct device *dev)
226 struct net_device *netdev;
227 int err;
229 netdev = alloc_etherdev(sizeof(struct mipsnet_priv));
230 if (!netdev) {
231 err = -ENOMEM;
232 goto out;
235 dev_set_drvdata(dev, netdev);
237 netdev->open = mipsnet_open;
238 netdev->stop = mipsnet_close;
239 netdev->hard_start_xmit = mipsnet_xmit;
240 netdev->get_stats = mipsnet_get_stats;
241 netdev->set_multicast_list = mipsnet_set_mclist;
244 * TODO: probe for these or load them from PARAM
246 netdev->base_addr = 0x4200;
247 netdev->irq = MIPSCPU_INT_BASE + MIPSCPU_INT_MB0 +
248 inl(mipsnet_reg_address(netdev, interruptInfo));
250 // Get the io region now, get irq on open()
251 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
252 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
253 "for dev is not availble.\n", netdev->name,
254 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
255 err = -EBUSY;
256 goto out_free_netdev;
260 * Lacking any better mechanism to allocate a MAC address we use a
261 * random one ...
263 random_ether_addr(netdev->dev_addr);
265 err = register_netdev(netdev);
266 if (err) {
267 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
268 goto out_free_region;
271 return 0;
273 out_free_region:
274 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
276 out_free_netdev:
277 free_netdev(netdev);
279 out:
280 return err;
283 static int __devexit mipsnet_device_remove(struct device *device)
285 struct net_device *dev = dev_get_drvdata(device);
287 unregister_netdev(dev);
288 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
289 free_netdev(dev);
290 dev_set_drvdata(device, NULL);
292 return 0;
295 static struct device_driver mipsnet_driver = {
296 .name = mipsnet_string,
297 .bus = &platform_bus_type,
298 .probe = mipsnet_probe,
299 .remove = __devexit_p(mipsnet_device_remove),
302 static void mipsnet_platform_release(struct device *device)
304 struct platform_device *pldev;
306 /* free device */
307 pldev = to_platform_device(device);
308 kfree(pldev);
311 static int __init mipsnet_init_module(void)
313 struct platform_device *pldev;
314 int err;
316 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
317 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
319 if (driver_register(&mipsnet_driver)) {
320 printk(KERN_ERR "Driver registration failed\n");
321 err = -ENODEV;
322 goto out;
325 if (!(pldev = kmalloc (sizeof (*pldev), GFP_KERNEL))) {
326 err = -ENOMEM;
327 goto out_unregister_driver;
330 memset (pldev, 0, sizeof (*pldev));
331 pldev->name = mipsnet_string;
332 pldev->id = 0;
333 pldev->dev.release = mipsnet_platform_release;
335 if (platform_device_register(pldev)) {
336 err = -ENODEV;
337 goto out_free_pldev;
340 if (!pldev->dev.driver) {
342 * The driver was not bound to this device, there was
343 * no hardware at this address. Unregister it, as the
344 * release fuction will take care of freeing the
345 * allocated structure
347 platform_device_unregister (pldev);
350 mips_plat_dev = pldev;
352 return 0;
354 out_free_pldev:
355 kfree(pldev);
357 out_unregister_driver:
358 driver_unregister(&mipsnet_driver);
359 out:
360 return err;
363 static void __exit mipsnet_exit_module(void)
365 pr_debug("MIPSNet Ethernet driver exiting\n");
367 driver_unregister(&mipsnet_driver);
370 module_init(mipsnet_init_module);
371 module_exit(mipsnet_exit_module);