Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 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 | |
| 7 | #define DEBUG |
| 8 | |
| 9 | #include <linux/init.h> |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 10 | #include <linux/io.h> |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/netdevice.h> |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 14 | #include <linux/etherdevice.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 16 | #include <asm/mips-boards/simint.h> |
| 17 | |
| 18 | #include "mipsnet.h" /* actual device IO mapping */ |
| 19 | |
| 20 | #define MIPSNET_VERSION "2005-06-20" |
| 21 | |
| 22 | #define mipsnet_reg_address(dev, field) (dev->base_addr + field_offset(field)) |
| 23 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 24 | static char mipsnet_string[] = "mipsnet"; |
| 25 | |
| 26 | /* |
| 27 | * Copy data from the MIPSNET rx data port |
| 28 | */ |
| 29 | static int ioiocpy_frommipsnet(struct net_device *dev, unsigned char *kdata, |
| 30 | int len) |
| 31 | { |
| 32 | uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount)); |
Ralf Baechle | f5e42fb | 2007-10-23 00:35:26 +0100 | [diff] [blame] | 33 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 34 | if (available_len < len) |
| 35 | return -EFAULT; |
| 36 | |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 37 | for (; len > 0; len--, kdata++) |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 38 | *kdata = inb(mipsnet_reg_address(dev, rxDataBuffer)); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 39 | |
| 40 | return inl(mipsnet_reg_address(dev, rxDataCount)); |
| 41 | } |
| 42 | |
| 43 | static inline ssize_t mipsnet_put_todevice(struct net_device *dev, |
| 44 | struct sk_buff *skb) |
| 45 | { |
| 46 | int count_to_go = skb->len; |
| 47 | char *buf_ptr = skb->data; |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 48 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 49 | outl(skb->len, mipsnet_reg_address(dev, txDataCount)); |
| 50 | |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 51 | for (; count_to_go; buf_ptr++, count_to_go--) |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 52 | outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer)); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 53 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 54 | dev->stats.tx_packets++; |
| 55 | dev->stats.tx_bytes += skb->len; |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 56 | |
| 57 | return skb->len; |
| 58 | } |
| 59 | |
| 60 | static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev) |
| 61 | { |
Ralf Baechle | f5e42fb | 2007-10-23 00:35:26 +0100 | [diff] [blame] | 62 | /* |
| 63 | * Only one packet at a time. Once TXDONE interrupt is serviced, the |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 64 | * queue will be restarted. |
| 65 | */ |
| 66 | netif_stop_queue(dev); |
| 67 | mipsnet_put_todevice(dev, skb); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count) |
| 73 | { |
| 74 | struct sk_buff *skb; |
| 75 | size_t len = count; |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 76 | |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 77 | skb = alloc_skb(len + 2, GFP_KERNEL); |
| 78 | if (!skb) { |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 79 | dev->stats.rx_dropped++; |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 80 | return -ENOMEM; |
| 81 | } |
| 82 | |
| 83 | skb_reserve(skb, 2); |
| 84 | if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len)) |
| 85 | return -EFAULT; |
| 86 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 87 | skb->protocol = eth_type_trans(skb, dev); |
| 88 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 89 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 90 | netif_rx(skb); |
| 91 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 92 | dev->stats.rx_packets++; |
| 93 | dev->stats.rx_bytes += len; |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 94 | |
| 95 | return count; |
| 96 | } |
| 97 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 98 | static irqreturn_t mipsnet_interrupt(int irq, void *dev_id) |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 99 | { |
| 100 | struct net_device *dev = dev_id; |
| 101 | |
| 102 | irqreturn_t retval = IRQ_NONE; |
| 103 | uint64_t interruptFlags; |
| 104 | |
| 105 | if (irq == dev->irq) { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 106 | retval = IRQ_HANDLED; |
| 107 | |
| 108 | interruptFlags = |
| 109 | inl(mipsnet_reg_address(dev, interruptControl)); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 110 | |
| 111 | if (interruptFlags & MIPSNET_INTCTL_TXDONE) { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 112 | outl(MIPSNET_INTCTL_TXDONE, |
| 113 | mipsnet_reg_address(dev, interruptControl)); |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 114 | /* only one packet at a time, we are done. */ |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 115 | netif_wake_queue(dev); |
| 116 | } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 117 | mipsnet_get_fromdev(dev, |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 118 | inl(mipsnet_reg_address(dev, rxDataCount))); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 119 | outl(MIPSNET_INTCTL_RXDONE, |
| 120 | mipsnet_reg_address(dev, interruptControl)); |
| 121 | |
| 122 | } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) { |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 123 | /* |
| 124 | * TESTBIT is cleared on read. |
| 125 | * And takes effect after a write with 0 |
| 126 | */ |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 127 | outl(0, mipsnet_reg_address(dev, interruptControl)); |
| 128 | } else { |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 129 | /* Maybe shared IRQ, just ignore, no clearing. */ |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 130 | retval = IRQ_NONE; |
| 131 | } |
| 132 | |
| 133 | } else { |
| 134 | printk(KERN_INFO "%s: %s(): irq %d for unknown device\n", |
| 135 | dev->name, __FUNCTION__, irq); |
| 136 | retval = IRQ_NONE; |
| 137 | } |
| 138 | return retval; |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 139 | } |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 140 | |
| 141 | static int mipsnet_open(struct net_device *dev) |
| 142 | { |
| 143 | int err; |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 144 | |
| 145 | err = request_irq(dev->irq, &mipsnet_interrupt, |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 146 | IRQF_SHARED, dev->name, (void *) dev); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 147 | |
| 148 | if (err) { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 149 | release_region(dev->base_addr, MIPSNET_IO_EXTENT); |
| 150 | return err; |
| 151 | } |
| 152 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 153 | netif_start_queue(dev); |
| 154 | |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 155 | /* test interrupt handler */ |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 156 | outl(MIPSNET_INTCTL_TESTBIT, |
| 157 | mipsnet_reg_address(dev, interruptControl)); |
| 158 | |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static int mipsnet_close(struct net_device *dev) |
| 164 | { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 165 | netif_stop_queue(dev); |
Ralf Baechle | f5e42fb | 2007-10-23 00:35:26 +0100 | [diff] [blame] | 166 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 170 | static void mipsnet_set_mclist(struct net_device *dev) |
| 171 | { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static int __init mipsnet_probe(struct device *dev) |
| 175 | { |
| 176 | struct net_device *netdev; |
| 177 | int err; |
| 178 | |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 179 | netdev = alloc_etherdev(0); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 180 | if (!netdev) { |
| 181 | err = -ENOMEM; |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | dev_set_drvdata(dev, netdev); |
| 186 | |
| 187 | netdev->open = mipsnet_open; |
| 188 | netdev->stop = mipsnet_close; |
| 189 | netdev->hard_start_xmit = mipsnet_xmit; |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 190 | netdev->set_multicast_list = mipsnet_set_mclist; |
| 191 | |
| 192 | /* |
| 193 | * TODO: probe for these or load them from PARAM |
| 194 | */ |
| 195 | netdev->base_addr = 0x4200; |
Ralf Baechle | 3b1d4ed | 2007-06-20 22:27:10 +0100 | [diff] [blame] | 196 | netdev->irq = MIPS_CPU_IRQ_BASE + MIPSCPU_INT_MB0 + |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 197 | inl(mipsnet_reg_address(netdev, interruptInfo)); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 198 | |
Ralf Baechle | c2af68e | 2007-10-12 14:59:56 +0100 | [diff] [blame] | 199 | /* Get the io region now, get irq on open() */ |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 200 | if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 201 | err = -EBUSY; |
| 202 | goto out_free_netdev; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Lacking any better mechanism to allocate a MAC address we use a |
| 207 | * random one ... |
| 208 | */ |
| 209 | random_ether_addr(netdev->dev_addr); |
| 210 | |
| 211 | err = register_netdev(netdev); |
| 212 | if (err) { |
| 213 | printk(KERN_ERR "MIPSNet: failed to register netdev.\n"); |
| 214 | goto out_free_region; |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | |
| 219 | out_free_region: |
| 220 | release_region(netdev->base_addr, MIPSNET_IO_EXTENT); |
| 221 | |
| 222 | out_free_netdev: |
| 223 | free_netdev(netdev); |
| 224 | |
| 225 | out: |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | static int __devexit mipsnet_device_remove(struct device *device) |
| 230 | { |
| 231 | struct net_device *dev = dev_get_drvdata(device); |
| 232 | |
| 233 | unregister_netdev(dev); |
| 234 | release_region(dev->base_addr, MIPSNET_IO_EXTENT); |
| 235 | free_netdev(dev); |
| 236 | dev_set_drvdata(device, NULL); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static struct device_driver mipsnet_driver = { |
| 242 | .name = mipsnet_string, |
| 243 | .bus = &platform_bus_type, |
| 244 | .probe = mipsnet_probe, |
| 245 | .remove = __devexit_p(mipsnet_device_remove), |
| 246 | }; |
| 247 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 248 | static int __init mipsnet_init_module(void) |
| 249 | { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 250 | int err; |
| 251 | |
| 252 | printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. " |
| 253 | "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION); |
| 254 | |
Ralf Baechle | 1e2b980 | 2007-03-18 23:21:22 +0000 | [diff] [blame] | 255 | err = driver_register(&mipsnet_driver); |
| 256 | if (err) |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 257 | printk(KERN_ERR "Driver registration failed\n"); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 258 | |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 259 | return err; |
| 260 | } |
| 261 | |
| 262 | static void __exit mipsnet_exit_module(void) |
| 263 | { |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 264 | driver_unregister(&mipsnet_driver); |
| 265 | } |
| 266 | |
| 267 | module_init(mipsnet_init_module); |
| 268 | module_exit(mipsnet_exit_module); |