pci200syn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Goramo PCI200SYN synchronous serial card driver for Linux
  3. *
  4. * Copyright (C) 2002-2008 Krzysztof Halasa <khc@pm.waw.pl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation.
  9. *
  10. * For information see <http://www.kernel.org/pub/linux/utils/net/hdlc/>
  11. *
  12. * Sources of information:
  13. * Hitachi HD64572 SCA-II User's Manual
  14. * PLX Technology Inc. PCI9052 Data Book
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/capability.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/in.h>
  23. #include <linux/string.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/ioport.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/hdlc.h>
  30. #include <linux/pci.h>
  31. #include <linux/delay.h>
  32. #include <asm/io.h>
  33. #include "hd64572.h"
  34. #undef DEBUG_PKT
  35. #define DEBUG_RINGS
  36. #define PCI200SYN_PLX_SIZE 0x80 /* PLX control window size (128b) */
  37. #define PCI200SYN_SCA_SIZE 0x400 /* SCA window size (1Kb) */
  38. #define MAX_TX_BUFFERS 10
  39. static int pci_clock_freq = 33000000;
  40. #define CLOCK_BASE pci_clock_freq
  41. /*
  42. * PLX PCI9052 local configuration and shared runtime registers.
  43. * This structure can be used to access 9052 registers (memory mapped).
  44. */
  45. typedef struct {
  46. u32 loc_addr_range[4]; /* 00-0Ch : Local Address Ranges */
  47. u32 loc_rom_range; /* 10h : Local ROM Range */
  48. u32 loc_addr_base[4]; /* 14-20h : Local Address Base Addrs */
  49. u32 loc_rom_base; /* 24h : Local ROM Base */
  50. u32 loc_bus_descr[4]; /* 28-34h : Local Bus Descriptors */
  51. u32 rom_bus_descr; /* 38h : ROM Bus Descriptor */
  52. u32 cs_base[4]; /* 3C-48h : Chip Select Base Addrs */
  53. u32 intr_ctrl_stat; /* 4Ch : Interrupt Control/Status */
  54. u32 init_ctrl; /* 50h : EEPROM ctrl, Init Ctrl, etc */
  55. }plx9052;
  56. typedef struct port_s {
  57. struct napi_struct napi;
  58. struct net_device *netdev;
  59. struct card_s *card;
  60. spinlock_t lock; /* TX lock */
  61. sync_serial_settings settings;
  62. int rxpart; /* partial frame received, next frame invalid*/
  63. unsigned short encoding;
  64. unsigned short parity;
  65. u16 rxin; /* rx ring buffer 'in' pointer */
  66. u16 txin; /* tx ring buffer 'in' and 'last' pointers */
  67. u16 txlast;
  68. u8 rxs, txs, tmc; /* SCA registers */
  69. u8 chan; /* physical port # - 0 or 1 */
  70. }port_t;
  71. typedef struct card_s {
  72. u8 __iomem *rambase; /* buffer memory base (virtual) */
  73. u8 __iomem *scabase; /* SCA memory base (virtual) */
  74. plx9052 __iomem *plxbase;/* PLX registers memory base (virtual) */
  75. u16 rx_ring_buffers; /* number of buffers in a ring */
  76. u16 tx_ring_buffers;
  77. u16 buff_offset; /* offset of first buffer of first channel */
  78. u8 irq; /* interrupt request level */
  79. port_t ports[2];
  80. }card_t;
  81. #define get_port(card, port) (&card->ports[port])
  82. #define sca_flush(card) (sca_in(IER0, card));
  83. static inline void new_memcpy_toio(char __iomem *dest, char *src, int length)
  84. {
  85. int len;
  86. do {
  87. len = length > 256 ? 256 : length;
  88. memcpy_toio(dest, src, len);
  89. dest += len;
  90. src += len;
  91. length -= len;
  92. readb(dest);
  93. } while (len);
  94. }
  95. #undef memcpy_toio
  96. #define memcpy_toio new_memcpy_toio
  97. #include "hd64572.c"
  98. static void pci200_set_iface(port_t *port)
  99. {
  100. card_t *card = port->card;
  101. u16 msci = get_msci(port);
  102. u8 rxs = port->rxs & CLK_BRG_MASK;
  103. u8 txs = port->txs & CLK_BRG_MASK;
  104. sca_out(EXS_TES1, (port->chan ? MSCI1_OFFSET : MSCI0_OFFSET) + EXS,
  105. port->card);
  106. switch(port->settings.clock_type) {
  107. case CLOCK_INT:
  108. rxs |= CLK_BRG; /* BRG output */
  109. txs |= CLK_PIN_OUT | CLK_TX_RXCLK; /* RX clock */
  110. break;
  111. case CLOCK_TXINT:
  112. rxs |= CLK_LINE; /* RXC input */
  113. txs |= CLK_PIN_OUT | CLK_BRG; /* BRG output */
  114. break;
  115. case CLOCK_TXFROMRX:
  116. rxs |= CLK_LINE; /* RXC input */
  117. txs |= CLK_PIN_OUT | CLK_TX_RXCLK; /* RX clock */
  118. break;
  119. default: /* EXTernal clock */
  120. rxs |= CLK_LINE; /* RXC input */
  121. txs |= CLK_PIN_OUT | CLK_LINE; /* TXC input */
  122. break;
  123. }
  124. port->rxs = rxs;
  125. port->txs = txs;
  126. sca_out(rxs, msci + RXS, card);
  127. sca_out(txs, msci + TXS, card);
  128. sca_set_port(port);
  129. }
  130. static int pci200_open(struct net_device *dev)
  131. {
  132. port_t *port = dev_to_port(dev);
  133. int result = hdlc_open(dev);
  134. if (result)
  135. return result;
  136. sca_open(dev);
  137. pci200_set_iface(port);
  138. sca_flush(port->card);
  139. return 0;
  140. }
  141. static int pci200_close(struct net_device *dev)
  142. {
  143. sca_close(dev);
  144. sca_flush(dev_to_port(dev)->card);
  145. hdlc_close(dev);
  146. return 0;
  147. }
  148. static int pci200_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  149. {
  150. const size_t size = sizeof(sync_serial_settings);
  151. sync_serial_settings new_line;
  152. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  153. port_t *port = dev_to_port(dev);
  154. #ifdef DEBUG_RINGS
  155. if (cmd == SIOCDEVPRIVATE) {
  156. sca_dump_rings(dev);
  157. return 0;
  158. }
  159. #endif
  160. if (cmd != SIOCWANDEV)
  161. return hdlc_ioctl(dev, ifr, cmd);
  162. switch(ifr->ifr_settings.type) {
  163. case IF_GET_IFACE:
  164. ifr->ifr_settings.type = IF_IFACE_V35;
  165. if (ifr->ifr_settings.size < size) {
  166. ifr->ifr_settings.size = size; /* data size wanted */
  167. return -ENOBUFS;
  168. }
  169. if (copy_to_user(line, &port->settings, size))
  170. return -EFAULT;
  171. return 0;
  172. case IF_IFACE_V35:
  173. case IF_IFACE_SYNC_SERIAL:
  174. if (!capable(CAP_NET_ADMIN))
  175. return -EPERM;
  176. if (copy_from_user(&new_line, line, size))
  177. return -EFAULT;
  178. if (new_line.clock_type != CLOCK_EXT &&
  179. new_line.clock_type != CLOCK_TXFROMRX &&
  180. new_line.clock_type != CLOCK_INT &&
  181. new_line.clock_type != CLOCK_TXINT)
  182. return -EINVAL; /* No such clock setting */
  183. if (new_line.loopback != 0 && new_line.loopback != 1)
  184. return -EINVAL;
  185. memcpy(&port->settings, &new_line, size); /* Update settings */
  186. pci200_set_iface(port);
  187. sca_flush(port->card);
  188. return 0;
  189. default:
  190. return hdlc_ioctl(dev, ifr, cmd);
  191. }
  192. }
  193. static void pci200_pci_remove_one(struct pci_dev *pdev)
  194. {
  195. int i;
  196. card_t *card = pci_get_drvdata(pdev);
  197. for (i = 0; i < 2; i++)
  198. if (card->ports[i].card)
  199. unregister_hdlc_device(card->ports[i].netdev);
  200. if (card->irq)
  201. free_irq(card->irq, card);
  202. if (card->rambase)
  203. iounmap(card->rambase);
  204. if (card->scabase)
  205. iounmap(card->scabase);
  206. if (card->plxbase)
  207. iounmap(card->plxbase);
  208. pci_release_regions(pdev);
  209. pci_disable_device(pdev);
  210. pci_set_drvdata(pdev, NULL);
  211. if (card->ports[0].netdev)
  212. free_netdev(card->ports[0].netdev);
  213. if (card->ports[1].netdev)
  214. free_netdev(card->ports[1].netdev);
  215. kfree(card);
  216. }
  217. static const struct net_device_ops pci200_ops = {
  218. .ndo_open = pci200_open,
  219. .ndo_stop = pci200_close,
  220. .ndo_change_mtu = hdlc_change_mtu,
  221. .ndo_start_xmit = hdlc_start_xmit,
  222. .ndo_do_ioctl = pci200_ioctl,
  223. };
  224. static int __devinit pci200_pci_init_one(struct pci_dev *pdev,
  225. const struct pci_device_id *ent)
  226. {
  227. card_t *card;
  228. u32 __iomem *p;
  229. int i;
  230. u32 ramsize;
  231. u32 ramphys; /* buffer memory base */
  232. u32 scaphys; /* SCA memory base */
  233. u32 plxphys; /* PLX registers memory base */
  234. i = pci_enable_device(pdev);
  235. if (i)
  236. return i;
  237. i = pci_request_regions(pdev, "PCI200SYN");
  238. if (i) {
  239. pci_disable_device(pdev);
  240. return i;
  241. }
  242. card = kzalloc(sizeof(card_t), GFP_KERNEL);
  243. if (card == NULL) {
  244. printk(KERN_ERR "pci200syn: unable to allocate memory\n");
  245. pci_release_regions(pdev);
  246. pci_disable_device(pdev);
  247. return -ENOBUFS;
  248. }
  249. pci_set_drvdata(pdev, card);
  250. card->ports[0].netdev = alloc_hdlcdev(&card->ports[0]);
  251. card->ports[1].netdev = alloc_hdlcdev(&card->ports[1]);
  252. if (!card->ports[0].netdev || !card->ports[1].netdev) {
  253. printk(KERN_ERR "pci200syn: unable to allocate memory\n");
  254. pci200_pci_remove_one(pdev);
  255. return -ENOMEM;
  256. }
  257. if (pci_resource_len(pdev, 0) != PCI200SYN_PLX_SIZE ||
  258. pci_resource_len(pdev, 2) != PCI200SYN_SCA_SIZE ||
  259. pci_resource_len(pdev, 3) < 16384) {
  260. printk(KERN_ERR "pci200syn: invalid card EEPROM parameters\n");
  261. pci200_pci_remove_one(pdev);
  262. return -EFAULT;
  263. }
  264. plxphys = pci_resource_start(pdev,0) & PCI_BASE_ADDRESS_MEM_MASK;
  265. card->plxbase = ioremap(plxphys, PCI200SYN_PLX_SIZE);
  266. scaphys = pci_resource_start(pdev,2) & PCI_BASE_ADDRESS_MEM_MASK;
  267. card->scabase = ioremap(scaphys, PCI200SYN_SCA_SIZE);
  268. ramphys = pci_resource_start(pdev,3) & PCI_BASE_ADDRESS_MEM_MASK;
  269. card->rambase = pci_ioremap_bar(pdev, 3);
  270. if (card->plxbase == NULL ||
  271. card->scabase == NULL ||
  272. card->rambase == NULL) {
  273. printk(KERN_ERR "pci200syn: ioremap() failed\n");
  274. pci200_pci_remove_one(pdev);
  275. return -EFAULT;
  276. }
  277. /* Reset PLX */
  278. p = &card->plxbase->init_ctrl;
  279. writel(readl(p) | 0x40000000, p);
  280. readl(p); /* Flush the write - do not use sca_flush */
  281. udelay(1);
  282. writel(readl(p) & ~0x40000000, p);
  283. readl(p); /* Flush the write - do not use sca_flush */
  284. udelay(1);
  285. ramsize = sca_detect_ram(card, card->rambase,
  286. pci_resource_len(pdev, 3));
  287. /* number of TX + RX buffers for one port - this is dual port card */
  288. i = ramsize / (2 * (sizeof(pkt_desc) + HDLC_MAX_MRU));
  289. card->tx_ring_buffers = min(i / 2, MAX_TX_BUFFERS);
  290. card->rx_ring_buffers = i - card->tx_ring_buffers;
  291. card->buff_offset = 2 * sizeof(pkt_desc) * (card->tx_ring_buffers +
  292. card->rx_ring_buffers);
  293. printk(KERN_INFO "pci200syn: %u KB RAM at 0x%x, IRQ%u, using %u TX +"
  294. " %u RX packets rings\n", ramsize / 1024, ramphys,
  295. pdev->irq, card->tx_ring_buffers, card->rx_ring_buffers);
  296. if (card->tx_ring_buffers < 1) {
  297. printk(KERN_ERR "pci200syn: RAM test failed\n");
  298. pci200_pci_remove_one(pdev);
  299. return -EFAULT;
  300. }
  301. /* Enable interrupts on the PCI bridge */
  302. p = &card->plxbase->intr_ctrl_stat;
  303. writew(readw(p) | 0x0040, p);
  304. /* Allocate IRQ */
  305. if (request_irq(pdev->irq, sca_intr, IRQF_SHARED, "pci200syn", card)) {
  306. printk(KERN_WARNING "pci200syn: could not allocate IRQ%d.\n",
  307. pdev->irq);
  308. pci200_pci_remove_one(pdev);
  309. return -EBUSY;
  310. }
  311. card->irq = pdev->irq;
  312. sca_init(card, 0);
  313. for (i = 0; i < 2; i++) {
  314. port_t *port = &card->ports[i];
  315. struct net_device *dev = port->netdev;
  316. hdlc_device *hdlc = dev_to_hdlc(dev);
  317. port->chan = i;
  318. spin_lock_init(&port->lock);
  319. dev->irq = card->irq;
  320. dev->mem_start = ramphys;
  321. dev->mem_end = ramphys + ramsize - 1;
  322. dev->tx_queue_len = 50;
  323. dev->netdev_ops = &pci200_ops;
  324. hdlc->attach = sca_attach;
  325. hdlc->xmit = sca_xmit;
  326. port->settings.clock_type = CLOCK_EXT;
  327. port->card = card;
  328. sca_init_port(port);
  329. if (register_hdlc_device(dev)) {
  330. printk(KERN_ERR "pci200syn: unable to register hdlc "
  331. "device\n");
  332. port->card = NULL;
  333. pci200_pci_remove_one(pdev);
  334. return -ENOBUFS;
  335. }
  336. printk(KERN_INFO "%s: PCI200SYN channel %d\n",
  337. dev->name, port->chan);
  338. }
  339. sca_flush(card);
  340. return 0;
  341. }
  342. static DEFINE_PCI_DEVICE_TABLE(pci200_pci_tbl) = {
  343. { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_PLX,
  344. PCI_DEVICE_ID_PLX_PCI200SYN, 0, 0, 0 },
  345. { 0, }
  346. };
  347. static struct pci_driver pci200_pci_driver = {
  348. .name = "PCI200SYN",
  349. .id_table = pci200_pci_tbl,
  350. .probe = pci200_pci_init_one,
  351. .remove = pci200_pci_remove_one,
  352. };
  353. static int __init pci200_init_module(void)
  354. {
  355. if (pci_clock_freq < 1000000 || pci_clock_freq > 80000000) {
  356. printk(KERN_ERR "pci200syn: Invalid PCI clock frequency\n");
  357. return -EINVAL;
  358. }
  359. return pci_register_driver(&pci200_pci_driver);
  360. }
  361. static void __exit pci200_cleanup_module(void)
  362. {
  363. pci_unregister_driver(&pci200_pci_driver);
  364. }
  365. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  366. MODULE_DESCRIPTION("Goramo PCI200SYN serial port driver");
  367. MODULE_LICENSE("GPL v2");
  368. MODULE_DEVICE_TABLE(pci, pci200_pci_tbl);
  369. module_param(pci_clock_freq, int, 0444);
  370. MODULE_PARM_DESC(pci_clock_freq, "System PCI clock frequency in Hz");
  371. module_init(pci200_init_module);
  372. module_exit(pci200_cleanup_module);