pc300too.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Cyclades PC300 synchronous serial card driver for Linux
  3. *
  4. * Copyright (C) 2000-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. * Original Cyclades PC300 Linux driver
  15. *
  16. * This driver currently supports only PC300/RSV (V.24/V.35) and
  17. * PC300/X21 cards.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/sched.h>
  23. #include <linux/types.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/in.h>
  26. #include <linux/string.h>
  27. #include <linux/errno.h>
  28. #include <linux/init.h>
  29. #include <linux/ioport.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/hdlc.h>
  33. #include <linux/pci.h>
  34. #include <linux/delay.h>
  35. #include <asm/io.h>
  36. #include "hd64572.h"
  37. #undef DEBUG_PKT
  38. #define DEBUG_RINGS
  39. #define PC300_PLX_SIZE 0x80 /* PLX control window size (128 B) */
  40. #define PC300_SCA_SIZE 0x400 /* SCA window size (1 KB) */
  41. #define MAX_TX_BUFFERS 10
  42. static int pci_clock_freq = 33000000;
  43. static int use_crystal_clock = 0;
  44. static unsigned int CLOCK_BASE;
  45. /* Masks to access the init_ctrl PLX register */
  46. #define PC300_CLKSEL_MASK (0x00000004UL)
  47. #define PC300_CHMEDIA_MASK(port) (0x00000020UL << ((port) * 3))
  48. #define PC300_CTYPE_MASK (0x00000800UL)
  49. enum { PC300_RSV = 1, PC300_X21, PC300_TE }; /* card types */
  50. /*
  51. * PLX PCI9050-1 local configuration and shared runtime registers.
  52. * This structure can be used to access 9050 registers (memory mapped).
  53. */
  54. typedef struct {
  55. u32 loc_addr_range[4]; /* 00-0Ch : Local Address Ranges */
  56. u32 loc_rom_range; /* 10h : Local ROM Range */
  57. u32 loc_addr_base[4]; /* 14-20h : Local Address Base Addrs */
  58. u32 loc_rom_base; /* 24h : Local ROM Base */
  59. u32 loc_bus_descr[4]; /* 28-34h : Local Bus Descriptors */
  60. u32 rom_bus_descr; /* 38h : ROM Bus Descriptor */
  61. u32 cs_base[4]; /* 3C-48h : Chip Select Base Addrs */
  62. u32 intr_ctrl_stat; /* 4Ch : Interrupt Control/Status */
  63. u32 init_ctrl; /* 50h : EEPROM ctrl, Init Ctrl, etc */
  64. }plx9050;
  65. typedef struct port_s {
  66. struct napi_struct napi;
  67. struct net_device *netdev;
  68. struct card_s *card;
  69. spinlock_t lock; /* TX lock */
  70. sync_serial_settings settings;
  71. int rxpart; /* partial frame received, next frame invalid*/
  72. unsigned short encoding;
  73. unsigned short parity;
  74. unsigned int iface;
  75. u16 rxin; /* rx ring buffer 'in' pointer */
  76. u16 txin; /* tx ring buffer 'in' and 'last' pointers */
  77. u16 txlast;
  78. u8 rxs, txs, tmc; /* SCA registers */
  79. u8 chan; /* physical port # - 0 or 1 */
  80. }port_t;
  81. typedef struct card_s {
  82. int type; /* RSV, X21, etc. */
  83. int n_ports; /* 1 or 2 ports */
  84. u8 __iomem *rambase; /* buffer memory base (virtual) */
  85. u8 __iomem *scabase; /* SCA memory base (virtual) */
  86. plx9050 __iomem *plxbase; /* PLX registers memory base (virtual) */
  87. u32 init_ctrl_value; /* Saved value - 9050 bug workaround */
  88. u16 rx_ring_buffers; /* number of buffers in a ring */
  89. u16 tx_ring_buffers;
  90. u16 buff_offset; /* offset of first buffer of first channel */
  91. u8 irq; /* interrupt request level */
  92. port_t ports[2];
  93. }card_t;
  94. #define get_port(card, port) ((port) < (card)->n_ports ? \
  95. (&(card)->ports[port]) : (NULL))
  96. #include "hd64572.c"
  97. static void pc300_set_iface(port_t *port)
  98. {
  99. card_t *card = port->card;
  100. u32 __iomem * init_ctrl = &card->plxbase->init_ctrl;
  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. if (port->card->type == PC300_RSV) {
  130. if (port->iface == IF_IFACE_V35)
  131. writel(card->init_ctrl_value |
  132. PC300_CHMEDIA_MASK(port->chan), init_ctrl);
  133. else
  134. writel(card->init_ctrl_value &
  135. ~PC300_CHMEDIA_MASK(port->chan), init_ctrl);
  136. }
  137. }
  138. static int pc300_open(struct net_device *dev)
  139. {
  140. port_t *port = dev_to_port(dev);
  141. int result = hdlc_open(dev);
  142. if (result)
  143. return result;
  144. sca_open(dev);
  145. pc300_set_iface(port);
  146. return 0;
  147. }
  148. static int pc300_close(struct net_device *dev)
  149. {
  150. sca_close(dev);
  151. hdlc_close(dev);
  152. return 0;
  153. }
  154. static int pc300_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  155. {
  156. const size_t size = sizeof(sync_serial_settings);
  157. sync_serial_settings new_line;
  158. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  159. int new_type;
  160. port_t *port = dev_to_port(dev);
  161. #ifdef DEBUG_RINGS
  162. if (cmd == SIOCDEVPRIVATE) {
  163. sca_dump_rings(dev);
  164. return 0;
  165. }
  166. #endif
  167. if (cmd != SIOCWANDEV)
  168. return hdlc_ioctl(dev, ifr, cmd);
  169. if (ifr->ifr_settings.type == IF_GET_IFACE) {
  170. ifr->ifr_settings.type = port->iface;
  171. if (ifr->ifr_settings.size < size) {
  172. ifr->ifr_settings.size = size; /* data size wanted */
  173. return -ENOBUFS;
  174. }
  175. if (copy_to_user(line, &port->settings, size))
  176. return -EFAULT;
  177. return 0;
  178. }
  179. if (port->card->type == PC300_X21 &&
  180. (ifr->ifr_settings.type == IF_IFACE_SYNC_SERIAL ||
  181. ifr->ifr_settings.type == IF_IFACE_X21))
  182. new_type = IF_IFACE_X21;
  183. else if (port->card->type == PC300_RSV &&
  184. (ifr->ifr_settings.type == IF_IFACE_SYNC_SERIAL ||
  185. ifr->ifr_settings.type == IF_IFACE_V35))
  186. new_type = IF_IFACE_V35;
  187. else if (port->card->type == PC300_RSV &&
  188. ifr->ifr_settings.type == IF_IFACE_V24)
  189. new_type = IF_IFACE_V24;
  190. else
  191. return hdlc_ioctl(dev, ifr, cmd);
  192. if (!capable(CAP_NET_ADMIN))
  193. return -EPERM;
  194. if (copy_from_user(&new_line, line, size))
  195. return -EFAULT;
  196. if (new_line.clock_type != CLOCK_EXT &&
  197. new_line.clock_type != CLOCK_TXFROMRX &&
  198. new_line.clock_type != CLOCK_INT &&
  199. new_line.clock_type != CLOCK_TXINT)
  200. return -EINVAL; /* No such clock setting */
  201. if (new_line.loopback != 0 && new_line.loopback != 1)
  202. return -EINVAL;
  203. memcpy(&port->settings, &new_line, size); /* Update settings */
  204. port->iface = new_type;
  205. pc300_set_iface(port);
  206. return 0;
  207. }
  208. static void pc300_pci_remove_one(struct pci_dev *pdev)
  209. {
  210. int i;
  211. card_t *card = pci_get_drvdata(pdev);
  212. for (i = 0; i < 2; i++)
  213. if (card->ports[i].card)
  214. unregister_hdlc_device(card->ports[i].netdev);
  215. if (card->irq)
  216. free_irq(card->irq, card);
  217. if (card->rambase)
  218. iounmap(card->rambase);
  219. if (card->scabase)
  220. iounmap(card->scabase);
  221. if (card->plxbase)
  222. iounmap(card->plxbase);
  223. pci_release_regions(pdev);
  224. pci_disable_device(pdev);
  225. pci_set_drvdata(pdev, NULL);
  226. if (card->ports[0].netdev)
  227. free_netdev(card->ports[0].netdev);
  228. if (card->ports[1].netdev)
  229. free_netdev(card->ports[1].netdev);
  230. kfree(card);
  231. }
  232. static const struct net_device_ops pc300_ops = {
  233. .ndo_open = pc300_open,
  234. .ndo_stop = pc300_close,
  235. .ndo_change_mtu = hdlc_change_mtu,
  236. .ndo_start_xmit = hdlc_start_xmit,
  237. .ndo_do_ioctl = pc300_ioctl,
  238. };
  239. static int __devinit pc300_pci_init_one(struct pci_dev *pdev,
  240. const struct pci_device_id *ent)
  241. {
  242. card_t *card;
  243. u32 __iomem *p;
  244. int i;
  245. u32 ramsize;
  246. u32 ramphys; /* buffer memory base */
  247. u32 scaphys; /* SCA memory base */
  248. u32 plxphys; /* PLX registers memory base */
  249. i = pci_enable_device(pdev);
  250. if (i)
  251. return i;
  252. i = pci_request_regions(pdev, "PC300");
  253. if (i) {
  254. pci_disable_device(pdev);
  255. return i;
  256. }
  257. card = kzalloc(sizeof(card_t), GFP_KERNEL);
  258. if (card == NULL) {
  259. printk(KERN_ERR "pc300: unable to allocate memory\n");
  260. pci_release_regions(pdev);
  261. pci_disable_device(pdev);
  262. return -ENOBUFS;
  263. }
  264. pci_set_drvdata(pdev, card);
  265. if (pci_resource_len(pdev, 0) != PC300_PLX_SIZE ||
  266. pci_resource_len(pdev, 2) != PC300_SCA_SIZE ||
  267. pci_resource_len(pdev, 3) < 16384) {
  268. printk(KERN_ERR "pc300: invalid card EEPROM parameters\n");
  269. pc300_pci_remove_one(pdev);
  270. return -EFAULT;
  271. }
  272. plxphys = pci_resource_start(pdev, 0) & PCI_BASE_ADDRESS_MEM_MASK;
  273. card->plxbase = ioremap(plxphys, PC300_PLX_SIZE);
  274. scaphys = pci_resource_start(pdev, 2) & PCI_BASE_ADDRESS_MEM_MASK;
  275. card->scabase = ioremap(scaphys, PC300_SCA_SIZE);
  276. ramphys = pci_resource_start(pdev, 3) & PCI_BASE_ADDRESS_MEM_MASK;
  277. card->rambase = pci_ioremap_bar(pdev, 3);
  278. if (card->plxbase == NULL ||
  279. card->scabase == NULL ||
  280. card->rambase == NULL) {
  281. printk(KERN_ERR "pc300: ioremap() failed\n");
  282. pc300_pci_remove_one(pdev);
  283. }
  284. /* PLX PCI 9050 workaround for local configuration register read bug */
  285. pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, scaphys);
  286. card->init_ctrl_value = readl(&((plx9050 __iomem *)card->scabase)->init_ctrl);
  287. pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, plxphys);
  288. if (pdev->device == PCI_DEVICE_ID_PC300_TE_1 ||
  289. pdev->device == PCI_DEVICE_ID_PC300_TE_2)
  290. card->type = PC300_TE; /* not fully supported */
  291. else if (card->init_ctrl_value & PC300_CTYPE_MASK)
  292. card->type = PC300_X21;
  293. else
  294. card->type = PC300_RSV;
  295. if (pdev->device == PCI_DEVICE_ID_PC300_RX_1 ||
  296. pdev->device == PCI_DEVICE_ID_PC300_TE_1)
  297. card->n_ports = 1;
  298. else
  299. card->n_ports = 2;
  300. for (i = 0; i < card->n_ports; i++)
  301. if (!(card->ports[i].netdev = alloc_hdlcdev(&card->ports[i]))) {
  302. printk(KERN_ERR "pc300: unable to allocate memory\n");
  303. pc300_pci_remove_one(pdev);
  304. return -ENOMEM;
  305. }
  306. /* Reset PLX */
  307. p = &card->plxbase->init_ctrl;
  308. writel(card->init_ctrl_value | 0x40000000, p);
  309. readl(p); /* Flush the write - do not use sca_flush */
  310. udelay(1);
  311. writel(card->init_ctrl_value, p);
  312. readl(p); /* Flush the write - do not use sca_flush */
  313. udelay(1);
  314. /* Reload Config. Registers from EEPROM */
  315. writel(card->init_ctrl_value | 0x20000000, p);
  316. readl(p); /* Flush the write - do not use sca_flush */
  317. udelay(1);
  318. writel(card->init_ctrl_value, p);
  319. readl(p); /* Flush the write - do not use sca_flush */
  320. udelay(1);
  321. ramsize = sca_detect_ram(card, card->rambase,
  322. pci_resource_len(pdev, 3));
  323. if (use_crystal_clock)
  324. card->init_ctrl_value &= ~PC300_CLKSEL_MASK;
  325. else
  326. card->init_ctrl_value |= PC300_CLKSEL_MASK;
  327. writel(card->init_ctrl_value, &card->plxbase->init_ctrl);
  328. /* number of TX + RX buffers for one port */
  329. i = ramsize / (card->n_ports * (sizeof(pkt_desc) + HDLC_MAX_MRU));
  330. card->tx_ring_buffers = min(i / 2, MAX_TX_BUFFERS);
  331. card->rx_ring_buffers = i - card->tx_ring_buffers;
  332. card->buff_offset = card->n_ports * sizeof(pkt_desc) *
  333. (card->tx_ring_buffers + card->rx_ring_buffers);
  334. printk(KERN_INFO "pc300: PC300/%s, %u KB RAM at 0x%x, IRQ%u, "
  335. "using %u TX + %u RX packets rings\n",
  336. card->type == PC300_X21 ? "X21" :
  337. card->type == PC300_TE ? "TE" : "RSV",
  338. ramsize / 1024, ramphys, pdev->irq,
  339. card->tx_ring_buffers, card->rx_ring_buffers);
  340. if (card->tx_ring_buffers < 1) {
  341. printk(KERN_ERR "pc300: RAM test failed\n");
  342. pc300_pci_remove_one(pdev);
  343. return -EFAULT;
  344. }
  345. /* Enable interrupts on the PCI bridge, LINTi1 active low */
  346. writew(0x0041, &card->plxbase->intr_ctrl_stat);
  347. /* Allocate IRQ */
  348. if (request_irq(pdev->irq, sca_intr, IRQF_SHARED, "pc300", card)) {
  349. printk(KERN_WARNING "pc300: could not allocate IRQ%d.\n",
  350. pdev->irq);
  351. pc300_pci_remove_one(pdev);
  352. return -EBUSY;
  353. }
  354. card->irq = pdev->irq;
  355. sca_init(card, 0);
  356. // COTE not set - allows better TX DMA settings
  357. // sca_out(sca_in(PCR, card) | PCR_COTE, PCR, card);
  358. sca_out(0x10, BTCR, card);
  359. for (i = 0; i < card->n_ports; i++) {
  360. port_t *port = &card->ports[i];
  361. struct net_device *dev = port->netdev;
  362. hdlc_device *hdlc = dev_to_hdlc(dev);
  363. port->chan = i;
  364. spin_lock_init(&port->lock);
  365. dev->irq = card->irq;
  366. dev->mem_start = ramphys;
  367. dev->mem_end = ramphys + ramsize - 1;
  368. dev->tx_queue_len = 50;
  369. dev->netdev_ops = &pc300_ops;
  370. hdlc->attach = sca_attach;
  371. hdlc->xmit = sca_xmit;
  372. port->settings.clock_type = CLOCK_EXT;
  373. port->card = card;
  374. if (card->type == PC300_X21)
  375. port->iface = IF_IFACE_X21;
  376. else
  377. port->iface = IF_IFACE_V35;
  378. sca_init_port(port);
  379. if (register_hdlc_device(dev)) {
  380. printk(KERN_ERR "pc300: unable to register hdlc "
  381. "device\n");
  382. port->card = NULL;
  383. pc300_pci_remove_one(pdev);
  384. return -ENOBUFS;
  385. }
  386. printk(KERN_INFO "%s: PC300 channel %d\n",
  387. dev->name, port->chan);
  388. }
  389. return 0;
  390. }
  391. static DEFINE_PCI_DEVICE_TABLE(pc300_pci_tbl) = {
  392. { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_RX_1, PCI_ANY_ID,
  393. PCI_ANY_ID, 0, 0, 0 },
  394. { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_RX_2, PCI_ANY_ID,
  395. PCI_ANY_ID, 0, 0, 0 },
  396. { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_TE_1, PCI_ANY_ID,
  397. PCI_ANY_ID, 0, 0, 0 },
  398. { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_TE_2, PCI_ANY_ID,
  399. PCI_ANY_ID, 0, 0, 0 },
  400. { 0, }
  401. };
  402. static struct pci_driver pc300_pci_driver = {
  403. .name = "PC300",
  404. .id_table = pc300_pci_tbl,
  405. .probe = pc300_pci_init_one,
  406. .remove = pc300_pci_remove_one,
  407. };
  408. static int __init pc300_init_module(void)
  409. {
  410. if (pci_clock_freq < 1000000 || pci_clock_freq > 80000000) {
  411. printk(KERN_ERR "pc300: Invalid PCI clock frequency\n");
  412. return -EINVAL;
  413. }
  414. if (use_crystal_clock != 0 && use_crystal_clock != 1) {
  415. printk(KERN_ERR "pc300: Invalid 'use_crystal_clock' value\n");
  416. return -EINVAL;
  417. }
  418. CLOCK_BASE = use_crystal_clock ? 24576000 : pci_clock_freq;
  419. return pci_register_driver(&pc300_pci_driver);
  420. }
  421. static void __exit pc300_cleanup_module(void)
  422. {
  423. pci_unregister_driver(&pc300_pci_driver);
  424. }
  425. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  426. MODULE_DESCRIPTION("Cyclades PC300 serial port driver");
  427. MODULE_LICENSE("GPL v2");
  428. MODULE_DEVICE_TABLE(pci, pc300_pci_tbl);
  429. module_param(pci_clock_freq, int, 0444);
  430. MODULE_PARM_DESC(pci_clock_freq, "System PCI clock frequency in Hz");
  431. module_param(use_crystal_clock, int, 0444);
  432. MODULE_PARM_DESC(use_crystal_clock,
  433. "Use 24.576 MHz clock instead of PCI clock");
  434. module_init(pc300_init_module);
  435. module_exit(pc300_cleanup_module);