xtsonic.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #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 <asm/io.h>
  38. #include <asm/pgtable.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 __init 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. if (!request_mem_region(base_addr, 0x100, xtsonic_string))
  115. return -EBUSY;
  116. /*
  117. * get the Silicon Revision ID. If this is one of the known
  118. * one assume that we found a SONIC ethernet controller at
  119. * the expected location.
  120. */
  121. silicon_revision = SONIC_READ(SONIC_SR);
  122. i = 0;
  123. while ((known_revisions[i] != 0xffff) &&
  124. (known_revisions[i] != silicon_revision))
  125. i++;
  126. if (known_revisions[i] == 0xffff) {
  127. pr_info("SONIC ethernet controller not found (0x%4x)\n",
  128. silicon_revision);
  129. return -ENODEV;
  130. }
  131. /*
  132. * Put the sonic into software reset, then retrieve ethernet address.
  133. * Note: we are assuming that the boot-loader has initialized the cam.
  134. */
  135. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  136. SONIC_WRITE(SONIC_DCR,
  137. SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS);
  138. SONIC_WRITE(SONIC_CEP,0);
  139. SONIC_WRITE(SONIC_IMR,0);
  140. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  141. SONIC_WRITE(SONIC_CEP,0);
  142. for (i=0; i<3; i++) {
  143. unsigned int val = SONIC_READ(SONIC_CAP0-i);
  144. dev->dev_addr[i*2] = val;
  145. dev->dev_addr[i*2+1] = val >> 8;
  146. }
  147. /* Initialize the device structure. */
  148. lp->dma_bitmode = SONIC_BITMODE32;
  149. /*
  150. * Allocate local private descriptor areas in uncached space.
  151. * The entire structure must be located within the same 64kb segment.
  152. * A simple way to ensure this is to allocate twice the
  153. * size of the structure -- given that the structure is
  154. * much less than 64 kB, at least one of the halves of
  155. * the allocated area will be contained entirely in 64 kB.
  156. * We also allocate extra space for a pointer to allow freeing
  157. * this structure later on (in xtsonic_cleanup_module()).
  158. */
  159. lp->descriptors = dma_alloc_coherent(lp->device,
  160. SIZEOF_SONIC_DESC *
  161. SONIC_BUS_SCALE(lp->dma_bitmode),
  162. &lp->descriptors_laddr,
  163. GFP_KERNEL);
  164. if (lp->descriptors == NULL) {
  165. err = -ENOMEM;
  166. goto out;
  167. }
  168. lp->cda = lp->descriptors;
  169. lp->tda = lp->cda + (SIZEOF_SONIC_CDA
  170. * SONIC_BUS_SCALE(lp->dma_bitmode));
  171. lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  172. * SONIC_BUS_SCALE(lp->dma_bitmode));
  173. lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  174. * SONIC_BUS_SCALE(lp->dma_bitmode));
  175. /* get the virtual dma address */
  176. lp->cda_laddr = lp->descriptors_laddr;
  177. lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
  178. * SONIC_BUS_SCALE(lp->dma_bitmode));
  179. lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  180. * SONIC_BUS_SCALE(lp->dma_bitmode));
  181. lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  182. * SONIC_BUS_SCALE(lp->dma_bitmode));
  183. dev->netdev_ops = &xtsonic_netdev_ops;
  184. dev->watchdog_timeo = TX_TIMEOUT;
  185. /*
  186. * clear tally counter
  187. */
  188. SONIC_WRITE(SONIC_CRCT,0xffff);
  189. SONIC_WRITE(SONIC_FAET,0xffff);
  190. SONIC_WRITE(SONIC_MPT,0xffff);
  191. return 0;
  192. out:
  193. release_region(dev->base_addr, SONIC_MEM_SIZE);
  194. return err;
  195. }
  196. /*
  197. * Probe for a SONIC ethernet controller on an XT2000 board.
  198. * Actually probing is superfluous but we're paranoid.
  199. */
  200. int xtsonic_probe(struct platform_device *pdev)
  201. {
  202. struct net_device *dev;
  203. struct sonic_local *lp;
  204. struct resource *resmem, *resirq;
  205. int err = 0;
  206. if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL)
  207. return -ENODEV;
  208. if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL)
  209. return -ENODEV;
  210. if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL)
  211. return -ENOMEM;
  212. lp = netdev_priv(dev);
  213. lp->device = &pdev->dev;
  214. platform_set_drvdata(pdev, dev);
  215. SET_NETDEV_DEV(dev, &pdev->dev);
  216. netdev_boot_setup_check(dev);
  217. dev->base_addr = resmem->start;
  218. dev->irq = resirq->start;
  219. if ((err = sonic_probe1(dev)))
  220. goto out;
  221. pr_info("SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
  222. dev->base_addr, dev->dev_addr, dev->irq);
  223. sonic_msg_init(dev);
  224. if ((err = register_netdev(dev)))
  225. goto out1;
  226. return 0;
  227. out1:
  228. release_region(dev->base_addr, SONIC_MEM_SIZE);
  229. out:
  230. free_netdev(dev);
  231. return err;
  232. }
  233. MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
  234. #include "sonic.c"
  235. static int xtsonic_device_remove(struct platform_device *pdev)
  236. {
  237. struct net_device *dev = platform_get_drvdata(pdev);
  238. struct sonic_local *lp = netdev_priv(dev);
  239. unregister_netdev(dev);
  240. dma_free_coherent(lp->device,
  241. SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
  242. lp->descriptors, lp->descriptors_laddr);
  243. release_region (dev->base_addr, SONIC_MEM_SIZE);
  244. free_netdev(dev);
  245. return 0;
  246. }
  247. static struct platform_driver xtsonic_driver = {
  248. .probe = xtsonic_probe,
  249. .remove = xtsonic_device_remove,
  250. .driver = {
  251. .name = xtsonic_string,
  252. },
  253. };
  254. module_platform_driver(xtsonic_driver);