]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/mipsnet.c
meth: convert to net_device_ops
[net-next-2.6.git] / drivers / net / mipsnet.c
CommitLineData
dcbf8477
RB
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 */
6
dcbf8477 7#include <linux/init.h>
c2af68e5 8#include <linux/io.h>
dcbf8477
RB
9#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/netdevice.h>
dcbf8477 12#include <linux/etherdevice.h>
d052d1be 13#include <linux/platform_device.h>
dcbf8477
RB
14#include <asm/mips-boards/simint.h>
15
c800c5c9
RB
16#define MIPSNET_VERSION "2007-11-17"
17
18/*
19 * Net status/control block as seen by sw in the core.
20 */
21struct mipsnet_regs {
22 /*
23 * Device info for probing, reads as MIPSNET%d where %d is some
24 * form of version.
25 */
26 u64 devId; /*0x00 */
dcbf8477 27
c800c5c9
RB
28 /*
29 * read only busy flag.
30 * Set and cleared by the Net Device to indicate that an rx or a tx
31 * is in progress.
32 */
33 u32 busy; /*0x08 */
dcbf8477 34
c800c5c9
RB
35 /*
36 * Set by the Net Device.
37 * The device will set it once data has been received.
38 * The value is the number of bytes that should be read from
39 * rxDataBuffer. The value will decrease till 0 until all the data
40 * from rxDataBuffer has been read.
41 */
42 u32 rxDataCount; /*0x0c */
43#define MIPSNET_MAX_RXTX_DATACOUNT (1 << 16)
44
45 /*
46 * Settable from the MIPS core, cleared by the Net Device.
47 * The core should set the number of bytes it wants to send,
48 * then it should write those bytes of data to txDataBuffer.
49 * The device will clear txDataCount has been processed (not
50 * necessarily sent).
51 */
52 u32 txDataCount; /*0x10 */
53
54 /*
55 * Interrupt control
56 *
57 * Used to clear the interrupted generated by this dev.
58 * Write a 1 to clear the interrupt. (except bit31).
59 *
60 * Bit0 is set if it was a tx-done interrupt.
61 * Bit1 is set when new rx-data is available.
62 * Until this bit is cleared there will be no other RXs.
63 *
64 * Bit31 is used for testing, it clears after a read.
65 * Writing 1 to this bit will cause an interrupt to be generated.
66 * To clear the test interrupt, write 0 to this register.
67 */
68 u32 interruptControl; /*0x14 */
69#define MIPSNET_INTCTL_TXDONE (1u << 0)
70#define MIPSNET_INTCTL_RXDONE (1u << 1)
71#define MIPSNET_INTCTL_TESTBIT (1u << 31)
72
73 /*
74 * Readonly core-specific interrupt info for the device to signal
75 * the core. The meaning of the contents of this field might change.
76 */
77 /* XXX: the whole memIntf interrupt scheme is messy: the device
78 * should have no control what so ever of what VPE/register set is
79 * being used.
80 * The MemIntf should only expose interrupt lines, and something in
81 * the config should be responsible for the line<->core/vpe bindings.
82 */
83 u32 interruptInfo; /*0x18 */
84
85 /*
86 * This is where the received data is read out.
87 * There is more data to read until rxDataReady is 0.
88 * Only 1 byte at this regs offset is used.
89 */
90 u32 rxDataBuffer; /*0x1c */
91
92 /*
93 * This is where the data to transmit is written.
94 * Data should be written for the amount specified in the
95 * txDataCount register.
96 * Only 1 byte at this regs offset is used.
97 */
98 u32 txDataBuffer; /*0x20 */
99};
100
101#define regaddr(dev, field) \
102 (dev->base_addr + offsetof(struct mipsnet_regs, field))
dcbf8477 103
dcbf8477
RB
104static char mipsnet_string[] = "mipsnet";
105
106/*
107 * Copy data from the MIPSNET rx data port
108 */
109static int ioiocpy_frommipsnet(struct net_device *dev, unsigned char *kdata,
110 int len)
111{
c2af68e5 112 for (; len > 0; len--, kdata++)
c800c5c9 113 *kdata = inb(regaddr(dev, rxDataBuffer));
dcbf8477 114
c800c5c9 115 return inl(regaddr(dev, rxDataCount));
dcbf8477
RB
116}
117
c800c5c9 118static inline void mipsnet_put_todevice(struct net_device *dev,
dcbf8477
RB
119 struct sk_buff *skb)
120{
121 int count_to_go = skb->len;
122 char *buf_ptr = skb->data;
dcbf8477 123
c800c5c9 124 outl(skb->len, regaddr(dev, txDataCount));
dcbf8477 125
c2af68e5 126 for (; count_to_go; buf_ptr++, count_to_go--)
c800c5c9 127 outb(*buf_ptr, regaddr(dev, txDataBuffer));
dcbf8477 128
09f75cd7
JG
129 dev->stats.tx_packets++;
130 dev->stats.tx_bytes += skb->len;
dcbf8477 131
c800c5c9 132 dev_kfree_skb(skb);
dcbf8477
RB
133}
134
135static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev)
136{
f5e42fba
RB
137 /*
138 * Only one packet at a time. Once TXDONE interrupt is serviced, the
dcbf8477
RB
139 * queue will be restarted.
140 */
141 netif_stop_queue(dev);
142 mipsnet_put_todevice(dev, skb);
143
144 return 0;
145}
146
c800c5c9 147static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t len)
dcbf8477
RB
148{
149 struct sk_buff *skb;
dcbf8477 150
c800c5c9
RB
151 if (!len)
152 return len;
153
154 skb = dev_alloc_skb(len + NET_IP_ALIGN);
c2af68e5 155 if (!skb) {
09f75cd7 156 dev->stats.rx_dropped++;
dcbf8477
RB
157 return -ENOMEM;
158 }
159
c800c5c9 160 skb_reserve(skb, NET_IP_ALIGN);
dcbf8477
RB
161 if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len))
162 return -EFAULT;
163
dcbf8477
RB
164 skb->protocol = eth_type_trans(skb, dev);
165 skb->ip_summed = CHECKSUM_UNNECESSARY;
166
dcbf8477
RB
167 netif_rx(skb);
168
09f75cd7
JG
169 dev->stats.rx_packets++;
170 dev->stats.rx_bytes += len;
dcbf8477 171
c800c5c9 172 return len;
dcbf8477
RB
173}
174
7d12e780 175static irqreturn_t mipsnet_interrupt(int irq, void *dev_id)
dcbf8477
RB
176{
177 struct net_device *dev = dev_id;
c800c5c9
RB
178 u32 int_flags;
179 irqreturn_t ret = IRQ_NONE;
180
181 if (irq != dev->irq)
182 goto out_badirq;
183
184 /* TESTBIT is cleared on read. */
185 int_flags = inl(regaddr(dev, interruptControl));
186 if (int_flags & MIPSNET_INTCTL_TESTBIT) {
187 /* TESTBIT takes effect after a write with 0. */
188 outl(0, regaddr(dev, interruptControl));
189 ret = IRQ_HANDLED;
190 } else if (int_flags & MIPSNET_INTCTL_TXDONE) {
191 /* Only one packet at a time, we are done. */
192 dev->stats.tx_packets++;
193 netif_wake_queue(dev);
194 outl(MIPSNET_INTCTL_TXDONE,
195 regaddr(dev, interruptControl));
196 ret = IRQ_HANDLED;
197 } else if (int_flags & MIPSNET_INTCTL_RXDONE) {
198 mipsnet_get_fromdev(dev, inl(regaddr(dev, rxDataCount)));
199 outl(MIPSNET_INTCTL_RXDONE, regaddr(dev, interruptControl));
200 ret = IRQ_HANDLED;
dcbf8477 201 }
c800c5c9
RB
202 return ret;
203
204out_badirq:
205 printk(KERN_INFO "%s: %s(): irq %d for unknown device\n",
b39d66a8 206 dev->name, __func__, irq);
c800c5c9 207 return ret;
c2af68e5 208}
dcbf8477
RB
209
210static int mipsnet_open(struct net_device *dev)
211{
212 int err;
dcbf8477
RB
213
214 err = request_irq(dev->irq, &mipsnet_interrupt,
1fb9df5d 215 IRQF_SHARED, dev->name, (void *) dev);
dcbf8477 216 if (err) {
c800c5c9 217 release_region(dev->base_addr, sizeof(struct mipsnet_regs));
dcbf8477
RB
218 return err;
219 }
220
dcbf8477
RB
221 netif_start_queue(dev);
222
c2af68e5 223 /* test interrupt handler */
c800c5c9 224 outl(MIPSNET_INTCTL_TESTBIT, regaddr(dev, interruptControl));
dcbf8477
RB
225
226 return 0;
227}
228
229static int mipsnet_close(struct net_device *dev)
230{
dcbf8477 231 netif_stop_queue(dev);
c800c5c9 232 free_irq(dev->irq, dev);
dcbf8477
RB
233 return 0;
234}
235
dcbf8477
RB
236static void mipsnet_set_mclist(struct net_device *dev)
237{
dcbf8477
RB
238}
239
7a192ec3 240static int __init mipsnet_probe(struct platform_device *dev)
dcbf8477
RB
241{
242 struct net_device *netdev;
243 int err;
244
09f75cd7 245 netdev = alloc_etherdev(0);
dcbf8477
RB
246 if (!netdev) {
247 err = -ENOMEM;
248 goto out;
249 }
250
7a192ec3 251 platform_set_drvdata(dev, netdev);
dcbf8477
RB
252
253 netdev->open = mipsnet_open;
254 netdev->stop = mipsnet_close;
255 netdev->hard_start_xmit = mipsnet_xmit;
dcbf8477
RB
256 netdev->set_multicast_list = mipsnet_set_mclist;
257
258 /*
259 * TODO: probe for these or load them from PARAM
260 */
261 netdev->base_addr = 0x4200;
3b1d4ed5 262 netdev->irq = MIPS_CPU_IRQ_BASE + MIPSCPU_INT_MB0 +
c800c5c9 263 inl(regaddr(netdev, interruptInfo));
dcbf8477 264
c2af68e5 265 /* Get the io region now, get irq on open() */
c800c5c9
RB
266 if (!request_region(netdev->base_addr, sizeof(struct mipsnet_regs),
267 "mipsnet")) {
dcbf8477
RB
268 err = -EBUSY;
269 goto out_free_netdev;
270 }
271
272 /*
273 * Lacking any better mechanism to allocate a MAC address we use a
274 * random one ...
275 */
276 random_ether_addr(netdev->dev_addr);
277
278 err = register_netdev(netdev);
279 if (err) {
280 printk(KERN_ERR "MIPSNet: failed to register netdev.\n");
281 goto out_free_region;
282 }
283
284 return 0;
285
286out_free_region:
c800c5c9 287 release_region(netdev->base_addr, sizeof(struct mipsnet_regs));
dcbf8477
RB
288
289out_free_netdev:
290 free_netdev(netdev);
291
292out:
293 return err;
294}
295
7a192ec3 296static int __devexit mipsnet_device_remove(struct platform_device *device)
dcbf8477 297{
7a192ec3 298 struct net_device *dev = platform_get_drvdata(device);
dcbf8477
RB
299
300 unregister_netdev(dev);
c800c5c9 301 release_region(dev->base_addr, sizeof(struct mipsnet_regs));
dcbf8477 302 free_netdev(dev);
7a192ec3 303 platform_set_drvdata(device, NULL);
dcbf8477
RB
304
305 return 0;
306}
307
7a192ec3
ML
308static struct platform_driver mipsnet_driver = {
309 .driver = {
310 .name = mipsnet_string,
311 .owner = THIS_MODULE,
312 },
313 .probe = mipsnet_probe,
314 .remove = __devexit_p(mipsnet_device_remove),
dcbf8477
RB
315};
316
dcbf8477
RB
317static int __init mipsnet_init_module(void)
318{
dcbf8477
RB
319 int err;
320
321 printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. "
322 "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION);
323
7a192ec3 324 err = platform_driver_register(&mipsnet_driver);
1e2b980f 325 if (err)
dcbf8477 326 printk(KERN_ERR "Driver registration failed\n");
dcbf8477 327
dcbf8477
RB
328 return err;
329}
330
331static void __exit mipsnet_exit_module(void)
332{
7a192ec3 333 platform_driver_unregister(&mipsnet_driver);
dcbf8477
RB
334}
335
336module_init(mipsnet_init_module);
337module_exit(mipsnet_exit_module);