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 | { |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 95 | if (request_irq(dev->irq,sonic_interrupt,IRQF_DISABLED,"sonic",dev)) { |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 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 | |
Alexander Beregalov | ccd97bb | 2009-04-11 07:30:19 +0000 | [diff] [blame] | 111 | static const struct net_device_ops xtsonic_netdev_ops = { |
| 112 | .ndo_open = xtsonic_open, |
| 113 | .ndo_stop = xtsonic_close, |
| 114 | .ndo_start_xmit = sonic_send_packet, |
| 115 | .ndo_get_stats = sonic_get_stats, |
| 116 | .ndo_set_multicast_list = sonic_multicast_list, |
| 117 | .ndo_tx_timeout = sonic_tx_timeout, |
| 118 | .ndo_validate_addr = eth_validate_addr, |
| 119 | .ndo_change_mtu = eth_change_mtu, |
| 120 | .ndo_set_mac_address = eth_mac_addr, |
| 121 | }; |
| 122 | |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 123 | static int __init sonic_probe1(struct net_device *dev) |
| 124 | { |
| 125 | static unsigned version_printed = 0; |
| 126 | unsigned int silicon_revision; |
| 127 | struct sonic_local *lp = netdev_priv(dev); |
| 128 | unsigned int base_addr = dev->base_addr; |
| 129 | int i; |
| 130 | int err = 0; |
| 131 | |
| 132 | if (!request_mem_region(base_addr, 0x100, xtsonic_string)) |
| 133 | return -EBUSY; |
| 134 | |
| 135 | /* |
| 136 | * get the Silicon Revision ID. If this is one of the known |
| 137 | * one assume that we found a SONIC ethernet controller at |
| 138 | * the expected location. |
| 139 | */ |
| 140 | silicon_revision = SONIC_READ(SONIC_SR); |
| 141 | if (sonic_debug > 1) |
| 142 | printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision); |
| 143 | |
| 144 | i = 0; |
| 145 | while ((known_revisions[i] != 0xffff) && |
| 146 | (known_revisions[i] != silicon_revision)) |
| 147 | i++; |
| 148 | |
| 149 | if (known_revisions[i] == 0xffff) { |
| 150 | printk("SONIC ethernet controller not found (0x%4x)\n", |
| 151 | silicon_revision); |
| 152 | return -ENODEV; |
| 153 | } |
| 154 | |
| 155 | if (sonic_debug && version_printed++ == 0) |
| 156 | printk(version); |
| 157 | |
| 158 | /* |
| 159 | * Put the sonic into software reset, then retrieve ethernet address. |
| 160 | * Note: we are assuming that the boot-loader has initialized the cam. |
| 161 | */ |
| 162 | SONIC_WRITE(SONIC_CMD,SONIC_CR_RST); |
| 163 | SONIC_WRITE(SONIC_DCR, |
| 164 | SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS); |
| 165 | SONIC_WRITE(SONIC_CEP,0); |
| 166 | SONIC_WRITE(SONIC_IMR,0); |
| 167 | |
| 168 | SONIC_WRITE(SONIC_CMD,SONIC_CR_RST); |
| 169 | SONIC_WRITE(SONIC_CEP,0); |
| 170 | |
| 171 | for (i=0; i<3; i++) { |
| 172 | unsigned int val = SONIC_READ(SONIC_CAP0-i); |
| 173 | dev->dev_addr[i*2] = val; |
| 174 | dev->dev_addr[i*2+1] = val >> 8; |
| 175 | } |
| 176 | |
| 177 | /* Initialize the device structure. */ |
| 178 | |
| 179 | lp->dma_bitmode = SONIC_BITMODE32; |
| 180 | |
| 181 | /* |
| 182 | * Allocate local private descriptor areas in uncached space. |
| 183 | * The entire structure must be located within the same 64kb segment. |
| 184 | * A simple way to ensure this is to allocate twice the |
| 185 | * size of the structure -- given that the structure is |
| 186 | * much less than 64 kB, at least one of the halves of |
| 187 | * the allocated area will be contained entirely in 64 kB. |
| 188 | * We also allocate extra space for a pointer to allow freeing |
| 189 | * this structure later on (in xtsonic_cleanup_module()). |
| 190 | */ |
| 191 | lp->descriptors = |
| 192 | dma_alloc_coherent(lp->device, |
| 193 | SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), |
| 194 | &lp->descriptors_laddr, GFP_KERNEL); |
| 195 | |
| 196 | if (lp->descriptors == NULL) { |
| 197 | printk(KERN_ERR "%s: couldn't alloc DMA memory for " |
Kay Sievers | c231355 | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 198 | " descriptors.\n", dev_name(lp->device)); |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 199 | goto out; |
| 200 | } |
| 201 | |
| 202 | lp->cda = lp->descriptors; |
| 203 | lp->tda = lp->cda + (SIZEOF_SONIC_CDA |
| 204 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 205 | lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS |
| 206 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 207 | lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS |
| 208 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 209 | |
| 210 | /* get the virtual dma address */ |
| 211 | |
| 212 | lp->cda_laddr = lp->descriptors_laddr; |
| 213 | lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA |
| 214 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 215 | lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS |
| 216 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 217 | lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS |
| 218 | * SONIC_BUS_SCALE(lp->dma_bitmode)); |
| 219 | |
Alexander Beregalov | ccd97bb | 2009-04-11 07:30:19 +0000 | [diff] [blame] | 220 | dev->netdev_ops = &xtsonic_netdev_ops; |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 221 | dev->watchdog_timeo = TX_TIMEOUT; |
| 222 | |
| 223 | /* |
| 224 | * clear tally counter |
| 225 | */ |
| 226 | SONIC_WRITE(SONIC_CRCT,0xffff); |
| 227 | SONIC_WRITE(SONIC_FAET,0xffff); |
| 228 | SONIC_WRITE(SONIC_MPT,0xffff); |
| 229 | |
| 230 | return 0; |
| 231 | out: |
| 232 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
| 233 | return err; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /* |
| 238 | * Probe for a SONIC ethernet controller on an XT2000 board. |
| 239 | * Actually probing is superfluous but we're paranoid. |
| 240 | */ |
| 241 | |
Uwe Kleine-König | 0c9cd39 | 2009-07-21 11:25:27 +0000 | [diff] [blame] | 242 | int __devinit xtsonic_probe(struct platform_device *pdev) |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 243 | { |
| 244 | struct net_device *dev; |
| 245 | struct sonic_local *lp; |
| 246 | struct resource *resmem, *resirq; |
| 247 | int err = 0; |
| 248 | |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 249 | if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL) |
| 250 | return -ENODEV; |
| 251 | |
| 252 | if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL) |
| 253 | return -ENODEV; |
| 254 | |
| 255 | if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | lp = netdev_priv(dev); |
| 259 | lp->device = &pdev->dev; |
| 260 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 261 | netdev_boot_setup_check(dev); |
| 262 | |
| 263 | dev->base_addr = resmem->start; |
| 264 | dev->irq = resirq->start; |
| 265 | |
| 266 | if ((err = sonic_probe1(dev))) |
| 267 | goto out; |
| 268 | if ((err = register_netdev(dev))) |
| 269 | goto out1; |
| 270 | |
Johannes Berg | e174961 | 2008-10-27 15:59:26 -0700 | [diff] [blame] | 271 | printk("%s: SONIC ethernet @%08lx, MAC %pM, IRQ %d\n", dev->name, |
| 272 | dev->base_addr, dev->dev_addr, dev->irq); |
Chris Zankel | 74f2a5f0 | 2008-05-05 23:36:35 -0700 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | |
| 276 | out1: |
| 277 | release_region(dev->base_addr, SONIC_MEM_SIZE); |
| 278 | out: |
| 279 | free_netdev(dev); |
| 280 | |
| 281 | return err; |
| 282 | } |
| 283 | |
| 284 | MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver"); |
| 285 | module_param(sonic_debug, int, 0); |
| 286 | MODULE_PARM_DESC(sonic_debug, "xtsonic debug level (1-4)"); |
| 287 | |
| 288 | #include "sonic.c" |
| 289 | |
| 290 | static int __devexit xtsonic_device_remove (struct platform_device *pdev) |
| 291 | { |
| 292 | struct net_device *dev = platform_get_drvdata(pdev); |
| 293 | struct sonic_local *lp = netdev_priv(dev); |
| 294 | |
| 295 | unregister_netdev(dev); |
| 296 | dma_free_coherent(lp->device, |
| 297 | SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode), |
| 298 | lp->descriptors, lp->descriptors_laddr); |
| 299 | release_region (dev->base_addr, SONIC_MEM_SIZE); |
| 300 | free_netdev(dev); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static struct platform_driver xtsonic_driver = { |
| 306 | .probe = xtsonic_probe, |
| 307 | .remove = __devexit_p(xtsonic_device_remove), |
| 308 | .driver = { |
| 309 | .name = xtsonic_string, |
| 310 | }, |
| 311 | }; |
| 312 | |
| 313 | static int __init xtsonic_init(void) |
| 314 | { |
| 315 | return platform_driver_register(&xtsonic_driver); |
| 316 | } |
| 317 | |
| 318 | static void __exit xtsonic_cleanup(void) |
| 319 | { |
| 320 | platform_driver_unregister(&xtsonic_driver); |
| 321 | } |
| 322 | |
| 323 | module_init(xtsonic_init); |
| 324 | module_exit(xtsonic_cleanup); |