xtsonic.c 8.2 KB

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