Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xtsonic.c |
| 3 | * |
| 4 | * (C) 2001 - 2007 Tensilica Inc. |
| 5 | * Kevin Chea <kchea@yahoo.com> |
| 6 | * Marc Gauthier <marc@linux-xtensa.org> |
| 7 | * Chris Zankel <chris@zankel.net> |
| 8 | * |
| 9 | * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de) |
| 10 | * |
| 11 | * This driver is based on work from Andreas Busse, but most of |
| 12 | * the code is rewritten. |
| 13 | * |
| 14 | * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de) |
| 15 | * |
| 16 | * A driver for the onboard Sonic ethernet controller on the XT2000. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/fcntl.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/ioport.h> |
| 26 | #include <linux/in.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/netdevice.h> |
| 32 | #include <linux/etherdevice.h> |
| 33 | #include <linux/skbuff.h> |
| 34 | #include <linux/platform_device.h> |
| 35 | #include <linux/dma-mapping.h> |
| 36 | |
| 37 | #include <asm/io.h> |
| 38 | #include <asm/pgtable.h> |
| 39 | #include <asm/dma.h> |
| 40 | |
| 41 | static char xtsonic_string[] = "xtsonic"; |
| 42 | |
| 43 | extern unsigned xtboard_nvram_valid(void); |
| 44 | extern void xtboard_get_ether_addr(unsigned char *buf); |
| 45 | |
| 46 | #include "sonic.h" |
| 47 | |
| 48 | /* |
| 49 | * According to the documentation for the Sonic ethernet controller, |
| 50 | * EOBC should be 760 words (1520 bytes) for 32-bit applications, and, |
| 51 | * as such, 2 words less than the buffer size. The value for RBSIZE |
| 52 | * defined in sonic.h, however is only 1520. |
| 53 | * |
| 54 | * (Note that in 16-bit configurations, EOBC is 759 words (1518 bytes) and |
| 55 | * RBSIZE 1520 bytes) |
| 56 | */ |
| 57 | #undef SONIC_RBSIZE |
| 58 | #define SONIC_RBSIZE 1524 |
| 59 | |
| 60 | /* |
| 61 | * The chip provides 256 byte register space. |
| 62 | */ |
| 63 | #define SONIC_MEM_SIZE 0x100 |
| 64 | |
| 65 | /* |
| 66 | * Macros to access SONIC registers |
| 67 | */ |
| 68 | #define SONIC_READ(reg) \ |
| 69 | (0xffff & *((volatile unsigned int *)dev->base_addr+reg)) |
| 70 | |
| 71 | #define SONIC_WRITE(reg,val) \ |
| 72 | *((volatile unsigned int *)dev->base_addr+reg) = val |
| 73 | |
| 74 | |
| 75 | /* Use 0 for production, 1 for verification, and >2 for debug */ |
| 76 | #ifdef SONIC_DEBUG |
| 77 | static unsigned int sonic_debug = SONIC_DEBUG; |
| 78 | #else |
| 79 | static unsigned int sonic_debug = 1; |
| 80 | #endif |
| 81 | |
| 82 | /* |
| 83 | * We cannot use station (ethernet) address prefixes to detect the |
| 84 | * sonic controller since these are board manufacturer depended. |
| 85 | * So we check for known Silicon Revision IDs instead. |
| 86 | */ |
| 87 | static unsigned short known_revisions[] = |
| 88 | { |
| 89 | 0x101, /* SONIC 83934 */ |
| 90 | 0xffff /* end of list */ |
| 91 | }; |
| 92 | |
| 93 | static int xtsonic_open(struct net_device *dev) |
| 94 | { |
| 95 | if (request_irq(dev->irq,&sonic_interrupt,IRQF_DISABLED,"sonic",dev)) { |
| 96 | printk(KERN_ERR "%s: unable to get IRQ %d.\n", |
| 97 | dev->name, dev->irq); |
| 98 | return -EAGAIN; |
| 99 | } |
| 100 | return sonic_open(dev); |
| 101 | } |
| 102 | |
| 103 | static int xtsonic_close(struct net_device *dev) |
| 104 | { |
| 105 | int err; |
| 106 | err = sonic_close(dev); |
| 107 | free_irq(dev->irq, dev); |
| 108 | return err; |
| 109 | } |
| 110 | |
| 111 | static int __init sonic_probe1(struct net_device *dev) |
| 112 | { |
| 113 | static unsigned version_printed = 0; |
| 114 | unsigned int silicon_revision; |
| 115 | struct sonic_local *lp = netdev_priv(dev); |
| 116 | unsigned int base_addr = dev->base_addr; |
| 117 | int i; |
| 118 | int err = 0; |
| 119 | |
| 120 | if (!request_mem_region(base_addr, 0x100, xtsonic_string)) |
| 121 | return -EBUSY; |
| 122 | |
| 123 | /* |
| 124 | * get the Silicon Revision ID. If this is one of the known |
| 125 | * one assume that we found a SONIC ethernet controller at |
| 126 | * the expected location. |
| 127 | */ |
| 128 | silicon_revision = SONIC_READ(SONIC_SR); |
| 129 | if (sonic_debug > 1) |
| 130 | printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision); |
| 131 | |
| 132 | i = 0; |
| 133 | while ((known_revisions[i] != 0xffff) && |
| 134 | (known_revisions[i] != silicon_revision)) |
| 135 | i++; |
| 136 | |
| 137 | if (known_revisions[i] == 0xffff) { |
| 138 | printk("SONIC ethernet controller not found (0x%4x)\n", |
| 139 | silicon_revision); |
| 140 | return -ENODEV; |
| 141 | } |
| 142 | |
| 143 | if (sonic_debug && version_printed++ == 0) |
| 144 | printk(version); |
| 145 | |
| 146 | /* |
| 147 | * Put the sonic into software reset, then retrieve ethernet address. |
| 148 | * Note: we are assuming that the boot-loader has initialized the cam. |
| 149 | */ |
| 150 | SONIC_WRITE(SONIC_CMD,SONIC_CR_RST); |
| 151 | SONIC_WRITE(SONIC_DCR, |
| 152 | SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS); |
| 153 | SONIC_WRITE(SONIC_CEP,0); |
| 154 | SONIC_WRITE(SONIC_IMR,0); |
| 155 | |
| 156 | SONIC_WRITE(SONIC_CMD,SONIC_CR_RST); |
| 157 | SONIC_WRITE(SONIC_CEP,0); |
| 158 | |
| 159 | for (i=0; i<3; i++) { |
| 160 | unsigned int val = SONIC_READ(SONIC_CAP0-i); |
| 161 | dev->dev_addr[i*2] = val; |
| 162 | dev->dev_addr[i*2+1] = val >> 8; |
| 163 | } |
| 164 | |
| 165 | /* Initialize the device structure. */ |
| 166 | |
| 167 | lp->dma_bitmode = SONIC_BITMODE32; |
| 168 | |
| 169 | /* |
| 170 | * Allocate local private descriptor areas in uncached space. |
| 171 | * The entire structure must be located within the same 64kb segment. |
| 172 | * A simple way to ensure this is to allocate twice the |
| 173 | * size of the structure -- given that the structure is |
| 174 | * much less than 64 kB, at least one of the halves of |
| 175 | * the allocated area will be contained entirely in 64 kB. |
| 176 | * We also allocate extra space for a pointer to allow freeing |
| 177 | * this structure later on (in xtsonic_cleanup_module()). |
| 178 | */ |
| 179 | lp->descriptors = |
| 180 | dma_alloc_coherent(lp->device, |
| 181 | SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), |
| 182 | &lp->descriptors_laddr, GFP_KERNEL); |
| 183 | |
| 184 | if (lp->descriptors == NULL) { |
| 185 | printk(KERN_ERR "%s: couldn't alloc DMA memory for " |
| 186 | " descriptors.\n", lp->device->bus_id); |
| 187 | goto out; |
| 188 | } |
| 189 | |
| 190 | lp->cda = lp->descriptors; |
| 191 | lp->tda = lp->cda + (SIZEOF_SONIC_CDA |
| 192 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 193 | lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS |
| 194 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 195 | lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS |
| 196 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 197 | |
| 198 | /* get the virtual dma address */ |
| 199 | |
| 200 | lp->cda_laddr = lp->descriptors_laddr; |
| 201 | lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA |
| 202 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 203 | lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS |
| 204 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 205 | lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS |
| 206 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 207 | |
| 208 | dev->open = xtsonic_open; |
| 209 | dev->stop = xtsonic_close; |
| 210 | dev->hard_start_xmit = sonic_send_packet; |
| 211 | dev->get_stats = sonic_get_stats; |
| 212 | dev->set_multicast_list = &sonic_multicast_list; |
| 213 | dev->tx_timeout = sonic_tx_timeout; |
| 214 | dev->watchdog_timeo = TX_TIMEOUT; |
| 215 | |
| 216 | /* |
| 217 | * clear tally counter |
| 218 | */ |
| 219 | SONIC_WRITE(SONIC_CRCT,0xffff); |
| 220 | SONIC_WRITE(SONIC_FAET,0xffff); |
| 221 | SONIC_WRITE(SONIC_MPT,0xffff); |
| 222 | |
| 223 | return 0; |
| 224 | out: |
| 225 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /* |
| 231 | * Probe for a SONIC ethernet controller on an XT2000 board. |
| 232 | * Actually probing is superfluous but we're paranoid. |
| 233 | */ |
| 234 | |
| 235 | int __init xtsonic_probe(struct platform_device *pdev) |
| 236 | { |
| 237 | struct net_device *dev; |
| 238 | struct sonic_local *lp; |
| 239 | struct resource *resmem, *resirq; |
| 240 | int err = 0; |
| 241 | |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 242 | if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL) |
| 243 | return -ENODEV; |
| 244 | |
| 245 | if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL) |
| 246 | return -ENODEV; |
| 247 | |
| 248 | if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL) |
| 249 | return -ENOMEM; |
| 250 | |
| 251 | lp = netdev_priv(dev); |
| 252 | lp->device = &pdev->dev; |
| 253 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 254 | netdev_boot_setup_check(dev); |
| 255 | |
| 256 | dev->base_addr = resmem->start; |
| 257 | dev->irq = resirq->start; |
| 258 | |
| 259 | if ((err = sonic_probe1(dev))) |
| 260 | goto out; |
| 261 | if ((err = register_netdev(dev))) |
| 262 | goto out1; |
| 263 | |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 264 | printk("%s: SONIC ethernet @%08lx, MAC %pM, IRQ %d\n", dev->name, |
| 265 | dev->base_addr, dev->dev_addr, dev->irq); |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 266 | |
| 267 | return 0; |
| 268 | |
| 269 | out1: |
| 270 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
| 271 | out: |
| 272 | free_netdev(dev); |
| 273 | |
| 274 | return err; |
| 275 | } |
| 276 | |
| 277 | MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver"); |
| 278 | module_param(sonic_debug, int, 0); |
| 279 | MODULE_PARM_DESC(sonic_debug, "xtsonic debug level (1-4)"); |
| 280 | |
| 281 | #include "sonic.c" |
| 282 | |
| 283 | static int __devexit xtsonic_device_remove (struct platform_device *pdev) |
| 284 | { |
| 285 | struct net_device *dev = platform_get_drvdata(pdev); |
| 286 | struct sonic_local *lp = netdev_priv(dev); |
| 287 | |
| 288 | unregister_netdev(dev); |
| 289 | dma_free_coherent(lp->device, |
| 290 | SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), |
| 291 | lp->descriptors, lp->descriptors_laddr); |
| 292 | release_region (dev->base_addr, SONIC_MEM_SIZE); |
| 293 | free_netdev(dev); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static struct platform_driver xtsonic_driver = { |
| 299 | .probe = xtsonic_probe, |
| 300 | .remove = __devexit_p(xtsonic_device_remove), |
| 301 | .driver = { |
| 302 | .name = xtsonic_string, |
| 303 | }, |
| 304 | }; |
| 305 | |
| 306 | static int __init xtsonic_init(void) |
| 307 | { |
| 308 | return platform_driver_register(&xtsonic_driver); |
| 309 | } |
| 310 | |
| 311 | static void __exit xtsonic_cleanup(void) |
| 312 | { |
| 313 | platform_driver_unregister(&xtsonic_driver); |
| 314 | } |
| 315 | |
| 316 | module_init(xtsonic_init); |
| 317 | module_exit(xtsonic_cleanup); |