pc300too.c 14 KB

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