jazzsonic.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * jazzsonic.c
  3. *
  4. * (C) 2005 Finn Thain
  5. *
  6. * Converted to DMA API, and (from the mac68k project) introduced
  7. * dhd's support for 16-bit cards.
  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 Mips Jazz
  17. * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and
  18. * perhaps others, too)
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/gfp.h>
  25. #include <linux/interrupt.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/bootinfo.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/io.h>
  40. #include <asm/dma.h>
  41. #include <asm/jazz.h>
  42. #include <asm/jazzdma.h>
  43. static char jazz_sonic_string[] = "jazzsonic";
  44. #define SONIC_MEM_SIZE 0x100
  45. #include "sonic.h"
  46. /*
  47. * Macros to access SONIC registers
  48. */
  49. #define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
  50. #define SONIC_WRITE(reg,val) \
  51. do { \
  52. *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \
  53. } while (0)
  54. /* use 0 for production, 1 for verification, >1 for debug */
  55. #ifdef SONIC_DEBUG
  56. static unsigned int sonic_debug = SONIC_DEBUG;
  57. #else
  58. static unsigned int sonic_debug = 1;
  59. #endif
  60. /*
  61. * We cannot use station (ethernet) address prefixes to detect the
  62. * sonic controller since these are board manufacturer depended.
  63. * So we check for known Silicon Revision IDs instead.
  64. */
  65. static unsigned short known_revisions[] =
  66. {
  67. 0x04, /* Mips Magnum 4000 */
  68. 0xffff /* end of list */
  69. };
  70. static int jazzsonic_open(struct net_device* dev)
  71. {
  72. int retval;
  73. retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
  74. if (retval) {
  75. printk(KERN_ERR "%s: unable to get IRQ %d.\n",
  76. dev->name, dev->irq);
  77. return retval;
  78. }
  79. retval = sonic_open(dev);
  80. if (retval)
  81. free_irq(dev->irq, dev);
  82. return retval;
  83. }
  84. static int jazzsonic_close(struct net_device* dev)
  85. {
  86. int err;
  87. err = sonic_close(dev);
  88. free_irq(dev->irq, dev);
  89. return err;
  90. }
  91. static const struct net_device_ops sonic_netdev_ops = {
  92. .ndo_open = jazzsonic_open,
  93. .ndo_stop = jazzsonic_close,
  94. .ndo_start_xmit = sonic_send_packet,
  95. .ndo_get_stats = sonic_get_stats,
  96. .ndo_set_rx_mode = sonic_multicast_list,
  97. .ndo_tx_timeout = sonic_tx_timeout,
  98. .ndo_validate_addr = eth_validate_addr,
  99. .ndo_set_mac_address = eth_mac_addr,
  100. };
  101. static int sonic_probe1(struct net_device *dev)
  102. {
  103. static unsigned version_printed;
  104. unsigned int silicon_revision;
  105. unsigned int val;
  106. struct sonic_local *lp = netdev_priv(dev);
  107. int err = -ENODEV;
  108. int i;
  109. if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
  110. return -EBUSY;
  111. /*
  112. * get the Silicon Revision ID. If this is one of the known
  113. * one assume that we found a SONIC ethernet controller at
  114. * the expected location.
  115. */
  116. silicon_revision = SONIC_READ(SONIC_SR);
  117. if (sonic_debug > 1)
  118. printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
  119. i = 0;
  120. while (known_revisions[i] != 0xffff &&
  121. known_revisions[i] != silicon_revision)
  122. i++;
  123. if (known_revisions[i] == 0xffff) {
  124. printk("SONIC ethernet controller not found (0x%4x)\n",
  125. silicon_revision);
  126. goto out;
  127. }
  128. if (sonic_debug && version_printed++ == 0)
  129. printk(version);
  130. printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ",
  131. dev_name(lp->device), dev->base_addr);
  132. /*
  133. * Put the sonic into software reset, then
  134. * retrieve and print the ethernet address.
  135. */
  136. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  137. SONIC_WRITE(SONIC_CEP,0);
  138. for (i=0; i<3; i++) {
  139. val = SONIC_READ(SONIC_CAP0-i);
  140. dev->dev_addr[i*2] = val;
  141. dev->dev_addr[i*2+1] = val >> 8;
  142. }
  143. err = -ENOMEM;
  144. /* Initialize the device structure. */
  145. lp->dma_bitmode = SONIC_BITMODE32;
  146. /* Allocate the entire chunk of memory for the descriptors.
  147. Note that this cannot cross a 64K boundary. */
  148. lp->descriptors = dma_alloc_coherent(lp->device,
  149. SIZEOF_SONIC_DESC *
  150. SONIC_BUS_SCALE(lp->dma_bitmode),
  151. &lp->descriptors_laddr,
  152. GFP_KERNEL);
  153. if (lp->descriptors == NULL)
  154. goto out;
  155. /* Now set up the pointers to point to the appropriate places */
  156. lp->cda = lp->descriptors;
  157. lp->tda = lp->cda + (SIZEOF_SONIC_CDA
  158. * SONIC_BUS_SCALE(lp->dma_bitmode));
  159. lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  160. * SONIC_BUS_SCALE(lp->dma_bitmode));
  161. lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  162. * SONIC_BUS_SCALE(lp->dma_bitmode));
  163. lp->cda_laddr = lp->descriptors_laddr;
  164. lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
  165. * SONIC_BUS_SCALE(lp->dma_bitmode));
  166. lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  167. * SONIC_BUS_SCALE(lp->dma_bitmode));
  168. lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  169. * SONIC_BUS_SCALE(lp->dma_bitmode));
  170. dev->netdev_ops = &sonic_netdev_ops;
  171. dev->watchdog_timeo = TX_TIMEOUT;
  172. /*
  173. * clear tally counter
  174. */
  175. SONIC_WRITE(SONIC_CRCT,0xffff);
  176. SONIC_WRITE(SONIC_FAET,0xffff);
  177. SONIC_WRITE(SONIC_MPT,0xffff);
  178. return 0;
  179. out:
  180. release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
  181. return err;
  182. }
  183. /*
  184. * Probe for a SONIC ethernet controller on a Mips Jazz board.
  185. * Actually probing is superfluous but we're paranoid.
  186. */
  187. static int jazz_sonic_probe(struct platform_device *pdev)
  188. {
  189. struct net_device *dev;
  190. struct sonic_local *lp;
  191. struct resource *res;
  192. int err = 0;
  193. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  194. if (!res)
  195. return -ENODEV;
  196. dev = alloc_etherdev(sizeof(struct sonic_local));
  197. if (!dev)
  198. return -ENOMEM;
  199. lp = netdev_priv(dev);
  200. lp->device = &pdev->dev;
  201. SET_NETDEV_DEV(dev, &pdev->dev);
  202. platform_set_drvdata(pdev, dev);
  203. netdev_boot_setup_check(dev);
  204. dev->base_addr = res->start;
  205. dev->irq = platform_get_irq(pdev, 0);
  206. err = sonic_probe1(dev);
  207. if (err)
  208. goto out;
  209. err = register_netdev(dev);
  210. if (err)
  211. goto out1;
  212. printk("%s: MAC %pM IRQ %d\n", dev->name, dev->dev_addr, dev->irq);
  213. return 0;
  214. out1:
  215. release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
  216. out:
  217. free_netdev(dev);
  218. return err;
  219. }
  220. MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
  221. module_param(sonic_debug, int, 0);
  222. MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
  223. MODULE_ALIAS("platform:jazzsonic");
  224. #include "sonic.c"
  225. static int jazz_sonic_device_remove(struct platform_device *pdev)
  226. {
  227. struct net_device *dev = platform_get_drvdata(pdev);
  228. struct sonic_local* lp = netdev_priv(dev);
  229. unregister_netdev(dev);
  230. dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
  231. lp->descriptors, lp->descriptors_laddr);
  232. release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
  233. free_netdev(dev);
  234. return 0;
  235. }
  236. static struct platform_driver jazz_sonic_driver = {
  237. .probe = jazz_sonic_probe,
  238. .remove = jazz_sonic_device_remove,
  239. .driver = {
  240. .name = jazz_sonic_string,
  241. },
  242. };
  243. module_platform_driver(jazz_sonic_driver);