jazzsonic.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 __init sonic_probe1(struct net_device *dev)
  80. {
  81. static unsigned version_printed;
  82. unsigned int silicon_revision;
  83. unsigned int val;
  84. struct sonic_local *lp = netdev_priv(dev);
  85. int err = -ENODEV;
  86. int i;
  87. if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
  88. return -EBUSY;
  89. /*
  90. * get the Silicon Revision ID. If this is one of the known
  91. * one assume that we found a SONIC ethernet controller at
  92. * the expected location.
  93. */
  94. silicon_revision = SONIC_READ(SONIC_SR);
  95. if (sonic_debug > 1)
  96. printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
  97. i = 0;
  98. while (known_revisions[i] != 0xffff
  99. && known_revisions[i] != silicon_revision)
  100. i++;
  101. if (known_revisions[i] == 0xffff) {
  102. printk("SONIC ethernet controller not found (0x%4x)\n",
  103. silicon_revision);
  104. goto out;
  105. }
  106. if (sonic_debug && version_printed++ == 0)
  107. printk(version);
  108. printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ", lp->device->bus_id, dev->base_addr);
  109. /*
  110. * Put the sonic into software reset, then
  111. * retrieve and print the ethernet address.
  112. */
  113. SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
  114. SONIC_WRITE(SONIC_CEP,0);
  115. for (i=0; i<3; i++) {
  116. val = SONIC_READ(SONIC_CAP0-i);
  117. dev->dev_addr[i*2] = val;
  118. dev->dev_addr[i*2+1] = val >> 8;
  119. }
  120. err = -ENOMEM;
  121. /* Initialize the device structure. */
  122. lp->dma_bitmode = SONIC_BITMODE32;
  123. /* Allocate the entire chunk of memory for the descriptors.
  124. Note that this cannot cross a 64K boundary. */
  125. if ((lp->descriptors = dma_alloc_coherent(lp->device,
  126. SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
  127. &lp->descriptors_laddr, GFP_KERNEL)) == NULL) {
  128. printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n", lp->device->bus_id);
  129. goto out;
  130. }
  131. /* Now set up the pointers to point to the appropriate places */
  132. lp->cda = lp->descriptors;
  133. lp->tda = lp->cda + (SIZEOF_SONIC_CDA
  134. * SONIC_BUS_SCALE(lp->dma_bitmode));
  135. lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  136. * SONIC_BUS_SCALE(lp->dma_bitmode));
  137. lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  138. * SONIC_BUS_SCALE(lp->dma_bitmode));
  139. lp->cda_laddr = lp->descriptors_laddr;
  140. lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
  141. * SONIC_BUS_SCALE(lp->dma_bitmode));
  142. lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
  143. * SONIC_BUS_SCALE(lp->dma_bitmode));
  144. lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
  145. * SONIC_BUS_SCALE(lp->dma_bitmode));
  146. dev->open = sonic_open;
  147. dev->stop = sonic_close;
  148. dev->hard_start_xmit = sonic_send_packet;
  149. dev->get_stats = sonic_get_stats;
  150. dev->set_multicast_list = &sonic_multicast_list;
  151. dev->tx_timeout = sonic_tx_timeout;
  152. dev->watchdog_timeo = TX_TIMEOUT;
  153. /*
  154. * clear tally counter
  155. */
  156. SONIC_WRITE(SONIC_CRCT,0xffff);
  157. SONIC_WRITE(SONIC_FAET,0xffff);
  158. SONIC_WRITE(SONIC_MPT,0xffff);
  159. return 0;
  160. out:
  161. release_region(dev->base_addr, SONIC_MEM_SIZE);
  162. return err;
  163. }
  164. /*
  165. * Probe for a SONIC ethernet controller on a Mips Jazz board.
  166. * Actually probing is superfluous but we're paranoid.
  167. */
  168. static int __init jazz_sonic_probe(struct platform_device *pdev)
  169. {
  170. struct net_device *dev;
  171. struct sonic_local *lp;
  172. int err = 0;
  173. int i;
  174. /*
  175. * Don't probe if we're not running on a Jazz board.
  176. */
  177. if (mips_machgroup != MACH_GROUP_JAZZ)
  178. return -ENODEV;
  179. dev = alloc_etherdev(sizeof(struct sonic_local));
  180. if (!dev)
  181. return -ENOMEM;
  182. lp = netdev_priv(dev);
  183. lp->device = &pdev->dev;
  184. SET_NETDEV_DEV(dev, &pdev->dev);
  185. SET_MODULE_OWNER(dev);
  186. netdev_boot_setup_check(dev);
  187. if (dev->base_addr >= KSEG0) { /* Check a single specified location. */
  188. err = sonic_probe1(dev);
  189. } else if (dev->base_addr != 0) { /* Don't probe at all. */
  190. err = -ENXIO;
  191. } else {
  192. for (i = 0; sonic_portlist[i].port; i++) {
  193. dev->base_addr = sonic_portlist[i].port;
  194. dev->irq = sonic_portlist[i].irq;
  195. if (sonic_probe1(dev) == 0)
  196. break;
  197. }
  198. if (!sonic_portlist[i].port)
  199. err = -ENODEV;
  200. }
  201. if (err)
  202. goto out;
  203. err = register_netdev(dev);
  204. if (err)
  205. goto out1;
  206. printk("%s: MAC ", dev->name);
  207. for (i = 0; i < 6; i++) {
  208. printk("%2.2x", dev->dev_addr[i]);
  209. if (i < 5)
  210. printk(":");
  211. }
  212. printk(" IRQ %d\n", dev->irq);
  213. return 0;
  214. out1:
  215. release_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. #define SONIC_IRQ_FLAG IRQF_DISABLED
  224. #include "sonic.c"
  225. static int __devexit 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_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 = __devexit_p(jazz_sonic_device_remove),
  239. .driver = {
  240. .name = jazz_sonic_string,
  241. },
  242. };
  243. static int __init jazz_sonic_init_module(void)
  244. {
  245. int err;
  246. if ((err = platform_driver_register(&jazz_sonic_driver))) {
  247. printk(KERN_ERR "Driver registration failed\n");
  248. return err;
  249. }
  250. jazz_sonic_device = platform_device_alloc(jazz_sonic_string, 0);
  251. if (!jazz_sonic_device)
  252. goto out_unregister;
  253. if (platform_device_add(jazz_sonic_device)) {
  254. platform_device_put(jazz_sonic_device);
  255. jazz_sonic_device = NULL;
  256. }
  257. return 0;
  258. out_unregister:
  259. platform_driver_unregister(&jazz_sonic_driver);
  260. return -ENOMEM;
  261. }
  262. static void __exit jazz_sonic_cleanup_module(void)
  263. {
  264. platform_driver_unregister(&jazz_sonic_driver);
  265. if (jazz_sonic_device) {
  266. platform_device_unregister(jazz_sonic_device);
  267. jazz_sonic_device = NULL;
  268. }
  269. }
  270. module_init(jazz_sonic_init_module);
  271. module_exit(jazz_sonic_cleanup_module);