mipsnet.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#define DEBUG

#include <linux/init.h>
R
Ralf Baechle 已提交
10
#include <linux/io.h>
11 12 13 14
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
15
#include <linux/platform_device.h>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <asm/mips-boards/simint.h>

#include "mipsnet.h"		/* actual device IO mapping */

#define MIPSNET_VERSION "2005-06-20"

#define mipsnet_reg_address(dev, field) (dev->base_addr + field_offset(field))

static char mipsnet_string[] = "mipsnet";

/*
 * Copy data from the MIPSNET rx data port
 */
static int ioiocpy_frommipsnet(struct net_device *dev, unsigned char *kdata,
			int len)
{
	uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount));
	if (available_len < len)
		return -EFAULT;

R
Ralf Baechle 已提交
36
	for (; len > 0; len--, kdata++)
37 38 39 40 41 42 43 44 45 46 47 48
		*kdata = inb(mipsnet_reg_address(dev, rxDataBuffer));

	return inl(mipsnet_reg_address(dev, rxDataCount));
}

static inline ssize_t mipsnet_put_todevice(struct net_device *dev,
	struct sk_buff *skb)
{
	int count_to_go = skb->len;
	char *buf_ptr = skb->data;

	pr_debug("%s: %s(): telling MIPSNET txDataCount(%d)\n",
R
Ralf Baechle 已提交
49
		 dev->name, __FUNCTION__, skb->len);
50 51 52 53

	outl(skb->len, mipsnet_reg_address(dev, txDataCount));

	pr_debug("%s: %s(): sending data to MIPSNET txDataBuffer(%d)\n",
R
Ralf Baechle 已提交
54
		 dev->name, __FUNCTION__, skb->len);
55

R
Ralf Baechle 已提交
56
	for (; count_to_go; buf_ptr++, count_to_go--)
57 58
		outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer));

59 60
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
61 62 63 64 65 66 67

	return skb->len;
}

static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev)
{
	pr_debug("%s:%s(): transmitting %d bytes\n",
R
Ralf Baechle 已提交
68
		 dev->name, __FUNCTION__, skb->len);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

	/* Only one packet at a time. Once TXDONE interrupt is serviced, the
	 * queue will be restarted.
	 */
	netif_stop_queue(dev);
	mipsnet_put_todevice(dev, skb);

	return 0;
}

static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count)
{
	struct sk_buff *skb;
	size_t len = count;

R
Ralf Baechle 已提交
84 85
	skb = alloc_skb(len + 2, GFP_KERNEL);
	if (!skb) {
86
		dev->stats.rx_dropped++;
87 88 89 90 91 92 93 94 95 96 97
		return -ENOMEM;
	}

	skb_reserve(skb, 2);
	if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
		return -EFAULT;

	skb->protocol = eth_type_trans(skb, dev);
	skb->ip_summed = CHECKSUM_UNNECESSARY;

	pr_debug("%s:%s(): pushing RXed data to kernel\n",
R
Ralf Baechle 已提交
98
		 dev->name, __FUNCTION__);
99 100
	netif_rx(skb);

101 102
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += len;
103 104 105 106

	return count;
}

107
static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
108 109 110 111 112 113 114 115
{
	struct net_device *dev = dev_id;

	irqreturn_t retval = IRQ_NONE;
	uint64_t interruptFlags;

	if (irq == dev->irq) {
		pr_debug("%s:%s(): irq %d for device\n",
R
Ralf Baechle 已提交
116
			 dev->name, __FUNCTION__, irq);
117 118 119 120 121 122

		retval = IRQ_HANDLED;

		interruptFlags =
		    inl(mipsnet_reg_address(dev, interruptControl));
		pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name,
R
Ralf Baechle 已提交
123
			 __FUNCTION__, interruptFlags);
124 125 126

		if (interruptFlags & MIPSNET_INTCTL_TXDONE) {
			pr_debug("%s:%s(): got TXDone\n",
R
Ralf Baechle 已提交
127
				 dev->name, __FUNCTION__);
128 129
			outl(MIPSNET_INTCTL_TXDONE,
			     mipsnet_reg_address(dev, interruptControl));
R
Ralf Baechle 已提交
130
			/* only one packet at a time, we are done. */
131 132 133
			netif_wake_queue(dev);
		} else if (interruptFlags & MIPSNET_INTCTL_RXDONE) {
			pr_debug("%s:%s(): got RX data\n",
R
Ralf Baechle 已提交
134
				 dev->name, __FUNCTION__);
135
			mipsnet_get_fromdev(dev,
R
Ralf Baechle 已提交
136
				    inl(mipsnet_reg_address(dev, rxDataCount)));
137
			pr_debug("%s:%s(): clearing RX int\n",
R
Ralf Baechle 已提交
138
				 dev->name, __FUNCTION__);
139 140 141 142 143
			outl(MIPSNET_INTCTL_RXDONE,
			     mipsnet_reg_address(dev, interruptControl));

		} else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) {
			pr_debug("%s:%s(): got test interrupt\n",
R
Ralf Baechle 已提交
144 145 146 147 148
				 dev->name, __FUNCTION__);
			/*
			 * TESTBIT is cleared on read.
			 * And takes effect after a write with 0
			 */
149 150 151
			outl(0, mipsnet_reg_address(dev, interruptControl));
		} else {
			pr_debug("%s:%s(): no valid fags 0x%016llx\n",
R
Ralf Baechle 已提交
152 153
				 dev->name, __FUNCTION__, interruptFlags);
			/* Maybe shared IRQ, just ignore, no clearing. */
154 155 156 157 158 159 160 161 162
			retval = IRQ_NONE;
		}

	} else {
		printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
		       dev->name, __FUNCTION__, irq);
		retval = IRQ_NONE;
	}
	return retval;
