jazzsonic.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * sonic.c
  3. *
  4. * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
  5. *
  6. * This driver is based on work from Andreas Busse, but most of
  7. * the code is rewritten.
  8. *
  9. * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
  10. *
  11. * A driver for the onboard Sonic ethernet controller on Mips Jazz
  12. * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and
  13. * perhaps others, too)
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/init.h>
  21. #include <linux/ioport.h>
  22. #include <linux/in.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/delay.h>
  26. #include <linux/errno.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/bitops.h>
  31. #include <linux/device.h>
  32. #include <asm/bootinfo.h>
  33. #include <asm/system.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/io.h>
  36. #include <asm/dma.h>
  37. #include <asm/jazz.h>
  38. #include <asm/jazzdma.h>
  39. static char jazz_sonic_string[] = "jazzsonic";
  40. static struct platform_device *jazz_sonic_device;
  41. #define SONIC_MEM_SIZE 0x100
  42. #define SREGS_PAD(n) u16 n;
  43. #include "sonic.h"
  44. /*
  45. * Macros to access SONIC registers
  46. */
  47. #define SONIC_READ(reg) (*((volatile unsigned int *)base_addr+reg))
  48. #define SONIC_WRITE(reg,val) \
  49. do { \
  50. *((volatile unsigned int *)base_addr+(reg)) = (val); \
  51. } while (0)
  52. /* use 0 for production, 1 for verification, >2 for debug */
  53. #ifdef SONIC_DEBUG
  54. static unsigned int sonic_debug = SONIC_DEBUG;
  55. #else
  56. static unsigned int sonic_debug = 1;
  57. #endif
  58. /*
  59. * Base address and interrupt of the SONIC controller on JAZZ boards
  60. */
  61. static struct {
  62. unsigned int port;
  63. unsigned int irq;
  64. } sonic_portlist[] = { {JAZZ_ETHERNET_BASE, JAZZ_ETHERNET_IRQ}, {0, 0}};
  65. /*
  66. * We cannot use station (ethernet) address prefixes to detect the
  67. * sonic controller since these are board manufacturer depended.
  68. * So we check for known Silicon Revision IDs instead.
  69. */
  70. static unsigned short known_revisions[] =
  71. {
  72. 0x04, /* Mips Magnum 4000 */
  73. 0xffff /* end of list */
  74. };
  75. static int __init sonic_probe1(struct net_device *dev, unsigned long base_addr,
  76. unsigned int irq)
  77. {
  78. static unsigned version_printed;
  79. unsigned int silicon_revision;
  80. unsigned int val;
  81. struct sonic_local *lp;
  82. int err = -ENODEV;
  83. int i;
  84. if (!request_mem_region(base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
  85. return -EBUSY;
  86. /*
  87. * get the Silicon Revision ID. If this is one of the known
  88. * one assume that we found a SONIC ethernet controller at
  89. * the expected location.
  90. */
  91. silicon_revision = SONIC_READ(SONIC_SR);
  92. if (sonic_debug > 1)
  93. printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
  94. i = 0;
  95. while (known_revisions[i] != 0xffff
  96. && known_revisions[i] != silicon_revision)
  97. i++;
  98. if (known_revisions[i] == 0xffff) {
  99. printk("SONIC ethernet controller not found (0x%4x)\n",
  100. silicon_revision);
  101. goto out;
  102. }
  103. if (sonic_debug && version_printed++ == 0)
  104. printk(version);
  105. printk("%s: Sonic ethernet found at 0x%08lx, ", dev->name, base_addr);
  106. /* Fill in the 'dev' fields. */
  107. dev->base_addr = base_addr;
  108. dev->irq = irq;
  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. printk("HW Address ");
  121. for (i = 0; i < 6; i++) {
  122. printk("%2.2x", dev->dev_addr[i]);
  123. if (i<5)
  124. printk(":");
  125. }
  126. printk(" IRQ %d\n", irq);
  127. err = -ENOMEM;
  128. /* Initialize the device structure. */
  129. if (dev->priv == NULL) {
  130. /*
  131. * the memory be located in the same 64kb segment
  132. */
  133. lp = NULL;
  134. i = 0;
  135. do {
  136. lp = kmalloc(sizeof(*lp), GFP_KERNEL);
  137. if ((unsigned long) lp >> 16
  138. != ((unsigned long)lp + sizeof(*lp) ) >> 16) {
  139. /* FIXME, free the memory later */
  140. kfree(lp);
  141. lp = NULL;
  142. }
  143. } while (lp == NULL && i++ < 20);
  144. if (lp == NULL) {
  145. printk("%s: couldn't allocate memory for descriptors\n",
  146. dev->name);
  147. goto out;
  148. }
  149. memset(lp, 0, sizeof(struct sonic_local));
  150. /* get the virtual dma address */
  151. lp->cda_laddr = vdma_alloc(CPHYSADDR(lp),sizeof(*lp));
  152. if (lp->cda_laddr == ~0UL) {
  153. printk("%s: couldn't get DMA page entry for "
  154. "descriptors\n", dev->name);
  155. goto out1;
  156. }
  157. lp->tda_laddr = lp->cda_laddr + sizeof (lp->cda);
  158. lp->rra_laddr = lp->tda_laddr + sizeof (lp->tda);
  159. lp->rda_laddr = lp->rra_laddr + sizeof (lp->rra);
  160. /* allocate receive buffer area */
  161. /* FIXME, maybe we should use skbs */
  162. lp->rba = kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_KERNEL);
  163. if (!lp->rba) {
  164. printk("%s: couldn't allocate receive buffers\n",
  165. dev->name);
  166. goto out2;
  167. }
  168. /* get virtual dma address */
  169. lp->rba_laddr = vdma_alloc(CPHYSADDR(lp->rba),
  170. SONIC_NUM_RRS * SONIC_RBSIZE);
  171. if (lp->rba_laddr == ~0UL) {
  172. printk("%s: couldn't get DMA page entry for receive "
  173. "buffers\n",dev->name);
  174. goto out3;
  175. }
  176. /* now convert pointer to KSEG1 pointer */
  177. lp->rba = (char *)KSEG1ADDR(lp->rba);
  178. flush_cache_all();
  179. dev->priv = (struct sonic_local *)KSEG1ADDR(lp);
  180. }
  181. lp = (struct sonic_local *)dev->priv;
  182. dev->open = sonic_open;
  183. dev->stop = sonic_close;
  184. dev->hard_start_xmit = sonic_send_packet;
  185. dev->get_stats = sonic_get_stats;
  186. dev->set_multicast_list = &sonic_multicast_list;
  187. dev->watchdog_timeo = TX_TIMEOUT;
  188. /*
  189. * clear tally counter
  190. */
  191. SONIC_WRITE(SONIC_CRCT,0xffff);
  192. SONIC_WRITE(SONIC_FAET,0xffff);
  193. SONIC_WRITE(SONIC_MPT,0xffff);
  194. return 0;
  195. out3:
  196. kfree(lp->rba);
  197. out2:
  198. vdma_free(lp->cda_laddr);
  199. out1:
  200. kfree(lp);
  201. out:
  202. release_region(base_addr, SONIC_MEM_SIZE);
  203. return err;
  204. }
  205. /*
  206. * Probe for a SONIC ethernet controller on a Mips Jazz board.
  207. * Actually probing is superfluous but we're paranoid.
  208. */
  209. static int __init jazz_sonic_probe(struct device *device)
  210. {
  211. struct net_device *dev;
  212. struct sonic_local *lp;
  213. unsigned long base_addr;
  214. int err = 0;
  215. int i;
  216. /*
  217. * Don't probe if we're not running on a Jazz board.
  218. */
  219. if (mips_machgroup != MACH_GROUP_JAZZ)
  220. return -ENODEV;
  221. dev = alloc_etherdev(0);
  222. if (!dev)
  223. return -ENOMEM;
  224. netdev_boot_setup_check(dev);
  225. base_addr = dev->base_addr;
  226. if (base_addr >= KSEG0) { /* Check a single specified location. */
  227. err = sonic_probe1(dev, base_addr, dev->irq);
  228. } else if (base_addr != 0) { /* Don't probe at all. */
  229. err = -ENXIO;
  230. } else {
  231. for (i = 0; sonic_portlist[i].port; i++) {
  232. int io = sonic_portlist[i].port;
  233. if (sonic_probe1(dev, io, sonic_portlist[i].irq) == 0)
  234. break;
  235. }
  236. if (!sonic_portlist[i].port)
  237. err = -ENODEV;
  238. }
  239. if (err)
  240. goto out;
  241. err = register_netdev(dev);
  242. if (err)
  243. goto out1;
  244. return 0;
  245. out1:
  246. lp = dev->priv;
  247. vdma_free(lp->rba_laddr);
  248. kfree(lp->rba);
  249. vdma_free(lp->cda_laddr);
  250. kfree(lp);
  251. release_region(dev->base_addr, SONIC_MEM_SIZE);
  252. out:
  253. free_netdev(dev);
  254. return err;
  255. }
  256. /*
  257. * SONIC uses a normal IRQ
  258. */
  259. #define sonic_request_irq request_irq
  260. #define sonic_free_irq free_irq
  261. #define sonic_chiptomem(x) KSEG1ADDR(vdma_log2phys(x))
  262. #include "sonic.c"
  263. static int __devexit jazz_sonic_device_remove (struct device *device)
  264. {
  265. struct net_device *dev = device->driver_data;
  266. unregister_netdev (dev);
  267. release_region (dev->base_addr, SONIC_MEM_SIZE);
  268. free_netdev (dev);
  269. return 0;
  270. }
  271. static struct device_driver jazz_sonic_driver = {
  272. .name = jazz_sonic_string,
  273. .bus = &platform_bus_type,
  274. .probe = jazz_sonic_probe,
  275. .remove = __devexit_p(jazz_sonic_device_remove),
  276. };
  277. static void jazz_sonic_platform_release (struct device *device)
  278. {
  279. struct platform_device *pldev;
  280. /* free device */
  281. pldev = to_platform_device (device);
  282. kfree (pldev);
  283. }
  284. static int __init jazz_sonic_init_module(void)
  285. {
  286. struct platform_device *pldev;
  287. if (driver_register(&jazz_sonic_driver)) {
  288. printk(KERN_ERR "Driver registration failed\n");
  289. return -ENOMEM;
  290. }
  291. jazz_sonic_device = NULL;
  292. if (!(pldev = kmalloc (sizeof (*pldev), GFP_KERNEL))) {
  293. goto out_unregister;
  294. }
  295. memset(pldev, 0, sizeof (*pldev));
  296. pldev->name = jazz_sonic_string;
  297. pldev->id = 0;
  298. pldev->dev.release = jazz_sonic_platform_release;
  299. jazz_sonic_device = pldev;
  300. if (platform_device_register (pldev)) {
  301. kfree(pldev);
  302. jazz_sonic_device = NULL;
  303. }
  304. return 0;
  305. out_unregister:
  306. platform_device_unregister(pldev);
  307. return -ENOMEM;
  308. }
  309. static void __exit jazz_sonic_cleanup_module(void)
  310. {
  311. driver_unregister(&jazz_sonic_driver);
  312. if (jazz_sonic_device) {
  313. platform_device_unregister(jazz_sonic_device);
  314. jazz_sonic_device = NULL;
  315. }
  316. }
  317. module_init(jazz_sonic_init_module);
  318. module_exit(jazz_sonic_cleanup_module);