xtsonic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xtsonic.c
  4. *
  5. * (C) 2001 - 2007 Tensilica Inc.
  6. * Kevin Chea <[email protected]>
  7. * Marc Gauthier <[email protected]>
  8. * Chris Zankel <[email protected]>
  9. *
  10. * (C) 1996,1998 by Thomas Bogendoerfer ([email protected])
  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 ([email protected])
  16. *
  17. * A driver for the onboard Sonic ethernet controller on the XT2000.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/gfp.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/ioport.h>
  27. #include <linux/in.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. #include <linux/slab.h>
  37. #include <linux/pgtable.h>
  38. #include <asm/io.h>
  39. #include <asm/dma.h>
  40. static char xtsonic_string[] = "xtsonic";
  41. extern unsigned xtboard_nvram_valid(void);
  42. extern void xtboard_get_ether_addr(unsigned char *buf);
  43. #include "sonic.h"
  44. /*
  45. * According to the documentation for the Sonic ethernet controller,
  46. * EOBC should be 760 words (1520 bytes) for 32-bit applications, and,
  47. * as such, 2 words less than the buffer size. The value for RBSIZE
  48. * defined in sonic.h, however is only 1520.
  49. *
  50. * (Note that in 16-bit configurations, EOBC is 759 words (1518 bytes) and
  51. * RBSIZE 1520 bytes)
  52. */
  53. #undef SONIC_RBSIZE
  54. #define SONIC_RBSIZE 1524
  55. /*
  56. * The chip provides 256 byte register space.
  57. */
  58. #define SONIC_MEM_SIZE 0x100
  59. /*
  60. * Macros to access SONIC registers
  61. */
  62. #define SONIC_READ(reg) \
  63. (0xffff & *((volatile unsigned int *)dev->base_addr+reg))
  64. #define SONIC_WRITE(reg,val) \
  65. *((volatile unsigned int *)dev->base_addr+reg) = val
  66. /*
  67. * We cannot use station (ethernet) address prefixes to detect the
  68. * sonic controller since these are board manufacturer depended.
  69. * So we check for known Silicon Revision IDs instead.
  70. */
  71. static unsigned short known_revisions[] =
  72. {
  73. 0x101, /* SONIC 83934 */
  74. 0xffff /* end of list */
  75. };
  76. static int xtsonic_open(struct net_device *dev)
  77. {
  78. int retval;
  79. retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
  80. if (retval) {
  81. printk(KERN_ERR "%s: unable to get IRQ %d.\n",
  82. dev->name, dev->irq);
  83. return -EAGAIN;
  84. }
  85. retval = sonic_open(dev);
  86. if (retval)
  87. free_irq(dev->irq, dev);
  88. return retval;
  89. }
  90. static int xtsonic_close(struct net_device *dev)
  91. {
  92. int err;
  93. err = sonic_close(dev);
  94. free_irq(dev->irq, dev);
  95. return err;
  96. }
  97. static const struct net_device_ops xtsonic_netdev_ops = {
  98. .ndo_open = xtsonic_open,
  99. .ndo_stop = xtsonic_close,
  100. .ndo_start_xmit = sonic_send_packet,
  101. .ndo_get_stats = sonic_get_stats,
  102. .ndo_set_rx_mode = sonic_multicast_list,
  103. .ndo_tx_timeout = sonic_tx_timeout,
  104. .ndo_validate_addr = eth_validate_addr,
  105. .ndo_set_mac_address = eth_mac_addr,
  106. };
  107. static int sonic_probe1(struct net_device *dev)
  108. {
  109. unsigned int silicon_revision;
  110. struct sonic_local *lp = netdev_priv(dev);
  111. unsigned int base_addr = dev->base_addr;
  112. int i;
  113. int err = 0;
  114. unsigned char addr[ETH_ALEN];
  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. i = 0;
  124. while ((known_revisions[i] != 0xffff) &&
  125. (known_revisions[i] != silicon_revision))
  126. i++;
  127. if (known_revisions[i] == 0xffff) {
  128. pr_info("SONIC ethernet controller not found (0x%4x)\n",
  129. silicon_revision);
  130. return -ENODEV;
  131. }
  132. /*
  133. * Put the sonic into software reset, then retrieve ethernet address.
  134. * Note: we are assuming that the boot-loader has initialized the cam.
  135. */
  136. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  137. SONIC_WRITE(SONIC_DCR,
  138. SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS);
  139. SONIC_WRITE(SONIC_CEP,0);
  140. SONIC_WRITE(SONIC_IMR,0);
  141. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  142. SONIC_WRITE(SONIC_CEP,0);
  143. for (i=0; i<3; i++) {
  144. unsigned int val = SONIC_READ(SONIC_CAP0-i);
  145. addr[i*2] = val;
  146. addr[i*2+1] = val >> 8;
  147. }
  148. eth_hw_addr_set(dev, addr);
  149. lp->dma_bitmode = SONIC_BITMODE32;
  150. err = sonic_alloc_descriptors(dev);
  151. if (err)
  152. goto out;
  153. dev->netdev_ops = &xtsonic_netdev_ops;
  154. dev->watchdog_timeo = TX_TIMEOUT;
  155. /*
  156. * clear tally counter
  157. */
  158. SONIC_WRITE(SONIC_CRCT,0xffff);
  159. SONIC_WRITE(SONIC_FAET,0xffff);
  160. SONIC_WRITE(SONIC_MPT,0xffff);
  161. return 0;
  162. out:
  163. release_region(dev->base_addr, SONIC_MEM_SIZE);
  164. return err;
  165. }
  166. /*
  167. * Probe for a SONIC ethernet controller on an XT2000 board.
  168. * Actually probing is superfluous but we're paranoid.
  169. */
  170. int xtsonic_probe(struct platform_device *pdev)
  171. {
  172. struct net_device *dev;
  173. struct sonic_local *lp;
  174. struct resource *resmem, *resirq;
  175. int err = 0;
  176. if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL)
  177. return -ENODEV;
  178. if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL)
  179. return -ENODEV;
  180. if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL)
  181. return -ENOMEM;
  182. lp = netdev_priv(dev);
  183. lp->device = &pdev->dev;
  184. platform_set_drvdata(pdev, dev);
  185. SET_NETDEV_DEV(dev, &pdev->dev);
  186. dev->base_addr = resmem->start;
  187. dev->irq = resirq->start;
  188. if ((err = sonic_probe1(dev)))
  189. goto out;
  190. pr_info("SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
  191. dev->base_addr, dev->dev_addr, dev->irq);
  192. sonic_msg_init(dev);
  193. if ((err = register_netdev(dev)))
  194. goto undo_probe1;
  195. return 0;
  196. undo_probe1:
  197. dma_free_coherent(lp->device,
  198. SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
  199. lp->descriptors, lp->descriptors_laddr);
  200. release_region(dev->base_addr, SONIC_MEM_SIZE);
  201. out:
  202. free_netdev(dev);
  203. return err;
  204. }
  205. MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
  206. #include "sonic.c"
  207. static int xtsonic_device_remove(struct platform_device *pdev)
  208. {
  209. struct net_device *dev = platform_get_drvdata(pdev);
  210. struct sonic_local *lp = netdev_priv(dev);
  211. unregister_netdev(dev);
  212. dma_free_coherent(lp->device,
  213. SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
  214. lp->descriptors, lp->descriptors_laddr);
  215. release_region (dev->base_addr, SONIC_MEM_SIZE);
  216. free_netdev(dev);
  217. return 0;
  218. }
  219. static struct platform_driver xtsonic_driver = {
  220. .probe = xtsonic_probe,
  221. .remove = xtsonic_device_remove,
  222. .driver = {
  223. .name = xtsonic_string,
  224. },
  225. };
  226. module_platform_driver(xtsonic_driver);