skisa.c 9.5 KB

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