jazzsonic.c 7.5 KB

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