1 // SPDX-License-Identifier: GPL-2.0-only
3 * SDL Inc. RISCom/N2 synchronous serial card driver for Linux
7 * For information see <https://www.kernel.org/pub/linux/utils/net/hdlc/>
9 * Note: integrated CSU/DSU/DDS are not supported by this driver
11 * Sources of information:
12 * Hitachi HD64570 SCA User's Manual
13 * SDL Inc. PPP/HDLC/CISCO driver
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/capability.h>
21 #include <linux/slab.h>
22 #include <linux/types.h>
23 #include <linux/fcntl.h>
25 #include <linux/string.h>
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/ioport.h>
29 #include <linux/moduleparam.h>
30 #include <linux/netdevice.h>
31 #include <linux/hdlc.h>
35 static const char *version = "SDL RISCom/N2 driver version: 1.15";
36 static const char *devname = "RISCom/N2";
41 #define USE_WINDOWSIZE 16384
42 #define USE_BUS16BITS 1
43 #define CLOCK_BASE 9830400 /* 9.8304 MHz */
44 #define MAX_PAGES 16 /* 16 RAM pages at max */
45 #define MAX_RAM_SIZE 0x80000 /* 512 KB */
46 #if MAX_RAM_SIZE > MAX_PAGES * USE_WINDOWSIZE
48 #define MAX_RAM_SIZE (MAX_PAGES * USE_WINDOWSIZE)
50 #define N2_IOPORTS 0x10
51 #define NEED_DETECT_RAM
52 #define NEED_SCA_MSCI_INTR
53 #define MAX_TX_BUFFERS 10
55 static char *hw; /* pointer to hw=xxx command line string */
57 /* RISCom/N2 Board Registers */
59 /* PC Control Register */
61 #define PCR_RUNSCA 1 /* Run 64570 */
62 #define PCR_VPM 2 /* Enable VPM - needed if using RAM above 1 MB */
63 #define PCR_ENWIN 4 /* Open window */
64 #define PCR_BUS16 8 /* 16-bit bus */
66 /* Memory Base Address Register */
69 /* Page Scan Register */
74 #define PSR_WINBITS 0x60
75 #define PSR_DMAEN 0x80
76 #define PSR_PAGEBITS 0x0F
78 /* Modem Control Reg */
80 #define CLOCK_OUT_PORT1 0x80
81 #define CLOCK_OUT_PORT0 0x40
82 #define TX422_PORT1 0x20
83 #define TX422_PORT0 0x10
84 #define DSR_PORT1 0x08
85 #define DSR_PORT0 0x04
86 #define DTR_PORT1 0x02
87 #define DTR_PORT0 0x01
89 typedef struct port_s {
90 struct net_device *dev;
92 spinlock_t lock; /* TX lock */
93 sync_serial_settings settings;
94 int valid; /* port enabled */
95 int rxpart; /* partial frame received, next frame invalid*/
96 unsigned short encoding;
97 unsigned short parity;
98 u16 rxin; /* rx ring buffer 'in' pointer */
99 u16 txin; /* tx ring buffer 'in' and 'last' pointers */
101 u8 rxs, txs, tmc; /* SCA registers */
102 u8 phy_node; /* physical port # - 0 or 1 */
103 u8 log_node; /* logical port # */
106 typedef struct card_s {
107 u8 __iomem *winbase; /* ISA window base address */
108 u32 phy_winbase; /* ISA physical base address */
109 u32 ram_size; /* number of bytes */
110 u16 io; /* IO Base address */
111 u16 buff_offset; /* offset of first buffer of first channel */
112 u16 rx_ring_buffers; /* number of buffers in a ring */
114 u8 irq; /* IRQ (3-15) */
117 struct card_s *next_card;
120 static card_t *first_card;
121 static card_t **new_card = &first_card;
123 #define sca_reg(reg, card) (0x8000 | (card)->io | \
124 ((reg) & 0x0F) | (((reg) & 0xF0) << 6))
125 #define sca_in(reg, card) inb(sca_reg(reg, card))
126 #define sca_out(value, reg, card) outb(value, sca_reg(reg, card))
127 #define sca_inw(reg, card) inw(sca_reg(reg, card))
128 #define sca_outw(value, reg, card) outw(value, sca_reg(reg, card))
130 #define port_to_card(port) ((port)->card)
131 #define log_node(port) ((port)->log_node)
132 #define phy_node(port) ((port)->phy_node)
133 #define winsize(card) (USE_WINDOWSIZE)
134 #define winbase(card) ((card)->winbase)
135 #define get_port(card, port) ((card)->ports[port].valid ? \
136 &(card)->ports[port] : NULL)
138 static __inline__ u8 sca_get_page(card_t *card)
140 return inb(card->io + N2_PSR) & PSR_PAGEBITS;
143 static __inline__ void openwin(card_t *card, u8 page)
145 u8 psr = inb(card->io + N2_PSR);
147 outb((psr & ~PSR_PAGEBITS) | page, card->io + N2_PSR);
152 static void n2_set_iface(port_t *port)
154 card_t *card = port->card;
156 u8 mcr = inb(io + N2_MCR);
157 u8 msci = get_msci(port);
158 u8 rxs = port->rxs & CLK_BRG_MASK;
159 u8 txs = port->txs & CLK_BRG_MASK;
161 switch (port->settings.clock_type) {
163 mcr |= port->phy_node ? CLOCK_OUT_PORT1 : CLOCK_OUT_PORT0;
164 rxs |= CLK_BRG_RX; /* BRG output */
165 txs |= CLK_RXCLK_TX; /* RX clock */
169 mcr |= port->phy_node ? CLOCK_OUT_PORT1 : CLOCK_OUT_PORT0;
170 rxs |= CLK_LINE_RX; /* RXC input */
171 txs |= CLK_BRG_TX; /* BRG output */
175 mcr |= port->phy_node ? CLOCK_OUT_PORT1 : CLOCK_OUT_PORT0;
176 rxs |= CLK_LINE_RX; /* RXC input */
177 txs |= CLK_RXCLK_TX; /* RX clock */
180 default: /* Clock EXTernal */
181 mcr &= port->phy_node ? ~CLOCK_OUT_PORT1 : ~CLOCK_OUT_PORT0;
182 rxs |= CLK_LINE_RX; /* RXC input */
183 txs |= CLK_LINE_TX; /* TXC input */
186 outb(mcr, io + N2_MCR);
189 sca_out(rxs, msci + RXS, card);
190 sca_out(txs, msci + TXS, card);
194 static int n2_open(struct net_device *dev)
196 port_t *port = dev_to_port(dev);
197 int io = port->card->io;
198 u8 mcr = inb(io + N2_MCR) |
199 (port->phy_node ? TX422_PORT1 : TX422_PORT0);
202 result = hdlc_open(dev);
206 mcr &= port->phy_node ? ~DTR_PORT1 : ~DTR_PORT0; /* set DTR ON */
207 outb(mcr, io + N2_MCR);
209 outb(inb(io + N2_PCR) | PCR_ENWIN, io + N2_PCR); /* open window */
210 outb(inb(io + N2_PSR) | PSR_DMAEN, io + N2_PSR); /* enable dma */
216 static int n2_close(struct net_device *dev)
218 port_t *port = dev_to_port(dev);
219 int io = port->card->io;
220 u8 mcr = inb(io + N2_MCR) |
221 (port->phy_node ? TX422_PORT1 : TX422_PORT0);
224 mcr |= port->phy_node ? DTR_PORT1 : DTR_PORT0; /* set DTR OFF */
225 outb(mcr, io + N2_MCR);
230 static int n2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
232 const size_t size = sizeof(sync_serial_settings);
233 sync_serial_settings new_line;
234 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
235 port_t *port = dev_to_port(dev);
238 if (cmd == SIOCDEVPRIVATE) {
243 if (cmd != SIOCWANDEV)
244 return hdlc_ioctl(dev, ifr, cmd);
246 switch (ifr->ifr_settings.type) {
248 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
249 if (ifr->ifr_settings.size < size) {
250 ifr->ifr_settings.size = size; /* data size wanted */
253 if (copy_to_user(line, &port->settings, size))
257 case IF_IFACE_SYNC_SERIAL:
258 if (!capable(CAP_NET_ADMIN))
261 if (copy_from_user(&new_line, line, size))
264 if (new_line.clock_type != CLOCK_EXT &&
265 new_line.clock_type != CLOCK_TXFROMRX &&
266 new_line.clock_type != CLOCK_INT &&
267 new_line.clock_type != CLOCK_TXINT)
268 return -EINVAL; /* No such clock setting */
270 if (new_line.loopback != 0 && new_line.loopback != 1)
273 memcpy(&port->settings, &new_line, size); /* Update settings */
278 return hdlc_ioctl(dev, ifr, cmd);
282 static void n2_destroy_card(card_t *card)
286 for (cnt = 0; cnt < 2; cnt++)
287 if (card->ports[cnt].card) {
288 struct net_device *dev = port_to_dev(&card->ports[cnt]);
290 unregister_hdlc_device(dev);
294 free_irq(card->irq, card);
297 iounmap(card->winbase);
298 release_mem_region(card->phy_winbase, USE_WINDOWSIZE);
302 release_region(card->io, N2_IOPORTS);
303 if (card->ports[0].dev)
304 free_netdev(card->ports[0].dev);
305 if (card->ports[1].dev)
306 free_netdev(card->ports[1].dev);
310 static const struct net_device_ops n2_ops = {
312 .ndo_stop = n2_close,
313 .ndo_start_xmit = hdlc_start_xmit,
314 .ndo_do_ioctl = n2_ioctl,
317 static int __init n2_run(unsigned long io, unsigned long irq,
318 unsigned long winbase, long valid0, long valid1)
324 if (io < 0x200 || io > 0x3FF || (io % N2_IOPORTS) != 0) {
325 pr_err("invalid I/O port value\n");
329 if (irq < 3 || irq > 15 || irq == 6) /* FIXME */ {
330 pr_err("invalid IRQ value\n");
334 if (winbase < 0xA0000 || winbase > 0xFFFFF || (winbase & 0xFFF) != 0) {
335 pr_err("invalid RAM value\n");
339 card = kzalloc(sizeof(card_t), GFP_KERNEL);
343 card->ports[0].dev = alloc_hdlcdev(&card->ports[0]);
344 card->ports[1].dev = alloc_hdlcdev(&card->ports[1]);
345 if (!card->ports[0].dev || !card->ports[1].dev) {
346 pr_err("unable to allocate memory\n");
347 n2_destroy_card(card);
351 if (!request_region(io, N2_IOPORTS, devname)) {
352 pr_err("I/O port region in use\n");
353 n2_destroy_card(card);
358 if (request_irq(irq, sca_intr, 0, devname, card)) {
359 pr_err("could not allocate IRQ\n");
360 n2_destroy_card(card);
365 if (!request_mem_region(winbase, USE_WINDOWSIZE, devname)) {
366 pr_err("could not request RAM window\n");
367 n2_destroy_card(card);
370 card->phy_winbase = winbase;
371 card->winbase = ioremap(winbase, USE_WINDOWSIZE);
372 if (!card->winbase) {
373 pr_err("ioremap() failed\n");
374 n2_destroy_card(card);
378 outb(0, io + N2_PCR);
379 outb(winbase >> 12, io + N2_BAR);
381 switch (USE_WINDOWSIZE) {
383 outb(WIN16K, io + N2_PSR);
387 outb(WIN32K, io + N2_PSR);
391 outb(WIN64K, io + N2_PSR);
395 pr_err("invalid window size\n");
396 n2_destroy_card(card);
400 pcr = PCR_ENWIN | PCR_VPM | (USE_BUS16BITS ? PCR_BUS16 : 0);
401 outb(pcr, io + N2_PCR);
403 card->ram_size = sca_detect_ram(card, card->winbase, MAX_RAM_SIZE);
405 /* number of TX + RX buffers for one port */
406 i = card->ram_size / ((valid0 + valid1) * (sizeof(pkt_desc) +
409 card->tx_ring_buffers = min(i / 2, MAX_TX_BUFFERS);
410 card->rx_ring_buffers = i - card->tx_ring_buffers;
412 card->buff_offset = (valid0 + valid1) * sizeof(pkt_desc) *
413 (card->tx_ring_buffers + card->rx_ring_buffers);
415 pr_info("RISCom/N2 %u KB RAM, IRQ%u, using %u TX + %u RX packets rings\n",
416 card->ram_size / 1024, card->irq,
417 card->tx_ring_buffers, card->rx_ring_buffers);
419 if (card->tx_ring_buffers < 1) {
420 pr_err("RAM test failed\n");
421 n2_destroy_card(card);
425 pcr |= PCR_RUNSCA; /* run SCA */
426 outb(pcr, io + N2_PCR);
427 outb(0, io + N2_MCR);
430 for (cnt = 0; cnt < 2; cnt++) {
431 port_t *port = &card->ports[cnt];
432 struct net_device *dev = port_to_dev(port);
433 hdlc_device *hdlc = dev_to_hdlc(dev);
435 if ((cnt == 0 && !valid0) || (cnt == 1 && !valid1))
438 port->phy_node = cnt;
441 if ((cnt == 1) && valid0)
444 spin_lock_init(&port->lock);
446 dev->mem_start = winbase;
447 dev->mem_end = winbase + USE_WINDOWSIZE - 1;
448 dev->tx_queue_len = 50;
449 dev->netdev_ops = &n2_ops;
450 hdlc->attach = sca_attach;
451 hdlc->xmit = sca_xmit;
452 port->settings.clock_type = CLOCK_EXT;
455 if (register_hdlc_device(dev)) {
456 pr_warn("unable to register hdlc device\n");
458 n2_destroy_card(card);
461 sca_init_port(port); /* Set up SCA memory */
463 netdev_info(dev, "RISCom/N2 node %d\n", port->phy_node);
467 new_card = &card->next_card;
472 static int __init n2_init(void)
476 pr_info("no card initialized\n");
478 return -EINVAL; /* no parameters specified, abort */
481 pr_info("%s\n", version);
484 unsigned long io, irq, ram;
485 long valid[2] = { 0, 0 }; /* Default = both ports disabled */
487 io = simple_strtoul(hw, &hw, 0);
491 irq = simple_strtoul(hw, &hw, 0);
495 ram = simple_strtoul(hw, &hw, 0);
500 if (*hw == '0' && !valid[0])
501 valid[0] = 1; /* Port 0 enabled */
502 else if (*hw == '1' && !valid[1])
503 valid[1] = 1; /* Port 1 enabled */
509 if (!valid[0] && !valid[1])
510 break; /* at least one port must be used */
512 if (*hw == ':' || *hw == '\x0')
513 n2_run(io, irq, ram, valid[0], valid[1]);
516 return first_card ? 0 : -EINVAL;
517 } while (*hw++ == ':');
519 pr_err("invalid hardware parameters\n");
520 return first_card ? 0 : -EINVAL;
523 static void __exit n2_cleanup(void)
525 card_t *card = first_card;
530 card = card->next_card;
531 n2_destroy_card(ptr);
535 module_init(n2_init);
536 module_exit(n2_cleanup);
539 MODULE_DESCRIPTION("RISCom/N2 serial port driver");
540 MODULE_LICENSE("GPL v2");
541 module_param(hw, charp, 0444);
542 MODULE_PARM_DESC(hw, "io,irq,ram,ports:io,irq,...");