fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / drivers / net / mipsnet.c
blob37707a0c0498cd7f5b65269ba8150af5e4f9a651
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/io.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #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,
30 int len)
32 uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount));
33 if (available_len < len)
34 return -EFAULT;
36 for (; len > 0; len--, kdata++)
37 *kdata = inb(mipsnet_reg_address(dev, rxDataBuffer));
39 return inl(mipsnet_reg_address(dev, rxDataCount));
42 static inline ssize_t mipsnet_put_todevice(struct net_device *dev,
43 struct sk_buff *skb)
45 int count_to_go = skb->len;
46 char *buf_ptr = skb->data;
48 pr_debug("%s: %s(): telling MIPSNET txDataCount(%d)\n",
49 dev->name, __FUNCTION__, skb->len);
51 outl(skb->len, mipsnet_reg_address(dev, txDataCount));
53 pr_debug("%s: %s(): sending data to MIPSNET txDataBuffer(%d)\n",
54 dev->name, __FUNCTION__, skb->len);
56 for (; count_to_go; buf_ptr++, count_to_go--)
57 outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer));
59 dev->stats.tx_packets++;
60 dev->stats.tx_bytes += skb->len;
62 return skb->len;
65 static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev)
67 pr_debug("%s:%s(): transmitting %d bytes\n",
68 dev->name, __FUNCTION__, skb->len);
70 /* Only one packet at a time. Once TXDONE interrupt is serviced, the
71 * queue will be restarted.
73 netif_stop_queue(dev);
74 mipsnet_put_todevice(dev, skb);
76 return 0;
79 static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count)
81 struct sk_buff *skb;
82 size_t len = count;
84 skb = alloc_skb(len + 2, GFP_KERNEL);
85 if (!skb) {
86 dev->stats.rx_dropped++;
87 return -ENOMEM;
90 skb_reserve(skb, 2);
91 if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
92 return -EFAULT;
94 skb->protocol = eth_type_trans(skb, dev);
95 skb->ip_summed = CHECKSUM_UNNECESSARY;
97 pr_debug("%s:%s(): pushing RXed data to kernel\n",
98 dev->name, __FUNCTION__);
99 netif_rx(skb);
101 dev->stats.rx_packets++;
102 dev->stats.rx_bytes += len;
104 return count;
107 static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
109 struct net_device *dev = dev_id;
111 irqreturn_t retval = IRQ_NONE;
112 uint64_t interruptFlags;
114 if (irq == dev->irq) {
115 pr_debug("%s:%s(): irq %d for device\n",
116 dev->name, __FUNCTION__, irq);
118 retval = IRQ_HANDLED;
120 interruptFlags =
121 inl(mipsnet_reg_address(dev, interruptControl));
122 pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
123 __FUNCTION__, interruptFlags);
125 if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
126 pr_debug("%s:%s(): got TXDone\n",
127 dev->name, __FUNCTION__);
128 outl(MIPSNET_INTCTL_TXDONE,
129 mipsnet_reg_address(dev, interruptControl));
130 /* only one packet at a time, we are done. */
131 netif_wake_queue(dev);
132 } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
133 pr_debug("%s:%s(): got RX data\n",
134 dev->name, __FUNCTION__);
135 mipsnet_get_fromdev(dev,
136 inl(mipsnet_reg_address(dev, rxDataCount)));
137 pr_debug("%s:%s(): clearing RX int\n",
138 dev->name, __FUNCTION__);
139 outl(MIPSNET_INTCTL_RXDONE,
140 mipsnet_reg_address(dev, interruptControl));
142 } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
143 pr_debug("%s:%s(): got test interrupt\n",
144 dev->name, __FUNCTION__);
146 * TESTBIT is cleared on read.
147 * And takes effect after a write with 0
149 outl(0, mipsnet_reg_address(dev, interruptControl));
150 } else {
151 pr_debug("%s:%s(): no valid fags 0x%016llx\n",
152 dev->name, __FUNCTION__, interruptFlags);
153 /* Maybe shared IRQ, just ignore, no clearing. */
154 retval = IRQ_NONE;
157 } else {
158 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
159 dev->name, __FUNCTION__, irq);
160 retval = IRQ_NONE;
162 return retval;
165 static int mipsnet_open(struct net_device *dev)
167 int err;
168 pr_debug("%s: mipsnet_open\n", dev->name);
170 err = request_irq(dev->irq, &mipsnet_interrupt,
171 IRQF_SHARED, dev->name, (void *) dev);
173 if (err) {
174 pr_debug("%s: %s(): can't get irq %d\n",
175 dev->name, __FUNCTION__, dev->irq);
176 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
177 return err;
180 pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
181 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
184 netif_start_queue(dev);
186 /* test interrupt handler */
187 outl(MIPSNET_INTCTL_TESTBIT,
188 mipsnet_reg_address(dev, interruptControl));
191 return 0;
194 static int mipsnet_close(struct net_device *dev)
196 pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
197 netif_stop_queue(dev);
198 return 0;
201 static void mipsnet_set_mclist(struct net_device *dev)
205 static int __init mipsnet_probe(struct device *dev)
207 struct net_device *netdev;
208 int err;
210 netdev = alloc_etherdev(0);
211 if (!netdev) {
212 err = -ENOMEM;
213 goto out;
216 dev_set_drvdata(dev, netdev);
218 netdev->open = mipsnet_open;
219 netdev->stop = mipsnet_close;
220 netdev->hard_start_xmit = mipsnet_xmit;
221 netdev->set_multicast_list = mipsnet_set_mclist;
224 * TODO: probe for these or load them from PARAM
226 netdev->base_addr = 0x4200;
227 netdev->irq = MIPS_CPU_IRQ_BASE + MIPSCPU_INT_MB0 +
228 inl(mipsnet_reg_address(netdev, interruptInfo));
230 /* Get the io region now, get irq on open() */
231 if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
232 pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
233 "for dev is not availble.\n", netdev->name,
234 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
235 err = -EBUSY;
236 goto out_free_netdev;
240 * Lacking any better mechanism to allocate a MAC address we use a
241 * random one ...
243 random_ether_addr(netdev->dev_addr);
245 err = register_netdev(netdev);
246 if (err) {
247 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
248 goto out_free_region;
251 return 0;
253 out_free_region:
254 release_region(netdev->base_addr, MIPSNET_IO_EXTENT);
256 out_free_netdev:
257 free_netdev(netdev);
259 out:
260 return err;
263 static int __devexit mipsnet_device_remove(struct device *device)
265 struct net_device *dev = dev_get_drvdata(device);
267 unregister_netdev(dev);
268 release_region(dev->base_addr, MIPSNET_IO_EXTENT);
269 free_netdev(dev);
270 dev_set_drvdata(device, NULL);
272 return 0;
275 static struct device_driver mipsnet_driver = {
276 .name = mipsnet_string,
277 .bus = &platform_bus_type,
278 .probe = mipsnet_probe,
279 .remove = __devexit_p(mipsnet_device_remove),
282 static int __init mipsnet_init_module(void)
284 int err;
286 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
287 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
289 err = driver_register(&mipsnet_driver);
290 if (err)
291 printk(KERN_ERR "Driver registration failed\n");
293 return err;
296 static void __exit mipsnet_exit_module(void)
298 pr_debug("MIPSNet Ethernet driver exiting\n");
300 driver_unregister(&mipsnet_driver);
303 module_init(mipsnet_init_module);
304 module_exit(mipsnet_exit_module);