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