n2.c 13 KB

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