c101.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Moxa C101 synchronous serial card driver for Linux
  3. *
  4. * Copyright (C) 2000-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. * Sources of information:
  13. * Hitachi HD64570 SCA User's Manual
  14. * Moxa C101 User's Manual
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/capability.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/string.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/hdlc.h>
  28. #include <linux/delay.h>
  29. #include <asm/io.h>
  30. #include "hd64570.h"
  31. static const char* version = "Moxa C101 driver version: 1.15";
  32. static const char* devname = "C101";
  33. #undef DEBUG_PKT
  34. #define DEBUG_RINGS
  35. #define C101_PAGE 0x1D00
  36. #define C101_DTR 0x1E00
  37. #define C101_SCA 0x1F00
  38. #define C101_WINDOW_SIZE 0x2000
  39. #define C101_MAPPED_RAM_SIZE 0x4000
  40. #define RAM_SIZE (256 * 1024)
  41. #define TX_RING_BUFFERS 10
  42. #define RX_RING_BUFFERS ((RAM_SIZE - C101_WINDOW_SIZE) / \
  43. (sizeof(pkt_desc) + HDLC_MAX_MRU) - TX_RING_BUFFERS)
  44. #define CLOCK_BASE 9830400 /* 9.8304 MHz */
  45. #define PAGE0_ALWAYS_MAPPED
  46. static char *hw; /* pointer to hw=xxx command line string */
  47. typedef struct card_s {
  48. struct net_device *dev;
  49. spinlock_t lock; /* TX lock */
  50. u8 __iomem *win0base; /* ISA window base address */
  51. u32 phy_winbase; /* ISA physical base address */
  52. sync_serial_settings settings;
  53. int rxpart; /* partial frame received, next frame invalid*/
  54. unsigned short encoding;
  55. unsigned short parity;
  56. u16 rx_ring_buffers; /* number of buffers in a ring */
  57. u16 tx_ring_buffers;
  58. u16 buff_offset; /* offset of first buffer of first channel */
  59. u16 rxin; /* rx ring buffer 'in' pointer */
  60. u16 txin; /* tx ring buffer 'in' and 'last' pointers */
  61. u16 txlast;
  62. u8 rxs, txs, tmc; /* SCA registers */
  63. u8 irq; /* IRQ (3-15) */
  64. u8 page;
  65. struct card_s *next_card;
  66. }card_t;
  67. typedef card_t port_t;
  68. static card_t *first_card;
  69. static card_t **new_card = &first_card;
  70. #define sca_in(reg, card) readb((card)->win0base + C101_SCA + (reg))
  71. #define sca_out(value, reg, card) writeb(value, (card)->win0base + C101_SCA + (reg))
  72. #define sca_inw(reg, card) readw((card)->win0base + C101_SCA + (reg))
  73. /* EDA address register must be set in EDAL, EDAH order - 8 bit ISA bus */
  74. #define sca_outw(value, reg, card) do { \
  75. writeb(value & 0xFF, (card)->win0base + C101_SCA + (reg)); \
  76. writeb((value >> 8 ) & 0xFF, (card)->win0base + C101_SCA + (reg + 1));\
  77. } while(0)
  78. #define port_to_card(port) (port)
  79. #define log_node(port) (0)
  80. #define phy_node(port) (0)
  81. #define winsize(card) (C101_WINDOW_SIZE)
  82. #define win0base(card) ((card)->win0base)
  83. #define winbase(card) ((card)->win0base + 0x2000)
  84. #define get_port(card, port) (card)
  85. static void sca_msci_intr(port_t *port);
  86. static inline u8 sca_get_page(card_t *card)
  87. {
  88. return card->page;
  89. }
  90. static inline void openwin(card_t *card, u8 page)
  91. {
  92. card->page = page;
  93. writeb(page, card->win0base + C101_PAGE);
  94. }
  95. #include "hd64570.c"
  96. static inline void set_carrier(port_t *port)
  97. {
  98. if (!(sca_in(MSCI1_OFFSET + ST3, port) & ST3_DCD))
  99. netif_carrier_on(port_to_dev(port));
  100. else
  101. netif_carrier_off(port_to_dev(port));
  102. }
  103. static void sca_msci_intr(port_t *port)
  104. {
  105. u8 stat = sca_in(MSCI0_OFFSET + ST1, port); /* read MSCI ST1 status */
  106. /* Reset MSCI TX underrun and CDCD (ignored) status bit */
  107. sca_out(stat & (ST1_UDRN | ST1_CDCD), MSCI0_OFFSET + ST1, port);
  108. if (stat & ST1_UDRN) {
  109. /* TX Underrun error detected */
  110. port_to_dev(port)->stats.tx_errors++;
  111. port_to_dev(port)->stats.tx_fifo_errors++;
  112. }
  113. stat = sca_in(MSCI1_OFFSET + ST1, port); /* read MSCI1 ST1 status */
  114. /* Reset MSCI CDCD status bit - uses ch#2 DCD input */
  115. sca_out(stat & ST1_CDCD, MSCI1_OFFSET + ST1, port);
  116. if (stat & ST1_CDCD)
  117. set_carrier(port);
  118. }
  119. static void c101_set_iface(port_t *port)
  120. {
  121. u8 rxs = port->rxs & CLK_BRG_MASK;
  122. u8 txs = port->txs & CLK_BRG_MASK;
  123. switch(port->settings.clock_type) {
  124. case CLOCK_INT:
  125. rxs |= CLK_BRG_RX; /* TX clock */
  126. txs |= CLK_RXCLK_TX; /* BRG output */
  127. break;
  128. case CLOCK_TXINT:
  129. rxs |= CLK_LINE_RX; /* RXC input */
  130. txs |= CLK_BRG_TX; /* BRG output */
  131. break;
  132. case CLOCK_TXFROMRX:
  133. rxs |= CLK_LINE_RX; /* RXC input */
  134. txs |= CLK_RXCLK_TX; /* RX clock */
  135. break;
  136. default: /* EXTernal clock */
  137. rxs |= CLK_LINE_RX; /* RXC input */
  138. txs |= CLK_LINE_TX; /* TXC input */
  139. }
  140. port->rxs = rxs;
  141. port->txs = txs;
  142. sca_out(rxs, MSCI1_OFFSET + RXS, port);
  143. sca_out(txs, MSCI1_OFFSET + TXS, port);
  144. sca_set_port(port);
  145. }
  146. static int c101_open(struct net_device *dev)
  147. {
  148. port_t *port = dev_to_port(dev);
  149. int result;
  150. result = hdlc_open(dev);
  151. if (result)
  152. return result;
  153. writeb(1, port->win0base + C101_DTR);
  154. sca_out(0, MSCI1_OFFSET + CTL, port); /* RTS uses ch#2 output */
  155. sca_open(dev);
  156. /* DCD is connected to port 2 !@#$%^& - disable MSCI0 CDCD interrupt */
  157. sca_out(IE1_UDRN, MSCI0_OFFSET + IE1, port);
  158. sca_out(IE0_TXINT, MSCI0_OFFSET + IE0, port);
  159. set_carrier(port);
  160. /* enable MSCI1 CDCD interrupt */
  161. sca_out(IE1_CDCD, MSCI1_OFFSET + IE1, port);
  162. sca_out(IE0_RXINTA, MSCI1_OFFSET + IE0, port);
  163. sca_out(0x48, IER0, port); /* TXINT #0 and RXINT #1 */
  164. c101_set_iface(port);
  165. return 0;
  166. }
  167. static int c101_close(struct net_device *dev)
  168. {
  169. port_t *port = dev_to_port(dev);
  170. sca_close(dev);
  171. writeb(0, port->win0base + C101_DTR);
  172. sca_out(CTL_NORTS, MSCI1_OFFSET + CTL, port);
  173. hdlc_close(dev);
  174. return 0;
  175. }
  176. static int c101_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  177. {
  178. const size_t size = sizeof(sync_serial_settings);
  179. sync_serial_settings new_line;
  180. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  181. port_t *port = dev_to_port(dev);
  182. #ifdef DEBUG_RINGS
  183. if (cmd == SIOCDEVPRIVATE) {
  184. sca_dump_rings(dev);
  185. printk(KERN_DEBUG "MSCI1: ST: %02x %02x %02x %02x\n",
  186. sca_in(MSCI1_OFFSET + ST0, port),
  187. sca_in(MSCI1_OFFSET + ST1, port),
  188. sca_in(MSCI1_OFFSET + ST2, port),
  189. sca_in(MSCI1_OFFSET + ST3, port));
  190. return 0;
  191. }
  192. #endif
  193. if (cmd != SIOCWANDEV)
  194. return hdlc_ioctl(dev, ifr, cmd);
  195. switch(ifr->ifr_settings.type) {
  196. case IF_GET_IFACE:
  197. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  198. if (ifr->ifr_settings.size < size) {
  199. ifr->ifr_settings.size = size; /* data size wanted */
  200. return -ENOBUFS;
  201. }
  202. if (copy_to_user(line, &port->settings, size))
  203. return -EFAULT;
  204. return 0;
  205. case IF_IFACE_SYNC_SERIAL:
  206. if(!capable(CAP_NET_ADMIN))
  207. return -EPERM;
  208. if (copy_from_user(&new_line, line, size))
  209. return -EFAULT;
  210. if (new_line.clock_type != CLOCK_EXT &&
  211. new_line.clock_type != CLOCK_TXFROMRX &&
  212. new_line.clock_type != CLOCK_INT &&
  213. new_line.clock_type != CLOCK_TXINT)
  214. return -EINVAL; /* No such clock setting */
  215. if (new_line.loopback != 0 && new_line.loopback != 1)
  216. return -EINVAL;
  217. memcpy(&port->settings, &new_line, size); /* Update settings */
  218. c101_set_iface(port);
  219. return 0;
  220. default:
  221. return hdlc_ioctl(dev, ifr, cmd);
  222. }
  223. }
  224. static void c101_destroy_card(card_t *card)
  225. {
  226. readb(card->win0base + C101_PAGE); /* Resets SCA? */
  227. if (card->irq)
  228. free_irq(card->irq, card);
  229. if (card->win0base) {
  230. iounmap(card->win0base);
  231. release_mem_region(card->phy_winbase, C101_MAPPED_RAM_SIZE);
  232. }
  233. free_netdev(card->dev);
  234. kfree(card);
  235. }
  236. static const struct net_device_ops c101_ops = {
  237. .ndo_open = c101_open,
  238. .ndo_stop = c101_close,
  239. .ndo_change_mtu = hdlc_change_mtu,
  240. .ndo_start_xmit = hdlc_start_xmit,
  241. .ndo_do_ioctl = c101_ioctl,
  242. };
  243. static int __init c101_run(unsigned long irq, unsigned long winbase)
  244. {
  245. struct net_device *dev;
  246. hdlc_device *hdlc;
  247. card_t *card;
  248. int result;
  249. if (irq<3 || irq>15 || irq == 6) /* FIXME */ {
  250. pr_err("invalid IRQ value\n");
  251. return -ENODEV;
  252. }
  253. if (winbase < 0xC0000 || winbase > 0xDFFFF || (winbase & 0x3FFF) !=0) {
  254. pr_err("invalid RAM value\n");
  255. return -ENODEV;
  256. }
  257. card = kzalloc(sizeof(card_t), GFP_KERNEL);
  258. if (card == NULL)
  259. return -ENOBUFS;
  260. card->dev = alloc_hdlcdev(card);
  261. if (!card->dev) {
  262. pr_err("unable to allocate memory\n");
  263. kfree(card);
  264. return -ENOBUFS;
  265. }
  266. if (request_irq(irq, sca_intr, 0, devname, card)) {
  267. pr_err("could not allocate IRQ\n");
  268. c101_destroy_card(card);
  269. return -EBUSY;
  270. }
  271. card->irq = irq;
  272. if (!request_mem_region(winbase, C101_MAPPED_RAM_SIZE, devname)) {
  273. pr_err("could not request RAM window\n");
  274. c101_destroy_card(card);
  275. return -EBUSY;
  276. }
  277. card->phy_winbase = winbase;
  278. card->win0base = ioremap(winbase, C101_MAPPED_RAM_SIZE);
  279. if (!card->win0base) {
  280. pr_err("could not map I/O address\n");
  281. c101_destroy_card(card);
  282. return -EFAULT;
  283. }
  284. card->tx_ring_buffers = TX_RING_BUFFERS;
  285. card->rx_ring_buffers = RX_RING_BUFFERS;
  286. card->buff_offset = C101_WINDOW_SIZE; /* Bytes 1D00-1FFF reserved */
  287. readb(card->win0base + C101_PAGE); /* Resets SCA? */
  288. udelay(100);
  289. writeb(0, card->win0base + C101_PAGE);
  290. writeb(0, card->win0base + C101_DTR); /* Power-up for RAM? */
  291. sca_init(card, 0);
  292. dev = port_to_dev(card);
  293. hdlc = dev_to_hdlc(dev);
  294. spin_lock_init(&card->lock);
  295. dev->irq = irq;
  296. dev->mem_start = winbase;
  297. dev->mem_end = winbase + C101_MAPPED_RAM_SIZE - 1;
  298. dev->tx_queue_len = 50;
  299. dev->netdev_ops = &c101_ops;
  300. hdlc->attach = sca_attach;
  301. hdlc->xmit = sca_xmit;
  302. card->settings.clock_type = CLOCK_EXT;
  303. result = register_hdlc_device(dev);
  304. if (result) {
  305. pr_warn("unable to register hdlc device\n");
  306. c101_destroy_card(card);
  307. return result;
  308. }
  309. sca_init_port(card); /* Set up C101 memory */
  310. set_carrier(card);
  311. netdev_info(dev, "Moxa C101 on IRQ%u, using %u TX + %u RX packets rings\n",
  312. card->irq, card->tx_ring_buffers, card->rx_ring_buffers);
  313. *new_card = card;
  314. new_card = &card->next_card;
  315. return 0;
  316. }
  317. static int __init c101_init(void)
  318. {
  319. if (hw == NULL) {
  320. #ifdef MODULE
  321. pr_info("no card initialized\n");
  322. #endif
  323. return -EINVAL; /* no parameters specified, abort */
  324. }
  325. pr_info("%s\n", version);
  326. do {
  327. unsigned long irq, ram;
  328. irq = simple_strtoul(hw, &hw, 0);
  329. if (*hw++ != ',')
  330. break;
  331. ram = simple_strtoul(hw, &hw, 0);
  332. if (*hw == ':' || *hw == '\x0')
  333. c101_run(irq, ram);
  334. if (*hw == '\x0')
  335. return first_card ? 0 : -EINVAL;
  336. }while(*hw++ == ':');
  337. pr_err("invalid hardware parameters\n");
  338. return first_card ? 0 : -EINVAL;
  339. }
  340. static void __exit c101_cleanup(void)
  341. {
  342. card_t *card = first_card;
  343. while (card) {
  344. card_t *ptr = card;
  345. card = card->next_card;
  346. unregister_hdlc_device(port_to_dev(ptr));
  347. c101_destroy_card(ptr);
  348. }
  349. }
  350. module_init(c101_init);
  351. module_exit(c101_cleanup);
  352. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  353. MODULE_DESCRIPTION("Moxa C101 serial port driver");
  354. MODULE_LICENSE("GPL v2");
  355. module_param(hw, charp, 0444);
  356. MODULE_PARM_DESC(hw, "irq,ram:irq,...");