abyss.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * abyss.c: Network driver for the Madge Smart 16/4 PCI Mk2 token ring card.
  3. *
  4. * Written 1999-2000 by Adam Fritzler
  5. *
  6. * This software may be used and distributed according to the terms
  7. * of the GNU General Public License, incorporated herein by reference.
  8. *
  9. * This driver module supports the following cards:
  10. * - Madge Smart 16/4 PCI Mk2
  11. *
  12. * Maintainer(s):
  13. * AF Adam Fritzler mid@auk.cx
  14. *
  15. * Modification History:
  16. * 30-Dec-99 AF Split off from the tms380tr driver.
  17. * 22-Jan-00 AF Updated to use indirect read/writes
  18. * 23-Nov-00 JG New PCI API, cleanups
  19. *
  20. *
  21. * TODO:
  22. * 1. See if we can use MMIO instead of inb/outb/inw/outw
  23. * 2. Add support for Mk1 (has AT24 attached to the PCI
  24. * config registers)
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/errno.h>
  30. #include <linux/pci.h>
  31. #include <linux/init.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/trdevice.h>
  34. #include <asm/system.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include "tms380tr.h"
  38. #include "abyss.h" /* Madge-specific constants */
  39. static char version[] __devinitdata =
  40. "abyss.c: v1.02 23/11/2000 by Adam Fritzler\n";
  41. #define ABYSS_IO_EXTENT 64
  42. static struct pci_device_id abyss_pci_tbl[] = {
  43. { PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_MK2,
  44. PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_TOKEN_RING << 8, 0x00ffffff, },
  45. { } /* Terminating entry */
  46. };
  47. MODULE_DEVICE_TABLE(pci, abyss_pci_tbl);
  48. MODULE_LICENSE("GPL");
  49. static int abyss_open(struct net_device *dev);
  50. static int abyss_close(struct net_device *dev);
  51. static void abyss_enable(struct net_device *dev);
  52. static int abyss_chipset_init(struct net_device *dev);
  53. static void abyss_read_eeprom(struct net_device *dev);
  54. static unsigned short abyss_setnselout_pins(struct net_device *dev);
  55. static void at24_writedatabyte(unsigned long regaddr, unsigned char byte);
  56. static int at24_sendfullcmd(unsigned long regaddr, unsigned char cmd, unsigned char addr);
  57. static int at24_sendcmd(unsigned long regaddr, unsigned char cmd);
  58. static unsigned char at24_readdatabit(unsigned long regaddr);
  59. static unsigned char at24_readdatabyte(unsigned long regaddr);
  60. static int at24_waitforack(unsigned long regaddr);
  61. static int at24_waitfornack(unsigned long regaddr);
  62. static void at24_setlines(unsigned long regaddr, unsigned char clock, unsigned char data);
  63. static void at24_start(unsigned long regaddr);
  64. static unsigned char at24_readb(unsigned long regaddr, unsigned char addr);
  65. static unsigned short abyss_sifreadb(struct net_device *dev, unsigned short reg)
  66. {
  67. return inb(dev->base_addr + reg);
  68. }
  69. static unsigned short abyss_sifreadw(struct net_device *dev, unsigned short reg)
  70. {
  71. return inw(dev->base_addr + reg);
  72. }
  73. static void abyss_sifwriteb(struct net_device *dev, unsigned short val, unsigned short reg)
  74. {
  75. outb(val, dev->base_addr + reg);
  76. }
  77. static void abyss_sifwritew(struct net_device *dev, unsigned short val, unsigned short reg)
  78. {
  79. outw(val, dev->base_addr + reg);
  80. }
  81. static int __devinit abyss_attach(struct pci_dev *pdev, const struct pci_device_id *ent)
  82. {
  83. static int versionprinted;
  84. struct net_device *dev;
  85. struct net_local *tp;
  86. int i, ret, pci_irq_line;
  87. unsigned long pci_ioaddr;
  88. if (versionprinted++ == 0)
  89. printk("%s", version);
  90. if (pci_enable_device(pdev))
  91. return -EIO;
  92. /* Remove I/O space marker in bit 0. */
  93. pci_irq_line = pdev->irq;
  94. pci_ioaddr = pci_resource_start (pdev, 0);
  95. /* At this point we have found a valid card. */
  96. dev = alloc_trdev(sizeof(struct net_local));
  97. if (!dev)
  98. return -ENOMEM;
  99. SET_MODULE_OWNER(dev);
  100. if (!request_region(pci_ioaddr, ABYSS_IO_EXTENT, dev->name)) {
  101. ret = -EBUSY;
  102. goto err_out_trdev;
  103. }
  104. ret = request_irq(pdev->irq, tms380tr_interrupt, SA_SHIRQ,
  105. dev->name, dev);
  106. if (ret)
  107. goto err_out_region;
  108. dev->base_addr = pci_ioaddr;
  109. dev->irq = pci_irq_line;
  110. printk("%s: Madge Smart 16/4 PCI Mk2 (Abyss)\n", dev->name);
  111. printk("%s: IO: %#4lx IRQ: %d\n",
  112. dev->name, pci_ioaddr, dev->irq);
  113. /*
  114. * The TMS SIF registers lay 0x10 above the card base address.
  115. */
  116. dev->base_addr += 0x10;
  117. ret = tmsdev_init(dev, &pdev->dev);
  118. if (ret) {
  119. printk("%s: unable to get memory for dev->priv.\n",
  120. dev->name);
  121. goto err_out_irq;
  122. }
  123. abyss_read_eeprom(dev);
  124. printk("%s: Ring Station Address: ", dev->name);
  125. printk("%2.2x", dev->dev_addr[0]);
  126. for (i = 1; i < 6; i++)
  127. printk(":%2.2x", dev->dev_addr[i]);
  128. printk("\n");
  129. tp = netdev_priv(dev);
  130. tp->setnselout = abyss_setnselout_pins;
  131. tp->sifreadb = abyss_sifreadb;
  132. tp->sifreadw = abyss_sifreadw;
  133. tp->sifwriteb = abyss_sifwriteb;
  134. tp->sifwritew = abyss_sifwritew;
  135. memcpy(tp->ProductID, "Madge PCI 16/4 Mk2", PROD_ID_SIZE + 1);
  136. dev->open = abyss_open;
  137. dev->stop = abyss_close;
  138. pci_set_drvdata(pdev, dev);
  139. SET_NETDEV_DEV(dev, &pdev->dev);
  140. ret = register_netdev(dev);
  141. if (ret)
  142. goto err_out_tmsdev;
  143. return 0;
  144. err_out_tmsdev:
  145. pci_set_drvdata(pdev, NULL);
  146. tmsdev_term(dev);
  147. err_out_irq:
  148. free_irq(pdev->irq, dev);
  149. err_out_region:
  150. release_region(pci_ioaddr, ABYSS_IO_EXTENT);
  151. err_out_trdev:
  152. free_netdev(dev);
  153. return ret;
  154. }
  155. static unsigned short abyss_setnselout_pins(struct net_device *dev)
  156. {
  157. unsigned short val = 0;
  158. struct net_local *tp = netdev_priv(dev);
  159. if(tp->DataRate == SPEED_4)
  160. val |= 0x01; /* Set 4Mbps */
  161. else
  162. val |= 0x00; /* Set 16Mbps */
  163. return val;
  164. }
  165. /*
  166. * The following Madge boards should use this code:
  167. * - Smart 16/4 PCI Mk2 (Abyss)
  168. * - Smart 16/4 PCI Mk1 (PCI T)
  169. * - Smart 16/4 Client Plus PnP (Big Apple)
  170. * - Smart 16/4 Cardbus Mk2
  171. *
  172. * These access an Atmel AT24 SEEPROM using their glue chip registers.
  173. *
  174. */
  175. static void at24_writedatabyte(unsigned long regaddr, unsigned char byte)
  176. {
  177. int i;
  178. for (i = 0; i < 8; i++) {
  179. at24_setlines(regaddr, 0, (byte >> (7-i))&0x01);
  180. at24_setlines(regaddr, 1, (byte >> (7-i))&0x01);
  181. at24_setlines(regaddr, 0, (byte >> (7-i))&0x01);
  182. }
  183. }
  184. static int at24_sendfullcmd(unsigned long regaddr, unsigned char cmd, unsigned char addr)
  185. {
  186. if (at24_sendcmd(regaddr, cmd)) {
  187. at24_writedatabyte(regaddr, addr);
  188. return at24_waitforack(regaddr);
  189. }
  190. return 0;
  191. }
  192. static int at24_sendcmd(unsigned long regaddr, unsigned char cmd)
  193. {
  194. int i;
  195. for (i = 0; i < 10; i++) {
  196. at24_start(regaddr);
  197. at24_writedatabyte(regaddr, cmd);
  198. if (at24_waitforack(regaddr))
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. static unsigned char at24_readdatabit(unsigned long regaddr)
  204. {
  205. unsigned char val;
  206. at24_setlines(regaddr, 0, 1);
  207. at24_setlines(regaddr, 1, 1);
  208. val = (inb(regaddr) & AT24_DATA)?1:0;
  209. at24_setlines(regaddr, 1, 1);
  210. at24_setlines(regaddr, 0, 1);
  211. return val;
  212. }
  213. static unsigned char at24_readdatabyte(unsigned long regaddr)
  214. {
  215. unsigned char data = 0;
  216. int i;
  217. for (i = 0; i < 8; i++) {
  218. data <<= 1;
  219. data |= at24_readdatabit(regaddr);
  220. }
  221. return data;
  222. }
  223. static int at24_waitforack(unsigned long regaddr)
  224. {
  225. int i;
  226. for (i = 0; i < 10; i++) {
  227. if ((at24_readdatabit(regaddr) & 0x01) == 0x00)
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static int at24_waitfornack(unsigned long regaddr)
  233. {
  234. int i;
  235. for (i = 0; i < 10; i++) {
  236. if ((at24_readdatabit(regaddr) & 0x01) == 0x01)
  237. return 1;
  238. }
  239. return 0;
  240. }
  241. static void at24_setlines(unsigned long regaddr, unsigned char clock, unsigned char data)
  242. {
  243. unsigned char val = AT24_ENABLE;
  244. if (clock)
  245. val |= AT24_CLOCK;
  246. if (data)
  247. val |= AT24_DATA;
  248. outb(val, regaddr);
  249. tms380tr_wait(20); /* Very necessary. */
  250. }
  251. static void at24_start(unsigned long regaddr)
  252. {
  253. at24_setlines(regaddr, 0, 1);
  254. at24_setlines(regaddr, 1, 1);
  255. at24_setlines(regaddr, 1, 0);
  256. at24_setlines(regaddr, 0, 1);
  257. }
  258. static unsigned char at24_readb(unsigned long regaddr, unsigned char addr)
  259. {
  260. unsigned char data = 0xff;
  261. if (at24_sendfullcmd(regaddr, AT24_WRITE, addr)) {
  262. if (at24_sendcmd(regaddr, AT24_READ)) {
  263. data = at24_readdatabyte(regaddr);
  264. if (!at24_waitfornack(regaddr))
  265. data = 0xff;
  266. }
  267. }
  268. return data;
  269. }
  270. /*
  271. * Enable basic functions of the Madge chipset needed
  272. * for initialization.
  273. */
  274. static void abyss_enable(struct net_device *dev)
  275. {
  276. unsigned char reset_reg;
  277. unsigned long ioaddr;
  278. ioaddr = dev->base_addr;
  279. reset_reg = inb(ioaddr + PCIBM2_RESET_REG);
  280. reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
  281. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  282. tms380tr_wait(100);
  283. }
  284. /*
  285. * Enable the functions of the Madge chipset needed for
  286. * full working order.
  287. */
  288. static int abyss_chipset_init(struct net_device *dev)
  289. {
  290. unsigned char reset_reg;
  291. unsigned long ioaddr;
  292. ioaddr = dev->base_addr;
  293. reset_reg = inb(ioaddr + PCIBM2_RESET_REG);
  294. reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
  295. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  296. reset_reg &= ~(PCIBM2_RESET_REG_CHIP_NRES |
  297. PCIBM2_RESET_REG_FIFO_NRES |
  298. PCIBM2_RESET_REG_SIF_NRES);
  299. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  300. tms380tr_wait(100);
  301. reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
  302. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  303. reset_reg |= PCIBM2_RESET_REG_SIF_NRES;
  304. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  305. reset_reg |= PCIBM2_RESET_REG_FIFO_NRES;
  306. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  307. outb(PCIBM2_INT_CONTROL_REG_SINTEN |
  308. PCIBM2_INT_CONTROL_REG_PCI_ERR_ENABLE,
  309. ioaddr + PCIBM2_INT_CONTROL_REG);
  310. outb(30, ioaddr + PCIBM2_FIFO_THRESHOLD);
  311. return 0;
  312. }
  313. static inline void abyss_chipset_close(struct net_device *dev)
  314. {
  315. unsigned long ioaddr;
  316. ioaddr = dev->base_addr;
  317. outb(0, ioaddr + PCIBM2_RESET_REG);
  318. }
  319. /*
  320. * Read configuration data from the AT24 SEEPROM on Madge cards.
  321. *
  322. */
  323. static void abyss_read_eeprom(struct net_device *dev)
  324. {
  325. struct net_local *tp;
  326. unsigned long ioaddr;
  327. unsigned short val;
  328. int i;
  329. tp = netdev_priv(dev);
  330. ioaddr = dev->base_addr;
  331. /* Must enable glue chip first */
  332. abyss_enable(dev);
  333. val = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
  334. PCIBM2_SEEPROM_RING_SPEED);
  335. tp->DataRate = val?SPEED_4:SPEED_16; /* set open speed */
  336. printk("%s: SEEPROM: ring speed: %dMb/sec\n", dev->name, tp->DataRate);
  337. val = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
  338. PCIBM2_SEEPROM_RAM_SIZE) * 128;
  339. printk("%s: SEEPROM: adapter RAM: %dkb\n", dev->name, val);
  340. dev->addr_len = 6;
  341. for (i = 0; i < 6; i++)
  342. dev->dev_addr[i] = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
  343. PCIBM2_SEEPROM_BIA+i);
  344. }
  345. static int abyss_open(struct net_device *dev)
  346. {
  347. abyss_chipset_init(dev);
  348. tms380tr_open(dev);
  349. return 0;
  350. }
  351. static int abyss_close(struct net_device *dev)
  352. {
  353. tms380tr_close(dev);
  354. abyss_chipset_close(dev);
  355. return 0;
  356. }
  357. static void __devexit abyss_detach (struct pci_dev *pdev)
  358. {
  359. struct net_device *dev = pci_get_drvdata(pdev);
  360. if (!dev)
  361. BUG();
  362. unregister_netdev(dev);
  363. release_region(dev->base_addr-0x10, ABYSS_IO_EXTENT);
  364. free_irq(dev->irq, dev);
  365. tmsdev_term(dev);
  366. free_netdev(dev);
  367. pci_set_drvdata(pdev, NULL);
  368. }
  369. static struct pci_driver abyss_driver = {
  370. .name = "abyss",
  371. .id_table = abyss_pci_tbl,
  372. .probe = abyss_attach,
  373. .remove = __devexit_p(abyss_detach),
  374. };
  375. static int __init abyss_init (void)
  376. {
  377. return pci_register_driver(&abyss_driver);
  378. }
  379. static void __exit abyss_rmmod (void)
  380. {
  381. pci_unregister_driver (&abyss_driver);
  382. }
  383. module_init(abyss_init);
  384. module_exit(abyss_rmmod);