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> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/etherdevice.h> |
| 15 | #include <linux/netdevice.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/mips-boards/simint.h> |
| 19 | |
| 20 | #include "mipsnet.h" /* actual device IO mapping */ |
| 21 | |
| 22 | #define MIPSNET_VERSION "2005-06-20" |
| 23 | |
| 24 | #define mipsnet_reg_address(dev, field) (dev->base_addr + field_offset(field)) |
| 25 | |
| 26 | struct mipsnet_priv { |
| 27 | struct net_device_stats stats; |
| 28 | }; |
| 29 | |
| 30 | static struct platform_device *mips_plat_dev; |
| 31 | |
| 32 | static char mipsnet_string[] = "mipsnet"; |
| 33 | |
| 34 | /* |
| 35 | * Copy data from the MIPSNET rx data port |
| 36 | */ |
| 37 | static int ioiocpy_frommipsnet(struct net_device *dev, unsigned char *kdata, |
| 38 | int len) |
| 39 | { |
| 40 | uint32_t available_len = inl(mipsnet_reg_address(dev, rxDataCount)); |
| 41 | if (available_len < len) |
| 42 | return -EFAULT; |
| 43 | |
| 44 | for (; len > 0; len--, kdata++) { |
| 45 | *kdata = inb(mipsnet_reg_address(dev, rxDataBuffer)); |
| 46 | } |
| 47 | |
| 48 | return inl(mipsnet_reg_address(dev, rxDataCount)); |
| 49 | } |
| 50 | |
| 51 | static inline ssize_t mipsnet_put_todevice(struct net_device *dev, |
| 52 | struct sk_buff *skb) |
| 53 | { |
| 54 | int count_to_go = skb->len; |
| 55 | char *buf_ptr = skb->data; |
| 56 | struct mipsnet_priv *mp = netdev_priv(dev); |
| 57 | |
| 58 | pr_debug("%s: %s(): telling MIPSNET txDataCount(%d)\n", |
| 59 | dev->name, __FUNCTION__, skb->len); |
| 60 | |
| 61 | outl(skb->len, mipsnet_reg_address(dev, txDataCount)); |
| 62 | |
| 63 | pr_debug("%s: %s(): sending data to MIPSNET txDataBuffer(%d)\n", |
| 64 | dev->name, __FUNCTION__, skb->len); |
| 65 | |
| 66 | for (; count_to_go; buf_ptr++, count_to_go--) { |
| 67 | outb(*buf_ptr, mipsnet_reg_address(dev, txDataBuffer)); |
| 68 | } |
| 69 | |
| 70 | mp->stats.tx_packets++; |
| 71 | mp->stats.tx_bytes += skb->len; |
| 72 | |
| 73 | return skb->len; |
| 74 | } |
| 75 | |
| 76 | static int mipsnet_xmit(struct sk_buff *skb, struct net_device *dev) |
| 77 | { |
| 78 | pr_debug("%s:%s(): transmitting %d bytes\n", |
| 79 | dev->name, __FUNCTION__, skb->len); |
| 80 | |
| 81 | /* Only one packet at a time. Once TXDONE interrupt is serviced, the |
| 82 | * queue will be restarted. |
| 83 | */ |
| 84 | netif_stop_queue(dev); |
| 85 | mipsnet_put_todevice(dev, skb); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static inline ssize_t mipsnet_get_fromdev(struct net_device *dev, size_t count) |
| 91 | { |
| 92 | struct sk_buff *skb; |
| 93 | size_t len = count; |
| 94 | struct mipsnet_priv *mp = netdev_priv(dev); |
| 95 | |
| 96 | if (!(skb = alloc_skb(len + 2, GFP_KERNEL))) { |
| 97 | mp->stats.rx_dropped++; |
| 98 | return -ENOMEM; |
| 99 | } |
| 100 | |
| 101 | skb_reserve(skb, 2); |
| 102 | if (ioiocpy_frommipsnet(dev, skb_put(skb, len), len)) |
| 103 | return -EFAULT; |
| 104 | |
| 105 | skb->dev = dev; |
| 106 | skb->protocol = eth_type_trans(skb, dev); |
| 107 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 108 | |
| 109 | pr_debug("%s:%s(): pushing RXed data to kernel\n", |
| 110 | dev->name, __FUNCTION__); |
| 111 | netif_rx(skb); |
| 112 | |
| 113 | mp->stats.rx_packets++; |
| 114 | mp->stats.rx_bytes += len; |
| 115 | |
| 116 | return count; |
| 117 | } |
| 118 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 119 | static irqreturn_t mipsnet_interrupt(int irq, void *dev_id) |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 120 | { |
| 121 | struct net_device *dev = dev_id; |
| 122 | |
| 123 | irqreturn_t retval = IRQ_NONE; |
| 124 | uint64_t interruptFlags; |
| 125 | |
| 126 | if (irq == dev->irq) { |
| 127 | pr_debug("%s:%s(): irq %d for device\n", |
| 128 | dev->name, __FUNCTION__, irq); |
| 129 | |
| 130 | retval = IRQ_HANDLED; |
| 131 | |
| 132 | interruptFlags = |
| 133 | inl(mipsnet_reg_address(dev, interruptControl)); |
| 134 | pr_debug("%s:%s(): intCtl=0x%016llx\n", dev->name, |
| 135 | __FUNCTION__, interruptFlags); |
| 136 | |
| 137 | if (interruptFlags & MIPSNET_INTCTL_TXDONE) { |
| 138 | pr_debug("%s:%s(): got TXDone\n", |
| 139 | dev->name, __FUNCTION__); |
| 140 | outl(MIPSNET_INTCTL_TXDONE, |
| 141 | mipsnet_reg_address(dev, interruptControl)); |
| 142 | // only one packet at a time, we are done. |
| 143 | netif_wake_queue(dev); |
| 144 | } else if (interruptFlags & MIPSNET_INTCTL_RXDONE) { |
| 145 | pr_debug("%s:%s(): got RX data\n", |
| 146 | dev->name, __FUNCTION__); |
| 147 | mipsnet_get_fromdev(dev, |
| 148 | inl(mipsnet_reg_address(dev, rxDataCount))); |
| 149 | pr_debug("%s:%s(): clearing RX int\n", |
| 150 | dev->name, __FUNCTION__); |
| 151 | outl(MIPSNET_INTCTL_RXDONE, |
| 152 | mipsnet_reg_address(dev, interruptControl)); |
| 153 | |
| 154 | } else if (interruptFlags & MIPSNET_INTCTL_TESTBIT) { |
| 155 | pr_debug("%s:%s(): got test interrupt\n", |
| 156 | dev->name, __FUNCTION__); |
| 157 | // TESTBIT is cleared on read. |
| 158 | // And takes effect after a write with 0 |
| 159 | outl(0, mipsnet_reg_address(dev, interruptControl)); |
| 160 | } else { |
| 161 | pr_debug("%s:%s(): no valid fags 0x%016llx\n", |
| 162 | dev->name, __FUNCTION__, interruptFlags); |
| 163 | // Maybe shared IRQ, just ignore, no clearing. |
| 164 | retval = IRQ_NONE; |
| 165 | } |
| 166 | |
| 167 | } else { |
| 168 | printk(KERN_INFO "%s: %s(): irq %d for unknown device\n", |
| 169 | dev->name, __FUNCTION__, irq); |
| 170 | retval = IRQ_NONE; |
| 171 | } |
| 172 | return retval; |
| 173 | } //mipsnet_interrupt() |
| 174 | |
| 175 | static int mipsnet_open(struct net_device *dev) |
| 176 | { |
| 177 | int err; |
| 178 | pr_debug("%s: mipsnet_open\n", dev->name); |
| 179 | |
| 180 | err = request_irq(dev->irq, &mipsnet_interrupt, |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 181 | IRQF_SHARED, dev->name, (void *) dev); |
Ralf Baechle | dcbf847 | 2005-10-10 14:51:27 +0100 | [diff] [blame] | 182 | |
| 183 | if (err) { |
| 184 | pr_debug("%s: %s(): can't get irq %d\n", |
| 185 | dev->name, __FUNCTION__, dev->irq); |
| 186 | release_region(dev->base_addr, MIPSNET_IO_EXTENT); |
| 187 | return err; |
| 188 | } |
| 189 | |
| 190 | pr_debug("%s: %s(): got IO region at 0x%04lx and irq %d for dev.\n", |
| 191 | dev->name, __FUNCTION__, dev->base_addr, dev->irq); |
| 192 | |
| 193 | |
| 194 | netif_start_queue(dev); |
| 195 | |
| 196 | // test interrupt handler |
| 197 | outl(MIPSNET_INTCTL_TESTBIT, |
| 198 | mipsnet_reg_address(dev, interruptControl)); |
| 199 | |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int mipsnet_close(struct net_device *dev) |
| 205 | { |
| 206 | pr_debug("%s: %s()\n", dev->name, __FUNCTION__); |
| 207 | netif_stop_queue(dev); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static struct net_device_stats *mipsnet_get_stats(struct net_device *dev) |
| 212 | { |
| 213 | struct mipsnet_priv *mp = netdev_priv(dev); |
| 214 | |
| 215 | return &mp->stats; |
| 216 | } |
| 217 | |
| 218 | static void mipsnet_set_mclist(struct net_device *dev) |
| 219 | { |
| 220 | // we don't do anything |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | static int __init mipsnet_probe(struct device *dev) |
| 225 | { |
| 226 | struct net_device *netdev; |
| 227 | int err; |
| 228 | |
| 229 | netdev = alloc_etherdev(sizeof(struct mipsnet_priv)); |
| 230 | if (!netdev) { |
| 231 | err = -ENOMEM; |
| 232 | goto out; |
| 233 | } |
| 234 | |
| 235 | dev_set_drvdata(dev, netdev); |
| 236 | |
| 237 | netdev->open = mipsnet_open; |
| 238 | netdev->stop = mipsnet_close; |
| 239 | netdev->hard_start_xmit = mipsnet_xmit; |
| 240 | netdev->get_stats = mipsnet_get_stats; |
| 241 | netdev->set_multicast_list = mipsnet_set_mclist; |
| 242 | |
| 243 | /* |
| 244 | * TODO: probe for these or load them from PARAM |
| 245 | */ |
| 246 | netdev->base_addr = 0x4200; |
| 247 | netdev->irq = MIPSCPU_INT_BASE + MIPSCPU_INT_MB0 + |
| 248 | inl(mipsnet_reg_address(netdev, interruptInfo)); |
| 249 | |
| 250 | // Get the io region now, get irq on open() |
| 251 | if (!request_region(netdev->base_addr, MIPSNET_IO_EXTENT, "mipsnet")) { |
| 252 | pr_debug("%s: %s(): IO region {start: 0x%04lux, len: %d} " |
| 253 | "for dev is not availble.\n", netdev->name, |
| 254 | __FUNCTION__, netdev->base_addr, MIPSNET_IO_EXTENT); |
| 255 | err = -EBUSY; |
| 256 | goto out_free_netdev; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Lacking any better mechanism to allocate a MAC address we use a |
| 261 | * random one ... |
| 262 | */ |
| 263 | random_ether_addr(netdev->dev_addr); |
| 264 | |
| 265 | err = register_netdev(netdev); |
| 266 | if (err) { |
| 267 | printk(KERN_ERR "MIPSNet: failed to register netdev.\n"); |
| 268 | goto out_free_region; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | |
| 273 | out_free_region: |
| 274 | release_region(netdev->base_addr, MIPSNET_IO_EXTENT); |
| 275 | |
| 276 | out_free_netdev: |
| 277 | free_netdev(netdev); |
| 278 | |
| 279 | out: |
| 280 | return err; |
| 281 | } |
| 282 | |
| 283 | static int __devexit mipsnet_device_remove(struct device *device) |
| 284 | { |
| 285 | struct net_device *dev = dev_get_drvdata(device); |
| 286 | |
| 287 | unregister_netdev(dev); |
| 288 | release_region(dev->base_addr, MIPSNET_IO_EXTENT); |
| 289 | free_netdev(dev); |
| 290 | dev_set_drvdata(device, NULL); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static struct device_driver mipsnet_driver = { |
| 296 | .name = mipsnet_string, |
| 297 | .bus = &platform_bus_type, |
| 298 | .probe = mipsnet_probe, |
| 299 | .remove = __devexit_p(mipsnet_device_remove), |
| 300 | }; |
| 301 | |
| 302 | static void mipsnet_platform_release(struct device *device) |
| 303 | { |
| 304 | struct platform_device *pldev; |
| 305 | |
| 306 | /* free device */ |
| 307 | pldev = to_platform_device(device); |
| 308 | kfree(pldev); |
| 309 | } |
| 310 | |
| 311 | static int __init mipsnet_init_module(void) |
| 312 | { |
| 313 | struct platform_device *pldev; |
| 314 | int err; |
| 315 | |
| 316 | printk(KERN_INFO "MIPSNet Ethernet driver. Version: %s. " |
| 317 | "(c)2005 MIPS Technologies, Inc.\n", MIPSNET_VERSION); |
| 318 | |
| 319 | if (driver_register(&mipsnet_driver)) { |
| 320 | printk(KERN_ERR "Driver registration failed\n"); |
| 321 | err = -ENODEV; |
| 322 | goto out; |
| 323 | } |
| 324 | |
| 325 | if (!(pldev = kmalloc (sizeof (*pldev), GFP_KERNEL))) { |
| 326 | err = -ENOMEM; |
| 327 | goto out_unregister_driver; |
| 328 | } |
| 329 | |
| 330 | memset (pldev, 0, sizeof (*pldev)); |
| 331 | pldev->name = mipsnet_string; |
| 332 | pldev->id = 0; |
| 333 | pldev->dev.release = mipsnet_platform_release; |
| 334 | |
| 335 | if (platform_device_register(pldev)) { |
| 336 | err = -ENODEV; |
| 337 | goto out_free_pldev; |
| 338 | } |
| 339 | |
| 340 | if (!pldev->dev.driver) { |
| 341 | /* |
| 342 | * The driver was not bound to this device, there was |
| 343 | * no hardware at this address. Unregister it, as the |
| 344 | * release fuction will take care of freeing the |
| 345 | * allocated structure |
| 346 | */ |
| 347 | platform_device_unregister (pldev); |
| 348 | } |
| 349 | |
| 350 | mips_plat_dev = pldev; |
| 351 | |
| 352 | return 0; |
| 353 | |
| 354 | out_free_pldev: |
| 355 | kfree(pldev); |
| 356 | |
| 357 | out_unregister_driver: |
| 358 | driver_unregister(&mipsnet_driver); |
| 359 | out: |
| 360 | return err; |
| 361 | } |
| 362 | |
| 363 | static void __exit mipsnet_exit_module(void) |
| 364 | { |
| 365 | pr_debug("MIPSNet Ethernet driver exiting\n"); |
| 366 | |
| 367 | driver_unregister(&mipsnet_driver); |
| 368 | } |
| 369 | |
| 370 | module_init(mipsnet_init_module); |
| 371 | module_exit(mipsnet_exit_module); |