skisa.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * skisa.c: A network driver for SK-NET TMS380-based ISA token ring cards.
  3. *
  4. * Based on tmspci written 1999 by Adam Fritzler
  5. *
  6. * Written 2000 by Jochen Friedrich
  7. * Dedicated to my girlfriend Steffi Bopp
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. *
  12. * This driver module supports the following cards:
  13. * - SysKonnect TR4/16(+) ISA (SK-4190)
  14. *
  15. * Maintainer(s):
  16. * AF Adam Fritzler
  17. * JF Jochen Friedrich jochen@scram.de
  18. *
  19. * Modification History:
  20. * 14-Jan-01 JF Created
  21. * 28-Oct-02 JF Fixed probe of card for static compilation.
  22. * Fixed module init to not make hotplug go wild.
  23. * 09-Nov-02 JF Fixed early bail out on out of memory
  24. * situations if multiple cards are found.
  25. * Cleaned up some unnecessary console SPAM.
  26. * 09-Dec-02 JF Fixed module reference counting.
  27. * 02-Jan-03 JF Renamed to skisa.c
  28. *
  29. */
  30. static const char version[] = "skisa.c: v1.03 09/12/2002 by Jochen Friedrich\n";
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. #include <linux/errno.h>
  34. #include <linux/pci.h>
  35. #include <linux/init.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/trdevice.h>
  38. #include <linux/platform_device.h>
  39. #include <asm/system.h>
  40. #include <asm/io.h>
  41. #include <asm/irq.h>
  42. #include <asm/pci.h>
  43. #include <asm/dma.h>
  44. #include "tms380tr.h"
  45. #define SK_ISA_IO_EXTENT 32
  46. /* A zero-terminated list of I/O addresses to be probed. */
  47. static unsigned int portlist[] __initdata = {
  48. 0x0A20, 0x1A20, 0x0B20, 0x1B20, 0x0980, 0x1980, 0x0900, 0x1900,// SK
  49. 0
  50. };
  51. /* A zero-terminated list of IRQs to be probed.
  52. * Used again after initial probe for sktr_chipset_init, called from sktr_open.
  53. */
  54. static const unsigned short irqlist[] = {
  55. 3, 5, 9, 10, 11, 12, 15,
  56. 0
  57. };
  58. /* A zero-terminated list of DMAs to be probed. */
  59. static int dmalist[] __initdata = {
  60. 5, 6, 7,
  61. 0
  62. };
  63. static char isa_cardname[] = "SK NET TR 4/16 ISA\0";
  64. static u64 dma_mask = ISA_MAX_ADDRESS;
  65. static int sk_isa_open(struct net_device *dev);
  66. static void sk_isa_read_eeprom(struct net_device *dev);
  67. static unsigned short sk_isa_setnselout_pins(struct net_device *dev);
  68. static unsigned short sk_isa_sifreadb(struct net_device *dev, unsigned short reg)
  69. {
  70. return inb(dev->base_addr + reg);
  71. }
  72. static unsigned short sk_isa_sifreadw(struct net_device *dev, unsigned short reg)
  73. {
  74. return inw(dev->base_addr + reg);
  75. }
  76. static void sk_isa_sifwriteb(struct net_device *dev, unsigned short val, unsigned short reg)
  77. {
  78. outb(val, dev->base_addr + reg);
  79. }
  80. static void sk_isa_sifwritew(struct net_device *dev, unsigned short val, unsigned short reg)
  81. {
  82. outw(val, dev->base_addr + reg);
  83. }
  84. static int __init sk_isa_probe1(struct net_device *dev, int ioaddr)
  85. {
  86. unsigned char old, chk1, chk2;
  87. if (!request_region(ioaddr, SK_ISA_IO_EXTENT, isa_cardname))
  88. return -ENODEV;
  89. old = inb(ioaddr + SIFADR); /* Get the old SIFADR value */
  90. chk1 = 0; /* Begin with check value 0 */
  91. do {
  92. /* Write new SIFADR value */
  93. outb(chk1, ioaddr + SIFADR);
  94. /* Read, invert and write */
  95. chk2 = inb(ioaddr + SIFADD);
  96. chk2 ^= 0x0FE;
  97. outb(chk2, ioaddr + SIFADR);
  98. /* Read, invert and compare */
  99. chk2 = inb(ioaddr + SIFADD);
  100. chk2 ^= 0x0FE;
  101. if(chk1 != chk2) {
  102. release_region(ioaddr, SK_ISA_IO_EXTENT);
  103. return -ENODEV;
  104. }
  105. chk1 -= 2;
  106. } while(chk1 != 0); /* Repeat 128 times (all byte values) */
  107. /* Restore the SIFADR value */
  108. outb(old, ioaddr + SIFADR);
  109. dev->base_addr = ioaddr;
  110. return 0;
  111. }
  112. static struct net_device_ops sk_isa_netdev_ops __read_mostly;
  113. static int __init setup_card(struct net_device *dev, struct device *pdev)
  114. {
  115. struct net_local *tp;
  116. static int versionprinted;
  117. const unsigned *port;
  118. int j, err = 0;
  119. if (!dev)
  120. return -ENOMEM;
  121. if (dev->base_addr) /* probe specific location */
  122. err = sk_isa_probe1(dev, dev->base_addr);
  123. else {
  124. for (port = portlist; *port; port++) {
  125. err = sk_isa_probe1(dev, *port);
  126. if (!err)
  127. break;
  128. }
  129. }
  130. if (err)
  131. goto out5;
  132. /* At this point we have found a valid card. */
  133. if (versionprinted++ == 0)
  134. printk(KERN_DEBUG "%s", version);
  135. err = -EIO;
  136. pdev->dma_mask = &dma_mask;
  137. if (tmsdev_init(dev, pdev))
  138. goto out4;
  139. dev->base_addr &= ~3;
  140. sk_isa_read_eeprom(dev);
  141. printk(KERN_DEBUG "skisa.c: Ring Station Address: %pM\n",
  142. dev->dev_addr);
  143. tp = netdev_priv(dev);
  144. tp->setnselout = sk_isa_setnselout_pins;
  145. tp->sifreadb = sk_isa_sifreadb;
  146. tp->sifreadw = sk_isa_sifreadw;
  147. tp->sifwriteb = sk_isa_sifwriteb;
  148. tp->sifwritew = sk_isa_sifwritew;
  149. memcpy(tp->ProductID, isa_cardname, PROD_ID_SIZE + 1);
  150. tp->tmspriv = NULL;
  151. dev->netdev_ops = &sk_isa_netdev_ops;
  152. if (dev->irq == 0)
  153. {
  154. for(j = 0; irqlist[j] != 0; j++)
  155. {
  156. dev->irq = irqlist[j];
  157. if (!request_irq(dev->irq, tms380tr_interrupt, 0,
  158. isa_cardname, dev))
  159. break;
  160. }
  161. if(irqlist[j] == 0)
  162. {
  163. printk(KERN_INFO "skisa.c: AutoSelect no IRQ available\n");
  164. goto out3;
  165. }
  166. }
  167. else
  168. {
  169. for(j = 0; irqlist[j] != 0; j++)
  170. if (irqlist[j] == dev->irq)
  171. break;
  172. if (irqlist[j] == 0)
  173. {
  174. printk(KERN_INFO "skisa.c: Illegal IRQ %d specified\n",
  175. dev->irq);
  176. goto out3;
  177. }
  178. if (request_irq(dev->irq, tms380tr_interrupt, 0,
  179. isa_cardname, dev))
  180. {
  181. printk(KERN_INFO "skisa.c: Selected IRQ %d not available\n",
  182. dev->irq);
  183. goto out3;
  184. }
  185. }
  186. if (dev->dma == 0)
  187. {
  188. for(j = 0; dmalist[j] != 0; j++)
  189. {
  190. dev->dma = dmalist[j];
  191. if (!request_dma(dev->dma, isa_cardname))
  192. break;
  193. }
  194. if(dmalist[j] == 0)
  195. {
  196. printk(KERN_INFO "skisa.c: AutoSelect no DMA available\n");
  197. goto out2;
  198. }
  199. }
  200. else
  201. {
  202. for(j = 0; dmalist[j] != 0; j++)
  203. if (dmalist[j] == dev->dma)
  204. break;
  205. if (dmalist[j] == 0)
  206. {
  207. printk(KERN_INFO "skisa.c: Illegal DMA %d specified\n",
  208. dev->dma);
  209. goto out2;
  210. }
  211. if (request_dma(dev->dma, isa_cardname))
  212. {
  213. printk(KERN_INFO "skisa.c: Selected DMA %d not available\n",
  214. dev->dma);
  215. goto out2;
  216. }
  217. }
  218. err = register_netdev(dev);
  219. if (err)
  220. goto out;
  221. printk(KERN_DEBUG "%s: IO: %#4lx IRQ: %d DMA: %d\n",
  222. dev->name, dev->base_addr, dev->irq, dev->dma);
  223. return 0;
  224. out:
  225. free_dma(dev->dma);
  226. out2:
  227. free_irq(dev->irq, dev);
  228. out3:
  229. tmsdev_term(dev);
  230. out4:
  231. release_region(dev->base_addr, SK_ISA_IO_EXTENT);
  232. out5:
  233. return err;
  234. }
  235. /*
  236. * Reads MAC address from adapter RAM, which should've read it from
  237. * the onboard ROM.
  238. *
  239. * Calling this on a board that does not support it can be a very
  240. * dangerous thing. The Madge board, for instance, will lock your
  241. * machine hard when this is called. Luckily, its supported in a
  242. * separate driver. --ASF
  243. */
  244. static void sk_isa_read_eeprom(struct net_device *dev)
  245. {
  246. int i;
  247. /* Address: 0000:0000 */
  248. sk_isa_sifwritew(dev, 0, SIFADX);
  249. sk_isa_sifwritew(dev, 0, SIFADR);
  250. /* Read six byte MAC address data */
  251. dev->addr_len = 6;
  252. for(i = 0; i < 6; i++)
  253. dev->dev_addr[i] = sk_isa_sifreadw(dev, SIFINC) >> 8;
  254. }
  255. static unsigned short sk_isa_setnselout_pins(struct net_device *dev)
  256. {
  257. return 0;
  258. }
  259. static int sk_isa_open(struct net_device *dev)
  260. {
  261. struct net_local *tp = netdev_priv(dev);
  262. unsigned short val = 0;
  263. unsigned short oldval;
  264. int i;
  265. val = 0;
  266. for(i = 0; irqlist[i] != 0; i++)
  267. {
  268. if(irqlist[i] == dev->irq)
  269. break;
  270. }
  271. val |= CYCLE_TIME << 2;
  272. val |= i << 4;
  273. i = dev->dma - 5;
  274. val |= i;
  275. if(tp->DataRate == SPEED_4)
  276. val |= LINE_SPEED_BIT;
  277. else
  278. val &= ~LINE_SPEED_BIT;
  279. oldval = sk_isa_sifreadb(dev, POSREG);
  280. /* Leave cycle bits alone */
  281. oldval |= 0xf3;
  282. val &= oldval;
  283. sk_isa_sifwriteb(dev, val, POSREG);
  284. return tms380tr_open(dev);
  285. }
  286. #define ISATR_MAX_ADAPTERS 3
  287. static int io[ISATR_MAX_ADAPTERS];
  288. static int irq[ISATR_MAX_ADAPTERS];
  289. static int dma[ISATR_MAX_ADAPTERS];
  290. MODULE_LICENSE("GPL");
  291. module_param_array(io, int, NULL, 0);
  292. module_param_array(irq, int, NULL, 0);
  293. module_param_array(dma, int, NULL, 0);
  294. static struct platform_device *sk_isa_dev[ISATR_MAX_ADAPTERS];
  295. static struct platform_driver sk_isa_driver = {
  296. .driver = {
  297. .name = "skisa",
  298. },
  299. };
  300. static int __init sk_isa_init(void)
  301. {
  302. struct net_device *dev;
  303. struct platform_device *pdev;
  304. int i, num = 0, err = 0;
  305. sk_isa_netdev_ops = tms380tr_netdev_ops;
  306. sk_isa_netdev_ops.ndo_open = sk_isa_open;
  307. sk_isa_netdev_ops.ndo_stop = tms380tr_close;
  308. err = platform_driver_register(&sk_isa_driver);
  309. if (err)
  310. return err;
  311. for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) {
  312. dev = alloc_trdev(sizeof(struct net_local));
  313. if (!dev)
  314. continue;
  315. dev->base_addr = io[i];
  316. dev->irq = irq[i];
  317. dev->dma = dma[i];
  318. pdev = platform_device_register_simple("skisa",
  319. i, NULL, 0);
  320. if (IS_ERR(pdev)) {
  321. free_netdev(dev);
  322. continue;
  323. }
  324. err = setup_card(dev, &pdev->dev);
  325. if (!err) {
  326. sk_isa_dev[i] = pdev;
  327. platform_set_drvdata(sk_isa_dev[i], dev);
  328. ++num;
  329. } else {
  330. platform_device_unregister(pdev);
  331. free_netdev(dev);
  332. }
  333. }
  334. printk(KERN_NOTICE "skisa.c: %d cards found.\n", num);
  335. /* Probe for cards. */
  336. if (num == 0) {
  337. printk(KERN_NOTICE "skisa.c: No cards found.\n");
  338. platform_driver_unregister(&sk_isa_driver);
  339. return -ENODEV;
  340. }
  341. return 0;
  342. }
  343. static void __exit sk_isa_cleanup(void)
  344. {
  345. struct net_device *dev;
  346. int i;
  347. for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) {
  348. struct platform_device *pdev = sk_isa_dev[i];
  349. if (!pdev)
  350. continue;
  351. dev = platform_get_drvdata(pdev);
  352. unregister_netdev(dev);
  353. release_region(dev->base_addr, SK_ISA_IO_EXTENT);
  354. free_irq(dev->irq, dev);
  355. free_dma(dev->dma);
  356. tmsdev_term(dev);
  357. free_netdev(dev);
  358. platform_set_drvdata(pdev, NULL);
  359. platform_device_unregister(pdev);
  360. }
  361. platform_driver_unregister(&sk_isa_driver);
  362. }
  363. module_init(sk_isa_init);
  364. module_exit(sk_isa_cleanup);