Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 2 | * jazzsonic.c |
| 3 | * |
| 4 | * (C) 2005 Finn Thain |
| 5 | * |
| 6 | * Converted to DMA API, and (from the mac68k project) introduced |
| 7 | * dhd's support for 16-bit cards. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * This driver is based on work from Andreas Busse, but most of |
| 12 | * the code is rewritten. |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de) |
| 15 | * |
| 16 | * A driver for the onboard Sonic ethernet controller on Mips Jazz |
| 17 | * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and |
| 18 | * perhaps others, too) |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/fcntl.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/ioport.h> |
| 28 | #include <linux/in.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/netdevice.h> |
| 34 | #include <linux/etherdevice.h> |
| 35 | #include <linux/skbuff.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 36 | #include <linux/platform_device.h> |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 37 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <asm/bootinfo.h> |
| 40 | #include <asm/system.h> |
| 41 | #include <asm/pgtable.h> |
| 42 | #include <asm/io.h> |
| 43 | #include <asm/dma.h> |
| 44 | #include <asm/jazz.h> |
| 45 | #include <asm/jazzdma.h> |
| 46 | |
| 47 | static char jazz_sonic_string[] = "jazzsonic"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #define SONIC_MEM_SIZE 0x100 |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "sonic.h" |
| 52 | |
| 53 | /* |
| 54 | * Macros to access SONIC registers |
| 55 | */ |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 56 | #define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #define SONIC_WRITE(reg,val) \ |
| 59 | do { \ |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 60 | *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } while (0) |
| 62 | |
| 63 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 64 | /* use 0 for production, 1 for verification, >1 for debug */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #ifdef SONIC_DEBUG |
| 66 | static unsigned int sonic_debug = SONIC_DEBUG; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 67 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static unsigned int sonic_debug = 1; |
| 69 | #endif |
| 70 | |
| 71 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * We cannot use station (ethernet) address prefixes to detect the |
| 73 | * sonic controller since these are board manufacturer depended. |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 74 | * So we check for known Silicon Revision IDs instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | */ |
| 76 | static unsigned short known_revisions[] = |
| 77 | { |
| 78 | 0x04, /* Mips Magnum 4000 */ |
| 79 | 0xffff /* end of list */ |
| 80 | }; |
| 81 | |
Finn Thain | f4d8675 | 2007-05-02 12:55:56 +1000 | [diff] [blame] | 82 | static int jazzsonic_open(struct net_device* dev) |
| 83 | { |
| 84 | if (request_irq(dev->irq, &sonic_interrupt, IRQF_DISABLED, "sonic", dev)) { |
| 85 | printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq); |
| 86 | return -EAGAIN; |
| 87 | } |
| 88 | return sonic_open(dev); |
| 89 | } |
| 90 | |
| 91 | static int jazzsonic_close(struct net_device* dev) |
| 92 | { |
| 93 | int err; |
| 94 | err = sonic_close(dev); |
| 95 | free_irq(dev->irq, dev); |
| 96 | return err; |
| 97 | } |
| 98 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 99 | static int __init sonic_probe1(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | static unsigned version_printed; |
| 102 | unsigned int silicon_revision; |
| 103 | unsigned int val; |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 104 | struct sonic_local *lp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | int err = -ENODEV; |
| 106 | int i; |
| 107 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 108 | if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | return -EBUSY; |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* |
| 112 | * get the Silicon Revision ID. If this is one of the known |
| 113 | * one assume that we found a SONIC ethernet controller at |
| 114 | * the expected location. |
| 115 | */ |
| 116 | silicon_revision = SONIC_READ(SONIC_SR); |
| 117 | if (sonic_debug > 1) |
| 118 | printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision); |
| 119 | |
| 120 | i = 0; |
| 121 | while (known_revisions[i] != 0xffff |
| 122 | && known_revisions[i] != silicon_revision) |
| 123 | i++; |
| 124 | |
| 125 | if (known_revisions[i] == 0xffff) { |
| 126 | printk("SONIC ethernet controller not found (0x%4x)\n", |
| 127 | silicon_revision); |
| 128 | goto out; |
| 129 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (sonic_debug && version_printed++ == 0) |
| 132 | printk(version); |
| 133 | |
Kay Sievers | c231355 | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 134 | printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ", |
| 135 | dev_name(lp->device), dev->base_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * Put the sonic into software reset, then |
| 139 | * retrieve and print the ethernet address. |
| 140 | */ |
| 141 | SONIC_WRITE(SONIC_CMD,SONIC_CR_RST); |
| 142 | SONIC_WRITE(SONIC_CEP,0); |
| 143 | for (i=0; i<3; i++) { |
| 144 | val = SONIC_READ(SONIC_CAP0-i); |
| 145 | dev->dev_addr[i*2] = val; |
| 146 | dev->dev_addr[i*2+1] = val >> 8; |
| 147 | } |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | err = -ENOMEM; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* Initialize the device structure. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 153 | lp->dma_bitmode = SONIC_BITMODE32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 155 | /* Allocate the entire chunk of memory for the descriptors. |
| 156 | Note that this cannot cross a 64K boundary. */ |
| 157 | if ((lp->descriptors = dma_alloc_coherent(lp->device, |
| 158 | SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), |
| 159 | &lp->descriptors_laddr, GFP_KERNEL)) == NULL) { |
Kay Sievers | c231355 | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 160 | printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n", |
| 161 | dev_name(lp->device)); |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 162 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 165 | /* Now set up the pointers to point to the appropriate places */ |
| 166 | lp->cda = lp->descriptors; |
| 167 | lp->tda = lp->cda + (SIZEOF_SONIC_CDA |
| 168 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 169 | lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS |
| 170 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 171 | lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS |
| 172 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 173 | |
| 174 | lp->cda_laddr = lp->descriptors_laddr; |
| 175 | lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA |
| 176 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 177 | lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS |
| 178 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 179 | lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS |
| 180 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 181 | |
Finn Thain | f4d8675 | 2007-05-02 12:55:56 +1000 | [diff] [blame] | 182 | dev->open = jazzsonic_open; |
| 183 | dev->stop = jazzsonic_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | dev->hard_start_xmit = sonic_send_packet; |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 185 | dev->get_stats = sonic_get_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | dev->set_multicast_list = &sonic_multicast_list; |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 187 | dev->tx_timeout = sonic_tx_timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | dev->watchdog_timeo = TX_TIMEOUT; |
| 189 | |
| 190 | /* |
| 191 | * clear tally counter |
| 192 | */ |
| 193 | SONIC_WRITE(SONIC_CRCT,0xffff); |
| 194 | SONIC_WRITE(SONIC_FAET,0xffff); |
| 195 | SONIC_WRITE(SONIC_MPT,0xffff); |
| 196 | |
| 197 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | out: |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 199 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return err; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Probe for a SONIC ethernet controller on a Mips Jazz board. |
| 205 | * Actually probing is superfluous but we're paranoid. |
| 206 | */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 207 | static int __init jazz_sonic_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | struct net_device *dev; |
| 210 | struct sonic_local *lp; |
Thomas Bogendoerfer | ed9f0e0 | 2007-09-08 21:46:49 +0200 | [diff] [blame] | 211 | struct resource *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Thomas Bogendoerfer | ed9f0e0 | 2007-09-08 21:46:49 +0200 | [diff] [blame] | 214 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 215 | if (!res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return -ENODEV; |
| 217 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 218 | dev = alloc_etherdev(sizeof(struct sonic_local)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (!dev) |
| 220 | return -ENOMEM; |
| 221 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 222 | lp = netdev_priv(dev); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 223 | lp->device = &pdev->dev; |
| 224 | SET_NETDEV_DEV(dev, &pdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 226 | netdev_boot_setup_check(dev); |
| 227 | |
Thomas Bogendoerfer | ed9f0e0 | 2007-09-08 21:46:49 +0200 | [diff] [blame] | 228 | dev->base_addr = res->start; |
| 229 | dev->irq = platform_get_irq(pdev, 0); |
| 230 | err = sonic_probe1(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (err) |
| 232 | goto out; |
| 233 | err = register_netdev(dev); |
| 234 | if (err) |
| 235 | goto out1; |
| 236 | |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 237 | printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq); |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return 0; |
| 240 | |
| 241 | out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
| 243 | out: |
| 244 | free_netdev(dev); |
| 245 | |
| 246 | return err; |
| 247 | } |
| 248 | |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 249 | MODULE_DESCRIPTION("Jazz SONIC ethernet driver"); |
| 250 | module_param(sonic_debug, int, 0); |
| 251 | MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)"); |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 252 | MODULE_ALIAS("platform:jazzsonic"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | #include "sonic.c" |
| 255 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 256 | static int __devexit jazz_sonic_device_remove (struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 258 | struct net_device *dev = platform_get_drvdata(pdev); |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 259 | struct sonic_local* lp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Finn Thain | d74472f | 2007-05-01 22:33:02 +0200 | [diff] [blame] | 261 | unregister_netdev(dev); |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 262 | dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), |
| 263 | lp->descriptors, lp->descriptors_laddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | release_region (dev->base_addr, SONIC_MEM_SIZE); |
Finn Thain | d74472f | 2007-05-01 22:33:02 +0200 | [diff] [blame] | 265 | free_netdev(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 270 | static struct platform_driver jazz_sonic_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | .probe = jazz_sonic_probe, |
| 272 | .remove = __devexit_p(jazz_sonic_device_remove), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 273 | .driver = { |
| 274 | .name = jazz_sonic_string, |
Kay Sievers | 72abb46 | 2008-04-18 13:50:44 -0700 | [diff] [blame] | 275 | .owner = THIS_MODULE, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 276 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | }; |
Finn Thain | efcce83 | 2005-08-20 15:53:22 +1000 | [diff] [blame] | 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | static int __init jazz_sonic_init_module(void) |
| 280 | { |
Thomas Bogendoerfer | ed9f0e0 | 2007-09-08 21:46:49 +0200 | [diff] [blame] | 281 | return platform_driver_register(&jazz_sonic_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void __exit jazz_sonic_cleanup_module(void) |
| 285 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 286 | platform_driver_unregister(&jazz_sonic_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | module_init(jazz_sonic_init_module); |
| 290 | module_exit(jazz_sonic_cleanup_module); |