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