n2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * SDL Inc. RISCom/N2 synchronous serial card driver for Linux
  3. *
  4. * Copyright (C) 1998-2003 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. * Note: integrated CSU/DSU/DDS are not supported by this driver
  13. *
  14. * Sources of information:
  15. * Hitachi HD64570 SCA User's Manual
  16. * SDL Inc. PPP/HDLC/CISCO driver
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/capability.h>
  22. #include <linux/slab.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 <asm/io.h>
  34. #include "hd64570.h"
  35. static const char* version = "SDL RISCom/N2 driver version: 1.15";
  36. static const char* devname = "RISCom/N2";
  37. #undef DEBUG_PKT
  38. #define DEBUG_RINGS
  39. #define USE_WINDOWSIZE 16384
  40. #define USE_BUS16BITS 1
  41. #define CLOCK_BASE 9830400 /* 9.8304 MHz */
  42. #define MAX_PAGES 16 /* 16 RAM pages at max */
  43. #define MAX_RAM_SIZE 0x80000 /* 512 KB */
  44. #if MAX_RAM_SIZE > MAX_PAGES * USE_WINDOWSIZE
  45. #undef MAX_RAM_SIZE
  46. #define MAX_RAM_SIZE (MAX_PAGES * USE_WINDOWSIZE)
  47. #endif
  48. #define N2_IOPORTS 0x10
  49. #define NEED_DETECT_RAM
  50. #define NEED_SCA_MSCI_INTR
  51. #define MAX_TX_BUFFERS 10
  52. static char *hw; /* pointer to hw=xxx command line string */
  53. /* RISCom/N2 Board Registers */
  54. /* PC Control Register */
  55. #define N2_PCR 0
  56. #define PCR_RUNSCA 1 /* Run 64570 */
  57. #define PCR_VPM 2 /* Enable VPM - needed if using RAM above 1 MB */
  58. #define PCR_ENWIN 4 /* Open window */
  59. #define PCR_BUS16 8 /* 16-bit bus */
  60. /* Memory Base Address Register */
  61. #define N2_BAR 2
  62. /* Page Scan Register */
  63. #define N2_PSR 4
  64. #define WIN16K 0x00
  65. #define WIN32K 0x20
  66. #define WIN64K 0x40
  67. #define PSR_WINBITS 0x60
  68. #define PSR_DMAEN 0x80
  69. #define PSR_PAGEBITS 0x0F
  70. /* Modem Control Reg */
  71. #define N2_MCR 6
  72. #define CLOCK_OUT_PORT1 0x80
  73. #define CLOCK_OUT_PORT0 0x40
  74. #define TX422_PORT1 0x20
  75. #define TX422_PORT0 0x10
  76. #define DSR_PORT1 0x08
  77. #define DSR_PORT0 0x04
  78. #define DTR_PORT1 0x02
  79. #define DTR_PORT0 0x01
  80. typedef struct port_s {
  81. struct net_device *dev;
  82. struct card_s *card;
  83. spinlock_t lock; /* TX lock */
  84. sync_serial_settings settings;
  85. int valid; /* port enabled */
  86. int rxpart; /* partial frame received, next frame invalid*/
  87. unsigned short encoding;
  88. unsigned short parity;
  89. u16 rxin; /* rx ring buffer 'in' pointer */
  90. u16 txin; /* tx ring buffer 'in' and 'last' pointers */
  91. u16 txlast;
  92. u8 rxs, txs, tmc; /* SCA registers */
  93. u8 phy_node; /* physical port # - 0 or 1 */
  94. u8 log_node; /* logical port # */
  95. }port_t;
  96. typedef struct card_s {
  97. u8 __iomem *winbase; /* ISA window base address */
  98. u32 phy_winbase; /* ISA physical base address */
  99. u32 ram_size; /* number of bytes */
  100. u16 io; /* IO Base address */
  101. u16 buff_offset; /* offset of first buffer of first channel */
  102. u16 rx_ring_buffers; /* number of buffers in a ring */
  103. u16 tx_ring_buffers;
  104. u8 irq; /* IRQ (3-15) */
  105. port_t ports[2];
  106. struct card_s *next_card;
  107. }card_t;
  108. static card_t *first_card;
  109. static card_t **new_card = &first_card;
  110. #define sca_reg(reg, card) (0x8000 | (card)->io | \
  111. ((reg) & 0x0F) | (((reg) & 0xF0) << 6))
  112. #define sca_in(reg, card) inb(sca_reg(reg, card))
  113. #define sca_out(value, reg, card) outb(value, sca_reg(reg, card))
  114. #define sca_inw(reg, card) inw(sca_reg(reg, card))
  115. #define sca_outw(value, reg, card) outw(value, sca_reg(reg, card))
  116. #define port_to_card(port) ((port)->card)
  117. #define log_node(port) ((port)->log_node)
  118. #define phy_node(port) ((port)->phy_node)
  119. #define winsize(card) (USE_WINDOWSIZE)
  120. #define winbase(card) ((card)->winbase)
  121. #define get_port(card, port) ((card)->ports[port].valid ? \
  122. &(card)->ports[port] : NULL)
  123. static __inline__ u8 sca_get_page(card_t *card)
  124. {
  125. return inb(card->io + N2_PSR) & PSR_PAGEBITS;
  126. }
  127. static __inline__ void openwin(card_t *card, u8 page)
  128. {
  129. u8 psr = inb(card->io + N2_PSR);
  130. outb((psr & ~PSR_PAGEBITS) | page, card->io + N2_PSR);
  131. }
  132. #include "hd64570.c"
  133. static void n2_set_iface(port_t *port)
  134. {
  135. card_t *card = port->card;
  136. int io = card->io;
  137. u8 mcr = inb(io + N2_MCR);
  138. u8 msci = get_msci(port);
  139. u8 rxs = port->rxs & CLK_BRG_MASK;
  140. u8 txs = port->txs & CLK_BRG_MASK;
  141. switch(port->settings.clock_type) {
  142. case CLOCK_INT:
  143. mcr |= port->phy_node ? CLOCK_OUT_PORT1 : CLOCK_OUT_PORT0;
  144. rxs |= CLK_BRG_RX; /* BRG output */
  145. txs |= CLK_RXCLK_TX; /* RX clock */
  146. break;
  147. case CLOCK_TXINT:
  148. mcr |= port->phy_node ? CLOCK_OUT_PORT1 : CLOCK_OUT_PORT0;
  149. rxs |= CLK_LINE_RX; /* RXC input */
  150. txs |= CLK_BRG_TX; /* BRG output */
  151. break;
  152. case CLOCK_TXFROMRX:
  153. mcr |= port->phy_node ? CLOCK_OUT_PORT1 : CLOCK_OUT_PORT0;
  154. rxs |= CLK_LINE_RX; /* RXC input */
  155. txs |= CLK_RXCLK_TX; /* RX clock */
  156. break;
  157. default: /* Clock EXTernal */
  158. mcr &= port->phy_node ? ~CLOCK_OUT_PORT1 : ~CLOCK_OUT_PORT0;
  159. rxs |= CLK_LINE_RX; /* RXC input */
  160. txs |= CLK_LINE_TX; /* TXC input */
  161. }
  162. outb(mcr, io + N2_MCR);
  163. port->rxs = rxs;
  164. port->txs = txs;
  165. sca_out(rxs, msci + RXS, card);
  166. sca_out(txs, msci + TXS, card);
  167. sca_set_port(port);
  168. }
  169. static int n2_open(struct net_device *dev)
  170. {
  171. port_t *port = dev_to_port(dev);
  172. int io = port->card->io;
  173. u8 mcr = inb(io + N2_MCR) | (port->phy_node ? TX422_PORT1:TX422_PORT0);
  174. int result;
  175. result = hdlc_open(dev);
  176. if (result)
  177. return result;
  178. mcr &= port->phy_node ? ~DTR_PORT1 : ~DTR_PORT0; /* set DTR ON */
  179. outb(mcr, io + N2_MCR);
  180. outb(inb(io + N2_PCR) | PCR_ENWIN, io + N2_PCR); /* open window */
  181. outb(inb(io + N2_PSR) | PSR_DMAEN, io + N2_PSR); /* enable dma */
  182. sca_open(dev);
  183. n2_set_iface(port);
  184. return 0;
  185. }
  186. static int n2_close(struct net_device *dev)
  187. {
  188. port_t *port = dev_to_port(dev);
  189. int io = port->card->io;
  190. u8 mcr = inb(io+N2_MCR) | (port->phy_node ? TX422_PORT1 : TX422_PORT0);
  191. sca_close(dev);
  192. mcr |= port->phy_node ? DTR_PORT1 : DTR_PORT0; /* set DTR OFF */
  193. outb(mcr, io + N2_MCR);
  194. hdlc_close(dev);
  195. return 0;
  196. }
  197. static int n2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  198. {
  199. const size_t size = sizeof(sync_serial_settings);
  200. sync_serial_settings new_line;
  201. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  202. port_t *port = dev_to_port(dev);
  203. #ifdef DEBUG_RINGS
  204. if (cmd == SIOCDEVPRIVATE) {
  205. sca_dump_rings(dev);
  206. return 0;
  207. }
  208. #endif
  209. if (cmd != SIOCWANDEV)
  210. return hdlc_ioctl(dev, ifr, cmd);
  211. switch(ifr->ifr_settings.type) {
  212. case IF_GET_IFACE:
  213. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  214. if (ifr->ifr_settings.size < size) {
  215. ifr->ifr_settings.size = size; /* data size wanted */
  216. return -ENOBUFS;
  217. }
  218. if (copy_to_user(line, &port->settings, size))
  219. return -EFAULT;
  220. return 0;
  221. case IF_IFACE_SYNC_SERIAL:
  222. if(!capable(CAP_NET_ADMIN))
  223. return -EPERM;
  224. if (copy_from_user(&new_line, line, size))
  225. return -EFAULT;
  226. if (new_line.clock_type != CLOCK_EXT &&
  227. new_line.clock_type != CLOCK_TXFROMRX &&
  228. new_line.clock_type != CLOCK_INT &&
  229. new_line.clock_type != CLOCK_TXINT)
  230. return -EINVAL; /* No such clock setting */
  231. if (new_line.loopback != 0 && new_line.loopback != 1)
  232. return -EINVAL;
  233. memcpy(&port->settings, &new_line, size); /* Update settings */
  234. n2_set_iface(port);
  235. return 0;
  236. default:
  237. return hdlc_ioctl(dev, ifr, cmd);
  238. }
  239. }
  240. static void n2_destroy_card(card_t *card)
  241. {
  242. int cnt;
  243. for (cnt = 0; cnt < 2; cnt++)
  244. if (card->ports[cnt].card) {
  245. struct net_device *dev = port_to_dev(&card->ports[cnt]);
  246. unregister_hdlc_device(dev);
  247. }
  248. if (card->irq)
  249. free_irq(card->irq, card);
  250. if (card->winbase) {
  251. iounmap(card->winbase);
  252. release_mem_region(card->phy_winbase, USE_WINDOWSIZE);
  253. }
  254. if (card->io)
  255. release_region(card->io, N2_IOPORTS);
  256. if (card->ports[0].dev)
  257. free_netdev(card->ports[0].dev);
  258. if (card->ports[1].dev)
  259. free_netdev(card->ports[1].dev);
  260. kfree(card);
  261. }
  262. static const struct net_device_ops n2_ops = {
  263. .ndo_open = n2_open,
  264. .ndo_stop = n2_close,
  265. .ndo_change_mtu = hdlc_change_mtu,
  266. .ndo_start_xmit = hdlc_start_xmit,
  267. .ndo_do_ioctl = n2_ioctl,
  268. };
  269. static int __init n2_run(unsigned long io, unsigned long irq,
  270. unsigned long winbase, long valid0, long valid1)
  271. {
  272. card_t *card;
  273. u8 cnt, pcr;
  274. int i;
  275. if (io < 0x200 || io > 0x3FF || (io % N2_IOPORTS) != 0) {
  276. pr_err("invalid I/O port value\n");
  277. return -ENODEV;
  278. }
  279. if (irq < 3 || irq > 15 || irq == 6) /* FIXME */ {
  280. pr_err("invalid IRQ value\n");
  281. return -ENODEV;
  282. }
  283. if (winbase < 0xA0000 || winbase > 0xFFFFF || (winbase & 0xFFF) != 0) {
  284. pr_err("invalid RAM value\n");
  285. return -ENODEV;
  286. }
  287. card = kzalloc(sizeof(card_t), GFP_KERNEL);
  288. if (card == NULL)
  289. return -ENOBUFS;
  290. card->ports[0].dev = alloc_hdlcdev(&card->ports[0]);
  291. card->ports[1].dev = alloc_hdlcdev(&card->ports[1]);
  292. if (!card->ports[0].dev || !card->ports[1].dev) {
  293. pr_err("unable to allocate memory\n");
  294. n2_destroy_card(card);
  295. return -ENOMEM;
  296. }
  297. if (!request_region(io, N2_IOPORTS, devname)) {
  298. pr_err("I/O port region in use\n");
  299. n2_destroy_card(card);
  300. return -EBUSY;
  301. }
  302. card->io = io;
  303. if (request_irq(irq, sca_intr, 0, devname, card)) {
  304. pr_err("could not allocate IRQ\n");
  305. n2_destroy_card(card);
  306. return -EBUSY;
  307. }
  308. card->irq = irq;
  309. if (!request_mem_region(winbase, USE_WINDOWSIZE, devname)) {
  310. pr_err("could not request RAM window\n");
  311. n2_destroy_card(card);
  312. return -EBUSY;
  313. }
  314. card->phy_winbase = winbase;
  315. card->winbase = ioremap(winbase, USE_WINDOWSIZE);
  316. if (!card->winbase) {
  317. pr_err("ioremap() failed\n");
  318. n2_destroy_card(card);
  319. return -EFAULT;
  320. }
  321. outb(0, io + N2_PCR);
  322. outb(winbase >> 12, io + N2_BAR);
  323. switch (USE_WINDOWSIZE) {
  324. case 16384:
  325. outb(WIN16K, io + N2_PSR);
  326. break;
  327. case 32768:
  328. outb(WIN32K, io + N2_PSR);
  329. break;
  330. case 65536:
  331. outb(WIN64K, io + N2_PSR);
  332. break;
  333. default:
  334. pr_err("invalid window size\n");
  335. n2_destroy_card(card);
  336. return -ENODEV;
  337. }
  338. pcr = PCR_ENWIN | PCR_VPM | (USE_BUS16BITS ? PCR_BUS16 : 0);
  339. outb(pcr, io + N2_PCR);
  340. card->ram_size = sca_detect_ram(card, card->winbase, MAX_RAM_SIZE);
  341. /* number of TX + RX buffers for one port */
  342. i = card->ram_size / ((valid0 + valid1) * (sizeof(pkt_desc) +
  343. HDLC_MAX_MRU));
  344. card->tx_ring_buffers = min(i / 2, MAX_TX_BUFFERS);
  345. card->rx_ring_buffers = i - card->tx_ring_buffers;
  346. card->buff_offset = (valid0 + valid1) * sizeof(pkt_desc) *
  347. (card->tx_ring_buffers + card->rx_ring_buffers);
  348. pr_info("RISCom/N2 %u KB RAM, IRQ%u, using %u TX + %u RX packets rings\n",
  349. card->ram_size / 1024, card->irq,
  350. card->tx_ring_buffers, card->rx_ring_buffers);
  351. if (card->tx_ring_buffers < 1) {
  352. pr_err("RAM test failed\n");
  353. n2_destroy_card(card);
  354. return -EIO;
  355. }
  356. pcr |= PCR_RUNSCA; /* run SCA */
  357. outb(pcr, io + N2_PCR);
  358. outb(0, io + N2_MCR);
  359. sca_init(card, 0);
  360. for (cnt = 0; cnt < 2; cnt++) {
  361. port_t *port = &card->ports[cnt];
  362. struct net_device *dev = port_to_dev(port);
  363. hdlc_device *hdlc = dev_to_hdlc(dev);
  364. if ((cnt == 0 && !valid0) || (cnt == 1 && !valid1))
  365. continue;
  366. port->phy_node = cnt;
  367. port->valid = 1;
  368. if ((cnt == 1) && valid0)
  369. port->log_node = 1;
  370. spin_lock_init(&port->lock);
  371. dev->irq = irq;
  372. dev->mem_start = winbase;
  373. dev->mem_end = winbase + USE_WINDOWSIZE - 1;
  374. dev->tx_queue_len = 50;
  375. dev->netdev_ops = &n2_ops;
  376. hdlc->attach = sca_attach;
  377. hdlc->xmit = sca_xmit;
  378. port->settings.clock_type = CLOCK_EXT;
  379. port->card = card;
  380. if (register_hdlc_device(dev)) {
  381. pr_warn("unable to register hdlc device\n");
  382. port->card = NULL;
  383. n2_destroy_card(card);
  384. return -ENOBUFS;
  385. }
  386. sca_init_port(port); /* Set up SCA memory */
  387. netdev_info(dev, "RISCom/N2 node %d\n", port->phy_node);
  388. }
  389. *new_card = card;
  390. new_card = &card->next_card;
  391. return 0;
  392. }
  393. static int __init n2_init(void)
  394. {
  395. if (hw==NULL) {
  396. #ifdef MODULE
  397. pr_info("no card initialized\n");
  398. #endif
  399. return -EINVAL; /* no parameters specified, abort */
  400. }
  401. pr_info("%s\n", version);
  402. do {
  403. unsigned long io, irq, ram;
  404. long valid[2] = { 0, 0 }; /* Default = both ports disabled */
  405. io = simple_strtoul(hw, &hw, 0);
  406. if (*hw++ != ',')
  407. break;
  408. irq = simple_strtoul(hw, &hw, 0);
  409. if (*hw++ != ',')
  410. break;
  411. ram = simple_strtoul(hw, &hw, 0);
  412. if (*hw++ != ',')
  413. break;
  414. while(1) {
  415. if (*hw == '0' && !valid[0])
  416. valid[0] = 1; /* Port 0 enabled */
  417. else if (*hw == '1' && !valid[1])
  418. valid[1] = 1; /* Port 1 enabled */
  419. else
  420. break;
  421. hw++;
  422. }
  423. if (!valid[0] && !valid[1])
  424. break; /* at least one port must be used */
  425. if (*hw == ':' || *hw == '\x0')
  426. n2_run(io, irq, ram, valid[0], valid[1]);
  427. if (*hw == '\x0')
  428. return first_card ? 0 : -EINVAL;
  429. }while(*hw++ == ':');
  430. pr_err("invalid hardware parameters\n");
  431. return first_card ? 0 : -EINVAL;
  432. }
  433. static void __exit n2_cleanup(void)
  434. {
  435. card_t *card = first_card;
  436. while (card) {
  437. card_t *ptr = card;
  438. card = card->next_card;
  439. n2_destroy_card(ptr);
  440. }
  441. }
  442. module_init(n2_init);
  443. module_exit(n2_cleanup);
  444. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  445. MODULE_DESCRIPTION("RISCom/N2 serial port driver");
  446. MODULE_LICENSE("GPL v2");
  447. module_param(hw, charp, 0444);
  448. MODULE_PARM_DESC(hw, "io,irq,ram,ports:io,irq,...");