xtsonic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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, 0, "sonic", dev);
  85. if (retval) {
  86. printk(KERN_ERR "%s: unable to get IRQ %d.\n",
  87. dev->name, dev->irq);
  88. return -EAGAIN;
  89. }
  90. retval = sonic_open(dev);
  91. if (retval)
  92. free_irq(dev->irq, dev);
  93. return retval;
  94. }
  95. static int xtsonic_close(struct net_device *dev)
  96. {
  97. int err;
  98. err = sonic_close(dev);
  99. free_irq(dev->irq, dev);
  100. return err;
  101. }
  102. static const struct net_device_ops xtsonic_netdev_ops = {
  103. .ndo_open = xtsonic_open,
  104. .ndo_stop = xtsonic_close,
  105. .ndo_start_xmit = sonic_send_packet,
  106. .ndo_get_stats = sonic_get_stats,
  107. .ndo_set_rx_mode = sonic_multicast_list,
  108. .ndo_tx_timeout = sonic_tx_timeout,
  109. .ndo_validate_addr = eth_validate_addr,
  110. .ndo_change_mtu = eth_change_mtu,
  111. .ndo_set_mac_address = eth_mac_addr,
  112. };
  113. static int __init sonic_probe1(struct net_device *dev)
  114. {
  115. static unsigned version_printed = 0;
  116. unsigned int silicon_revision;
  117. struct sonic_local *lp = netdev_priv(dev);
  118. unsigned int base_addr = dev->base_addr;
  119. int i;
  120. int err = 0;
  121. if (!request_mem_region(base_addr, 0x100, xtsonic_string))
  122. return -EBUSY;
  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. i = 0;
  132. while ((known_revisions[i] != 0xffff) &&
  133. (known_revisions[i] != silicon_revision))
  134. i++;
  135. if (known_revisions[i] == 0xffff) {
  136. printk("SONIC ethernet controller not found (0x%4x)\n",
  137. silicon_revision);
  138. return -ENODEV;
  139. }
  140. if (sonic_debug && version_printed++ == 0)
  141. printk(version);
  142. /*
  143. * Put the sonic into software reset, then retrieve ethernet address.
  144. * Note: we are assuming that the boot-loader has initialized the cam.
  145. */
  146. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  147. SONIC_WRITE(SONIC_DCR,
  148. SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS);
  149. SONIC_WRITE(SONIC_CEP,0);
  150. SONIC_WRITE(SONIC_IMR,0);
  151. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  152. SONIC_WRITE(SONIC_CEP,0);
  153. for (i=0; i<3; i++) {
  154. unsigned int val = SONIC_READ(SONIC_CAP0-i);
  155. dev->dev_addr[i*2] = val;
  156. dev->dev_addr[i*2+1] = val >> 8;
  157. }
  158. /* Initialize the device structure. */
  159. lp->dma_bitmode = SONIC_BITMODE32;
  160. /*
  161. * Allocate local private descriptor areas in uncached space.
  162. * The entire structure must be located within the same 64kb segment.
  163. * A simple way to ensure this is to allocate twice the
  164. * size of the structure -- given that the structure is
  165. * much less than 64 kB, at least one of the halves of
  166. * the allocated area will be contained entirely in 64 kB.
  167. * We also allocate extra space for a pointer to allow freeing
  168. * this structure later on (in xtsonic_cleanup_module()).
  169. */
  170. lp->descriptors = dma_alloc_coherent(lp->device,
  171. SIZEOF_SONIC_DESC *
  172. SONIC_BUS_SCALE(lp->dma_bitmode),
  173. &lp->descriptors_laddr,
  174. GFP_KERNEL);
  175. if (lp->descriptors == NULL) {
  176. err = -ENOMEM;
  177. goto out;
  178. }
  179. lp->cda = lp->descriptors;
  180. lp->tda = lp->cda + (SIZEOF_SONIC_CDA
  181. * SONIC_BUS_SCALE(lp->dma_bitmode));
  182. lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  183. * SONIC_BUS_SCALE(lp->dma_bitmode));
  184. lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  185. * SONIC_BUS_SCALE(lp->dma_bitmode));
  186. /* get the virtual dma address */
  187. lp->cda_laddr = lp->descriptors_laddr;
  188. lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
  189. * SONIC_BUS_SCALE(lp->dma_bitmode));
  190. lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  191. * SONIC_BUS_SCALE(lp->dma_bitmode));
  192. lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  193. * SONIC_BUS_SCALE(lp->dma_bitmode));
  194. dev->netdev_ops = &xtsonic_netdev_ops;
  195. dev->watchdog_timeo = TX_TIMEOUT;
  196. /*
  197. * clear tally counter
  198. */
  199. SONIC_WRITE(SONIC_CRCT,0xffff);
  200. SONIC_WRITE(SONIC_FAET,0xffff);
  201. SONIC_WRITE(SONIC_MPT,0xffff);
  202. return 0;
  203. out:
  204. release_region(dev->base_addr, SONIC_MEM_SIZE);
  205. return err;
  206. }
  207. /*
  208. * Probe for a SONIC ethernet controller on an XT2000 board.
  209. * Actually probing is superfluous but we're paranoid.
  210. */
  211. int xtsonic_probe(struct platform_device *pdev)
  212. {
  213. struct net_device *dev;
  214. struct sonic_local *lp;
  215. struct resource *resmem, *resirq;
  216. int err = 0;
  217. if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL)
  218. return -ENODEV;
  219. if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL)
  220. return -ENODEV;
  221. if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL)
  222. return -ENOMEM;
  223. lp = netdev_priv(dev);
  224. lp->device = &pdev->dev;
  225. platform_set_drvdata(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 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 = xtsonic_device_remove,
  262. .driver = {
  263. .name = xtsonic_string,
  264. },
  265. };
  266. module_platform_driver(xtsonic_driver);