skisa.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 mid@auk.cx
  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 int __init setup_card(struct net_device *dev, struct device *pdev)
  113. {
  114. struct net_local *tp;
  115. static int versionprinted;
  116. const unsigned *port;
  117. int j, err = 0;
  118. if (!dev)
  119. return -ENOMEM;
  120. SET_MODULE_OWNER(dev);
  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: ");
  142. printk("%2.2x", dev->dev_addr[0]);
  143. for (j = 1; j < 6; j++)
  144. printk(":%2.2x", dev->dev_addr[j]);
  145. printk("\n");
  146. tp = netdev_priv(dev);
  147. tp->setnselout = sk_isa_setnselout_pins;
  148. tp->sifreadb = sk_isa_sifreadb;
  149. tp->sifreadw = sk_isa_sifreadw;
  150. tp->sifwriteb = sk_isa_sifwriteb;
  151. tp->sifwritew = sk_isa_sifwritew;
  152. memcpy(tp->ProductID, isa_cardname, PROD_ID_SIZE + 1);
  153. tp->tmspriv = NULL;
  154. dev->open = sk_isa_open;
  155. dev->stop = tms380tr_close;
  156. if (dev->irq == 0)
  157. {
  158. for(j = 0; irqlist[j] != 0; j++)
  159. {
  160. dev->irq = irqlist[j];
  161. if (!request_irq(dev->irq, tms380tr_interrupt, 0,
  162. isa_cardname, dev))
  163. break;
  164. }
  165. if(irqlist[j] == 0)
  166. {
  167. printk(KERN_INFO "skisa.c: AutoSelect no IRQ available\n");
  168. goto out3;
  169. }
  170. }
  171. else
  172. {
  173. for(j = 0; irqlist[j] != 0; j++)
  174. if (irqlist[j] == dev->irq)
  175. break;
  176. if (irqlist[j] == 0)
  177. {
  178. printk(KERN_INFO "skisa.c: Illegal IRQ %d specified\n",
  179. dev->irq);
  180. goto out3;
  181. }
  182. if (request_irq(dev->irq, tms380tr_interrupt, 0,
  183. isa_cardname, dev))
  184. {
  185. printk(KERN_INFO "skisa.c: Selected IRQ %d not available\n",
  186. dev->irq);
  187. goto out3;
  188. }
  189. }
  190. if (dev->dma == 0)
  191. {
  192. for(j = 0; dmalist[j] != 0; j++)
  193. {
  194. dev->dma = dmalist[j];
  195. if (!request_dma(dev->dma, isa_cardname))
  196. break;
  197. }
  198. if(dmalist[j] == 0)
  199. {
  200. printk(KERN_INFO "skisa.c: AutoSelect no DMA available\n");
  201. goto out2;
  202. }
  203. }
  204. else
  205. {
  206. for(j = 0; dmalist[j] != 0; j++)
  207. if (dmalist[j] == dev->dma)
  208. break;
  209. if (dmalist[j] == 0)
  210. {
  211. printk(KERN_INFO "skisa.c: Illegal DMA %d specified\n",
  212. dev->dma);
  213. goto out2;
  214. }
  215. if (request_dma(dev->dma, isa_cardname))
  216. {
  217. printk(KERN_INFO "skisa.c: Selected DMA %d not available\n",
  218. dev->dma);
  219. goto out2;
  220. }
  221. }
  222. err = register_netdev(dev);
  223. if (err)
  224. goto out;
  225. printk(KERN_DEBUG "%s: IO: %#4lx IRQ: %d DMA: %d\n",
  226. dev->name, dev->base_addr, dev->irq, dev->dma);
  227. return 0;
  228. out:
  229. free_dma(dev->dma);
  230. out2:
  231. free_irq(dev->irq, dev);
  232. out3:
  233. tmsdev_term(dev);
  234. out4:
  235. release_region(dev->base_addr, SK_ISA_IO_EXTENT);
  236. out5:
  237. return err;
  238. }
  239. /*
  240. * Reads MAC address from adapter RAM, which should've read it from
  241. * the onboard ROM.
  242. *
  243. * Calling this on a board that does not support it can be a very
  244. * dangerous thing. The Madge board, for instance, will lock your
  245. * machine hard when this is called. Luckily, its supported in a
  246. * separate driver. --ASF
  247. */
  248. static void sk_isa_read_eeprom(struct net_device *dev)
  249. {
  250. int i;
  251. /* Address: 0000:0000 */
  252. sk_isa_sifwritew(dev, 0, SIFADX);
  253. sk_isa_sifwritew(dev, 0, SIFADR);
  254. /* Read six byte MAC address data */
  255. dev->addr_len = 6;
  256. for(i = 0; i < 6; i++)
  257. dev->dev_addr[i] = sk_isa_sifreadw(dev, SIFINC) >> 8;
  258. }
  259. unsigned short sk_isa_setnselout_pins(struct net_device *dev)
  260. {
  261. return 0;
  262. }
  263. static int sk_isa_open(struct net_device *dev)
  264. {
  265. struct net_local *tp = netdev_priv(dev);
  266. unsigned short val = 0;
  267. unsigned short oldval;
  268. int i;
  269. val = 0;
  270. for(i = 0; irqlist[i] != 0; i++)
  271. {
  272. if(irqlist[i] == dev->irq)
  273. break;
  274. }
  275. val |= CYCLE_TIME << 2;
  276. val |= i << 4;
  277. i = dev->dma - 5;
  278. val |= i;
  279. if(tp->DataRate == SPEED_4)
  280. val |= LINE_SPEED_BIT;
  281. else
  282. val &= ~LINE_SPEED_BIT;
  283. oldval = sk_isa_sifreadb(dev, POSREG);
  284. /* Leave cycle bits alone */
  285. oldval |= 0xf3;
  286. val &= oldval;
  287. sk_isa_sifwriteb(dev, val, POSREG);
  288. return tms380tr_open(dev);
  289. }
  290. #define ISATR_MAX_ADAPTERS 3
  291. static int io[ISATR_MAX_ADAPTERS];
  292. static int irq[ISATR_MAX_ADAPTERS];
  293. static int dma[ISATR_MAX_ADAPTERS];
  294. MODULE_LICENSE("GPL");
  295. module_param_array(io, int, NULL, 0);
  296. module_param_array(irq, int, NULL, 0);
  297. module_param_array(dma, int, NULL, 0);
  298. static struct platform_device *sk_isa_dev[ISATR_MAX_ADAPTERS];
  299. static struct platform_driver sk_isa_driver = {
  300. .driver = {
  301. .name = "skisa",
  302. },
  303. };
  304. static int __init sk_isa_init(void)
  305. {
  306. struct net_device *dev;
  307. struct platform_device *pdev;
  308. int i, num = 0, err = 0;
  309. err = platform_driver_register(&sk_isa_driver);
  310. if (err)
  311. return err;
  312. for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) {
  313. dev = alloc_trdev(sizeof(struct net_local));
  314. if (!dev)
  315. continue;
  316. dev->base_addr = io[i];
  317. dev->irq = irq[i];
  318. dev->dma = dma[i];
  319. pdev = platform_device_register_simple("skisa",
  320. i, NULL, 0);
  321. err = setup_card(dev, &pdev->dev);
  322. if (!err) {
  323. sk_isa_dev[i] = pdev;
  324. platform_set_drvdata(sk_isa_dev[i], dev);
  325. ++num;
  326. } else {
  327. platform_device_unregister(pdev);
  328. free_netdev(dev);
  329. }
  330. }
  331. printk(KERN_NOTICE "skisa.c: %d cards found.\n", num);
  332. /* Probe for cards. */
  333. if (num == 0) {
  334. printk(KERN_NOTICE "skisa.c: No cards found.\n");
  335. return (-ENODEV);
  336. }
  337. return (0);
  338. }
  339. static void __exit sk_isa_cleanup(void)
  340. {
  341. struct net_device *dev;
  342. int i;
  343. for (i = 0; i < ISATR_MAX_ADAPTERS ; i++) {
  344. struct platform_device *pdev = sk_isa_dev[i];
  345. if (!pdev)
  346. continue;
  347. dev = platform_get_drvdata(pdev);
  348. unregister_netdev(dev);
  349. release_region(dev->base_addr, SK_ISA_IO_EXTENT);
  350. free_irq(dev->irq, dev);
  351. free_dma(dev->dma);
  352. tmsdev_term(dev);
  353. free_netdev(dev);
  354. platform_set_drvdata(pdev, NULL);
  355. platform_device_unregister(pdev);
  356. }
  357. platform_driver_unregister(&sk_isa_driver);
  358. }
  359. module_init(sk_isa_init);
  360. module_exit(sk_isa_cleanup);