jazzsonic.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. static struct platform_device *jazz_sonic_device;
  46. #define SONIC_MEM_SIZE 0x100
  47. #include "sonic.h"
  48. /*
  49. * Macros to access SONIC registers
  50. */
  51. #define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
  52. #define SONIC_WRITE(reg,val) \
  53. do { \
  54. *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \
  55. } while (0)
  56. /* use 0 for production, 1 for verification, >1 for debug */
  57. #ifdef SONIC_DEBUG
  58. static unsigned int sonic_debug = SONIC_DEBUG;
  59. #else
  60. static unsigned int sonic_debug = 1;
  61. #endif
  62. /*
  63. * Base address and interrupt of the SONIC controller on JAZZ boards
  64. */
  65. static struct {
  66. unsigned int port;
  67. unsigned int irq;
  68. } sonic_portlist[] = { {JAZZ_ETHERNET_BASE, JAZZ_ETHERNET_IRQ}, {0, 0}};
  69. /*
  70. * We cannot use station (ethernet) address prefixes to detect the
  71. * sonic controller since these are board manufacturer depended.
  72. * So we check for known Silicon Revision IDs instead.
  73. */
  74. static unsigned short known_revisions[] =
  75. {
  76. 0x04, /* Mips Magnum 4000 */
  77. 0xffff /* end of list */
  78. };
  79. static int jazzsonic_open(struct net_device* dev)
  80. {
  81. if (request_irq(dev->irq, &sonic_interrupt, IRQF_DISABLED, "sonic", dev)) {
  82. printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq);
  83. return -EAGAIN;
  84. }
  85. return sonic_open(dev);
  86. }
  87. static int jazzsonic_close(struct net_device* dev)
  88. {
  89. int err;
  90. err = sonic_close(dev);
  91. free_irq(dev->irq, dev);
  92. return err;
  93. }
  94. static int __init sonic_probe1(struct net_device *dev)
  95. {
  96. static unsigned version_printed;
  97. unsigned int silicon_revision;
  98. unsigned int val;
  99. struct sonic_local *lp = netdev_priv(dev);
  100. int err = -ENODEV;
  101. int i;
  102. if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
  103. return -EBUSY;
  104. /*
  105. * get the Silicon Revision ID. If this is one of the known
  106. * one assume that we found a SONIC ethernet controller at
  107. * the expected location.
  108. */
  109. silicon_revision = SONIC_READ(SONIC_SR);
  110. if (sonic_debug > 1)
  111. printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
  112. i = 0;
  113. while (known_revisions[i] != 0xffff
  114. && known_revisions[i] != silicon_revision)
  115. i++;
  116. if (known_revisions[i] == 0xffff) {
  117. printk("SONIC ethernet controller not found (0x%4x)\n",
  118. silicon_revision);
  119. goto out;
  120. }
  121. if (sonic_debug && version_printed++ == 0)
  122. printk(version);
  123. printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ", lp->device->bus_id, dev->base_addr);
  124. /*
  125. * Put the sonic into software reset, then
  126. * retrieve and print the ethernet address.
  127. */
  128. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  129. SONIC_WRITE(SONIC_CEP,0);
  130. for (i=0; i<3; i++) {
  131. val = SONIC_READ(SONIC_CAP0-i);
  132. dev->dev_addr[i*2] = val;
  133. dev->dev_addr[i*2+1] = val >> 8;
  134. }
  135. err = -ENOMEM;
  136. /* Initialize the device structure. */
  137. lp->dma_bitmode = SONIC_BITMODE32;
  138. /* Allocate the entire chunk of memory for the descriptors.
  139. Note that this cannot cross a 64K boundary. */
  140. if ((lp->descriptors = dma_alloc_coherent(lp->device,
  141. SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
  142. &lp->descriptors_laddr, GFP_KERNEL)) == NULL) {
  143. printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n", lp->device->bus_id);
  144. goto out;
  145. }
  146. /* Now set up the pointers to point to the appropriate places */
  147. lp->cda = lp->descriptors;
  148. lp->tda = lp->cda + (SIZEOF_SONIC_CDA
  149. * SONIC_BUS_SCALE(lp->dma_bitmode));
  150. lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  151. * SONIC_BUS_SCALE(lp->dma_bitmode));
  152. lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  153. * SONIC_BUS_SCALE(lp->dma_bitmode));
  154. lp->cda_laddr = lp->descriptors_laddr;
  155. lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
  156. * SONIC_BUS_SCALE(lp->dma_bitmode));
  157. lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  158. * SONIC_BUS_SCALE(lp->dma_bitmode));
  159. lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  160. * SONIC_BUS_SCALE(lp->dma_bitmode));
  161. dev->open = jazzsonic_open;
  162. dev->stop = jazzsonic_close;
  163. dev->hard_start_xmit = sonic_send_packet;
  164. dev->get_stats = sonic_get_stats;
  165. dev->set_multicast_list = &sonic_multicast_list;
  166. dev->tx_timeout = sonic_tx_timeout;
  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. int err = 0;
  188. int i;
  189. /*
  190. * Don't probe if we're not running on a Jazz board.
  191. */
  192. if (mips_machgroup != MACH_GROUP_JAZZ)
  193. return -ENODEV;
  194. dev = alloc_etherdev(sizeof(struct sonic_local));
  195. if (!dev)
  196. return -ENOMEM;
  197. lp = netdev_priv(dev);
  198. lp->device = &pdev->dev;
  199. SET_NETDEV_DEV(dev, &pdev->dev);
  200. SET_MODULE_OWNER(dev);
  201. netdev_boot_setup_check(dev);
  202. if (dev->base_addr >= KSEG0) { /* Check a single specified location. */
  203. err = sonic_probe1(dev);
  204. } else if (dev->base_addr != 0) { /* Don't probe at all. */
  205. err = -ENXIO;
  206. } else {
  207. for (i = 0; sonic_portlist[i].port; i++) {
  208. dev->base_addr = sonic_portlist[i].port;
  209. dev->irq = sonic_portlist[i].irq;
  210. if (sonic_probe1(dev) == 0)
  211. break;
  212. }
  213. if (!sonic_portlist[i].port)
  214. err = -ENODEV;
  215. }
  216. if (err)
  217. goto out;
  218. err = register_netdev(dev);
  219. if (err)
  220. goto out1;
  221. printk("%s: MAC ", dev->name);
  222. for (i = 0; i < 6; i++) {
  223. printk("%2.2x", dev->dev_addr[i]);
  224. if (i < 5)
  225. printk(":");
  226. }
  227. printk(" IRQ %d\n", dev->irq);
  228. return 0;
  229. out1:
  230. release_region(dev->base_addr, SONIC_MEM_SIZE);
  231. out:
  232. free_netdev(dev);
  233. return err;
  234. }
  235. MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
  236. module_param(sonic_debug, int, 0);
  237. MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
  238. #include "sonic.c"
  239. static int __devexit jazz_sonic_device_remove (struct platform_device *pdev)
  240. {
  241. struct net_device *dev = platform_get_drvdata(pdev);
  242. struct sonic_local* lp = netdev_priv(dev);
  243. unregister_netdev(dev);
  244. dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
  245. lp->descriptors, lp->descriptors_laddr);
  246. release_region (dev->base_addr, SONIC_MEM_SIZE);
  247. free_netdev(dev);
  248. return 0;
  249. }
  250. static struct platform_driver jazz_sonic_driver = {
  251. .probe = jazz_sonic_probe,
  252. .remove = __devexit_p(jazz_sonic_device_remove),
  253. .driver = {
  254. .name = jazz_sonic_string,
  255. },
  256. };
  257. static int __init jazz_sonic_init_module(void)
  258. {
  259. int err;
  260. if ((err = platform_driver_register(&jazz_sonic_driver))) {
  261. printk(KERN_ERR "Driver registration failed\n");
  262. return err;
  263. }
  264. jazz_sonic_device = platform_device_alloc(jazz_sonic_string, 0);
  265. if (!jazz_sonic_device)
  266. goto out_unregister;
  267. if (platform_device_add(jazz_sonic_device)) {
  268. platform_device_put(jazz_sonic_device);
  269. jazz_sonic_device = NULL;
  270. }
  271. return 0;
  272. out_unregister:
  273. platform_driver_unregister(&jazz_sonic_driver);
  274. return -ENOMEM;
  275. }
  276. static void __exit jazz_sonic_cleanup_module(void)
  277. {
  278. platform_driver_unregister(&jazz_sonic_driver);
  279. if (jazz_sonic_device) {
  280. platform_device_unregister(jazz_sonic_device);
  281. jazz_sonic_device = NULL;
  282. }
  283. }
  284. module_init(jazz_sonic_init_module);
  285. module_exit(jazz_sonic_cleanup_module);