xtsonic.c 8.3 KB

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