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