R
Ralf Baechle 已提交
163
}
164 165 166 167 168 169 170

static int mipsnet_open(struct net_device *dev)
{
	int err;
	pr_debug("%s: mipsnet_open\n", dev->name);

	err = request_irq(dev->irq, &mipsnet_interrupt,
171
			  IRQF_SHARED, dev->name, (void *) dev);
172 173 174

	if (err) {
		pr_debug("%s: %s(): can't get irq %d\n",
R
Ralf Baechle 已提交
175
			 dev->name, __FUNCTION__, dev->irq);
176 177 178 179 180
		release_region(dev->base_addr, MIPSNET_IO_EXTENT);
		return err;
	}

	pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n",
R
Ralf Baechle 已提交
181
		 dev->name, __FUNCTION__, dev->base_addr, dev->irq);
182 183 184 185


	netif_start_queue(dev);

R
Ralf Baechle 已提交
186
	/* test interrupt handler */
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	outl(MIPSNET_INTCTL_TESTBIT,
	     mipsnet_reg_address(dev, interruptControl));


	return 0;
}

static int mipsnet_close(struct net_device *dev)
{
	pr_debug("%s: %s()\n", dev->name, __FUNCTION__);
	netif_stop_queue(dev);
	return 0;
}

static void mipsnet_set_mclist(struct net_device *dev)
{
}

static int __init mipsnet_probe(struct device *dev)
{
	struct net_device *netdev;
	int err;

210
	netdev = alloc_etherdev(0);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	if (!netdev) {
		err = -ENOMEM;
		goto out;
	}

	dev_set_drvdata(dev, netdev);

	netdev->open			= mipsnet_open;
	netdev->stop			= mipsnet_close;
	netdev->hard_start_xmit		= mipsnet_xmit;
	netdev->set_multicast_list	= mipsnet_set_mclist;

	/*
	 * TODO: probe for these or load them from PARAM
	 */
	netdev->base_addr = 0x4200;
227
	netdev->irq = MIPS_CPU_IRQ_BASE + MIPSCPU_INT_MB0 +
R
Ralf Baechle 已提交
228
		      inl(mipsnet_reg_address(netdev, interruptInfo));
229

R
Ralf Baechle 已提交
230
	/* Get the io region now, get irq on open() */
231 232
	if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) {
		pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} "
R
Ralf Baechle 已提交
233 234
			 "for dev is not availble.\n", netdev->name,
			 __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT);
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		err = -EBUSY;
		goto out_free_netdev;
	}

	/*
	 * Lacking any better mechanism to allocate a MAC address we use a
	 * random one ...
	 */
	random_ether_addr(netdev->dev_addr);

	err = register_netdev(netdev);
	if (err) {
		printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
		goto out_free_region;
	}

	return 0;

out_free_region:
	release_region(netdev->base_addr, MIPSNET_IO_EXTENT);

out_free_netdev:
	free_netdev(netdev);

out:
	return err;
}

static int __devexit mipsnet_device_remove(struct device *device)
{
	struct net_device *dev = dev_get_drvdata(device);

	unregister_netdev(dev);
	release_region(dev->base_addr, MIPSNET_IO_EXTENT);
	free_netdev(dev);
	dev_set_drvdata(device, NULL);

	return 0;
}

static struct device_driver mipsnet_driver = {
	.name	= mipsnet_string,
	.bus	= &platform_bus_type,
	.probe	= mipsnet_probe,
	.remove	= __devexit_p(mipsnet_device_remove),
};

static int __init mipsnet_init_module(void)
{
	int err;

	printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
	       "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);

289 290
	err = driver_register(&mipsnet_driver);
	if (err)
291 292 293 294 295 296 297 298 299 300 301 302 303 304
		printk(KERN_ERR "Driver registration failed\n");

	return err;
}

static void __exit mipsnet_exit_module(void)
{
	pr_debug("MIPSNet Ethernet driver exiting\n");

	driver_unregister(&mipsnet_driver);
}

module_init(mipsnet_init_module);
module_exit(mipsnet_exit_module);