abyss.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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
  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 struct net_device_ops abyss_netdev_ops;
  82. static int __devinit abyss_attach(struct pci_dev *pdev, const struct pci_device_id *ent)
  83. {
  84. static int versionprinted;
  85. struct net_device *dev;
  86. struct net_local *tp;
  87. int ret, pci_irq_line;
  88. unsigned long pci_ioaddr;
  89. if (versionprinted++ == 0)
  90. printk("%s", version);
  91. if (pci_enable_device(pdev))
  92. return -EIO;
  93. /* Remove I/O space marker in bit 0. */
  94. pci_irq_line = pdev->irq;
  95. pci_ioaddr = pci_resource_start (pdev, 0);
  96. /* At this point we have found a valid card. */
  97. dev = alloc_trdev(sizeof(struct net_local));
  98. if (!dev)
  99. return -ENOMEM;
  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, IRQF_SHARED,
  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: %pM\n", dev->name, dev->dev_addr);
  125. tp = netdev_priv(dev);
  126. tp->setnselout = abyss_setnselout_pins;
  127. tp->sifreadb = abyss_sifreadb;
  128. tp->sifreadw = abyss_sifreadw;
  129. tp->sifwriteb = abyss_sifwriteb;
  130. tp->sifwritew = abyss_sifwritew;
  131. memcpy(tp->ProductID, "Madge PCI 16/4 Mk2", PROD_ID_SIZE + 1);
  132. dev->netdev_ops = &abyss_netdev_ops;
  133. pci_set_drvdata(pdev, dev);
  134. SET_NETDEV_DEV(dev, &pdev->dev);
  135. ret = register_netdev(dev);
  136. if (ret)
  137. goto err_out_tmsdev;
  138. return 0;
  139. err_out_tmsdev:
  140. pci_set_drvdata(pdev, NULL);
  141. tmsdev_term(dev);
  142. err_out_irq:
  143. free_irq(pdev->irq, dev);
  144. err_out_region:
  145. release_region(pci_ioaddr, ABYSS_IO_EXTENT);
  146. err_out_trdev:
  147. free_netdev(dev);
  148. return ret;
  149. }
  150. static unsigned short abyss_setnselout_pins(struct net_device *dev)
  151. {
  152. unsigned short val = 0;
  153. struct net_local *tp = netdev_priv(dev);
  154. if(tp->DataRate == SPEED_4)
  155. val |= 0x01; /* Set 4Mbps */
  156. else
  157. val |= 0x00; /* Set 16Mbps */
  158. return val;
  159. }
  160. /*
  161. * The following Madge boards should use this code:
  162. * - Smart 16/4 PCI Mk2 (Abyss)
  163. * - Smart 16/4 PCI Mk1 (PCI T)
  164. * - Smart 16/4 Client Plus PnP (Big Apple)
  165. * - Smart 16/4 Cardbus Mk2
  166. *
  167. * These access an Atmel AT24 SEEPROM using their glue chip registers.
  168. *
  169. */
  170. static void at24_writedatabyte(unsigned long regaddr, unsigned char byte)
  171. {
  172. int i;
  173. for (i = 0; i < 8; i++) {
  174. at24_setlines(regaddr, 0, (byte >> (7-i))&0x01);
  175. at24_setlines(regaddr, 1, (byte >> (7-i))&0x01);
  176. at24_setlines(regaddr, 0, (byte >> (7-i))&0x01);
  177. }
  178. }
  179. static int at24_sendfullcmd(unsigned long regaddr, unsigned char cmd, unsigned char addr)
  180. {
  181. if (at24_sendcmd(regaddr, cmd)) {
  182. at24_writedatabyte(regaddr, addr);
  183. return at24_waitforack(regaddr);
  184. }
  185. return 0;
  186. }
  187. static int at24_sendcmd(unsigned long regaddr, unsigned char cmd)
  188. {
  189. int i;
  190. for (i = 0; i < 10; i++) {
  191. at24_start(regaddr);
  192. at24_writedatabyte(regaddr, cmd);
  193. if (at24_waitforack(regaddr))
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. static unsigned char at24_readdatabit(unsigned long regaddr)
  199. {
  200. unsigned char val;
  201. at24_setlines(regaddr, 0, 1);
  202. at24_setlines(regaddr, 1, 1);
  203. val = (inb(regaddr) & AT24_DATA)?1:0;
  204. at24_setlines(regaddr, 1, 1);
  205. at24_setlines(regaddr, 0, 1);
  206. return val;
  207. }
  208. static unsigned char at24_readdatabyte(unsigned long regaddr)
  209. {
  210. unsigned char data = 0;
  211. int i;
  212. for (i = 0; i < 8; i++) {
  213. data <<= 1;
  214. data |= at24_readdatabit(regaddr);
  215. }
  216. return data;
  217. }
  218. static int at24_waitforack(unsigned long regaddr)
  219. {
  220. int i;
  221. for (i = 0; i < 10; i++) {
  222. if ((at24_readdatabit(regaddr) & 0x01) == 0x00)
  223. return 1;
  224. }
  225. return 0;
  226. }
  227. static int at24_waitfornack(unsigned long regaddr)
  228. {
  229. int i;
  230. for (i = 0; i < 10; i++) {
  231. if ((at24_readdatabit(regaddr) & 0x01) == 0x01)
  232. return 1;
  233. }
  234. return 0;
  235. }
  236. static void at24_setlines(unsigned long regaddr, unsigned char clock, unsigned char data)
  237. {
  238. unsigned char val = AT24_ENABLE;
  239. if (clock)
  240. val |= AT24_CLOCK;
  241. if (data)
  242. val |= AT24_DATA;
  243. outb(val, regaddr);
  244. tms380tr_wait(20); /* Very necessary. */
  245. }
  246. static void at24_start(unsigned long regaddr)
  247. {
  248. at24_setlines(regaddr, 0, 1);
  249. at24_setlines(regaddr, 1, 1);
  250. at24_setlines(regaddr, 1, 0);
  251. at24_setlines(regaddr, 0, 1);
  252. }
  253. static unsigned char at24_readb(unsigned long regaddr, unsigned char addr)
  254. {
  255. unsigned char data = 0xff;
  256. if (at24_sendfullcmd(regaddr, AT24_WRITE, addr)) {
  257. if (at24_sendcmd(regaddr, AT24_READ)) {
  258. data = at24_readdatabyte(regaddr);
  259. if (!at24_waitfornack(regaddr))
  260. data = 0xff;
  261. }
  262. }
  263. return data;
  264. }
  265. /*
  266. * Enable basic functions of the Madge chipset needed
  267. * for initialization.
  268. */
  269. static void abyss_enable(struct net_device *dev)
  270. {
  271. unsigned char reset_reg;
  272. unsigned long ioaddr;
  273. ioaddr = dev->base_addr;
  274. reset_reg = inb(ioaddr + PCIBM2_RESET_REG);
  275. reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
  276. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  277. tms380tr_wait(100);
  278. }
  279. /*
  280. * Enable the functions of the Madge chipset needed for
  281. * full working order.
  282. */
  283. static int abyss_chipset_init(struct net_device *dev)
  284. {
  285. unsigned char reset_reg;
  286. unsigned long ioaddr;
  287. ioaddr = dev->base_addr;
  288. reset_reg = inb(ioaddr + PCIBM2_RESET_REG);
  289. reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
  290. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  291. reset_reg &= ~(PCIBM2_RESET_REG_CHIP_NRES |
  292. PCIBM2_RESET_REG_FIFO_NRES |
  293. PCIBM2_RESET_REG_SIF_NRES);
  294. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  295. tms380tr_wait(100);
  296. reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
  297. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  298. reset_reg |= PCIBM2_RESET_REG_SIF_NRES;
  299. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  300. reset_reg |= PCIBM2_RESET_REG_FIFO_NRES;
  301. outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
  302. outb(PCIBM2_INT_CONTROL_REG_SINTEN |
  303. PCIBM2_INT_CONTROL_REG_PCI_ERR_ENABLE,
  304. ioaddr + PCIBM2_INT_CONTROL_REG);
  305. outb(30, ioaddr + PCIBM2_FIFO_THRESHOLD);
  306. return 0;
  307. }
  308. static inline void abyss_chipset_close(struct net_device *dev)
  309. {
  310. unsigned long ioaddr;
  311. ioaddr = dev->base_addr;
  312. outb(0, ioaddr + PCIBM2_RESET_REG);
  313. }
  314. /*
  315. * Read configuration data from the AT24 SEEPROM on Madge cards.
  316. *
  317. */
  318. static void abyss_read_eeprom(struct net_device *dev)
  319. {
  320. struct net_local *tp;
  321. unsigned long ioaddr;
  322. unsigned short val;
  323. int i;
  324. tp = netdev_priv(dev);
  325. ioaddr = dev->base_addr;
  326. /* Must enable glue chip first */
  327. abyss_enable(dev);
  328. val = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
  329. PCIBM2_SEEPROM_RING_SPEED);
  330. tp->DataRate = val?SPEED_4:SPEED_16; /* set open speed */
  331. printk("%s: SEEPROM: ring speed: %dMb/sec\n", dev->name, tp->DataRate);
  332. val = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
  333. PCIBM2_SEEPROM_RAM_SIZE) * 128;
  334. printk("%s: SEEPROM: adapter RAM: %dkb\n", dev->name, val);
  335. dev->addr_len = 6;
  336. for (i = 0; i < 6; i++)
  337. dev->dev_addr[i] = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
  338. PCIBM2_SEEPROM_BIA+i);
  339. }
  340. static int abyss_open(struct net_device *dev)
  341. {
  342. abyss_chipset_init(dev);
  343. tms380tr_open(dev);
  344. return 0;
  345. }
  346. static int abyss_close(struct net_device *dev)
  347. {
  348. tms380tr_close(dev);
  349. abyss_chipset_close(dev);
  350. return 0;
  351. }
  352. static void __devexit abyss_detach (struct pci_dev *pdev)
  353. {
  354. struct net_device *dev = pci_get_drvdata(pdev);
  355. BUG_ON(!dev);
  356. unregister_netdev(dev);
  357. release_region(dev->base_addr-0x10, ABYSS_IO_EXTENT);
  358. free_irq(dev->irq, dev);
  359. tmsdev_term(dev);
  360. free_netdev(dev);
  361. pci_set_drvdata(pdev, NULL);
  362. }
  363. static struct pci_driver abyss_driver = {
  364. .name = "abyss",
  365. .id_table = abyss_pci_tbl,
  366. .probe = abyss_attach,
  367. .remove = __devexit_p(abyss_detach),
  368. };
  369. static int __init abyss_init (void)
  370. {
  371. abyss_netdev_ops = tms380tr_netdev_ops;
  372. abyss_netdev_ops.ndo_open = abyss_open;
  373. abyss_netdev_ops.ndo_stop = abyss_close;
  374. return pci_register_driver(&abyss_driver);
  375. }
  376. static void __exit abyss_rmmod (void)
  377. {
  378. pci_unregister_driver (&abyss_driver);
  379. }
  380. module_init(abyss_init);
  381. module_exit(abyss_rmmod);