2 * SDL Inc. RISCom/N2 synchronous serial card driver for Linux
4 * Copyright (C) 1998-2003 Krzysztof Halasa <khc@pm.waw.pl>
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.
10 * For information see <http://www.kernel.org/pub/linux/utils/net/hdlc/>
12 * Note: integrated CSU/DSU/DDS are not supported by this driver
14 * Sources of information:
15 * Hitachi HD64570 SCA User's Manual
16 * SDL Inc. PPP/HDLC/CISCO driver
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/capability.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
26 #include <linux/fcntl.h>
28 #include <linux/string.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/ioport.h>
32 #include <linux/moduleparam.h>
33 #include <linux/netdevice.h>
34 #include <linux/hdlc.h>
39 static const char* version
= "SDL RISCom/N2 driver version: 1.15";
40 static const char* devname
= "RISCom/N2";
45 #define USE_WINDOWSIZE 16384
46 #define USE_BUS16BITS 1
47 #define CLOCK_BASE 9830400 /* 9.8304 MHz */
48 #define MAX_PAGES 16 /* 16 RAM pages at max */
49 #define MAX_RAM_SIZE 0x80000 /* 512 KB */
50 #if MAX_RAM_SIZE > MAX_PAGES * USE_WINDOWSIZE
52 #define MAX_RAM_SIZE (MAX_PAGES * USE_WINDOWSIZE)
54 #define N2_IOPORTS 0x10
55 #define NEED_DETECT_RAM
56 #define NEED_SCA_MSCI_INTR
57 #define MAX_TX_BUFFERS 10
59 static char *hw
; /* pointer to hw=xxx command line string */
61 /* RISCom/N2 Board Registers */
63 /* PC Control Register */
65 #define PCR_RUNSCA 1 /* Run 64570 */
66 #define PCR_VPM 2 /* Enable VPM - needed if using RAM above 1 MB */
67 #define PCR_ENWIN 4 /* Open window */
68 #define PCR_BUS16 8 /* 16-bit bus */
71 /* Memory Base Address Register */
75 /* Page Scan Register */
80 #define PSR_WINBITS 0x60
81 #define PSR_DMAEN 0x80
82 #define PSR_PAGEBITS 0x0F
85 /* Modem Control Reg */
87 #define CLOCK_OUT_PORT1 0x80
88 #define CLOCK_OUT_PORT0 0x40
89 #define TX422_PORT1 0x20
90 #define TX422_PORT0 0x10
91 #define DSR_PORT1 0x08
92 #define DSR_PORT0 0x04
93 #define DTR_PORT1 0x02
94 #define DTR_PORT0 0x01
97 typedef struct port_s
{
98 struct net_device
*dev
;
100 spinlock_t lock
; /* TX lock */
101 sync_serial_settings settings
;
102 int valid
; /* port enabled */
103 int rxpart
; /* partial frame received, next frame invalid*/
104 unsigned short encoding
;
105 unsigned short parity
;
106 u16 rxin
; /* rx ring buffer 'in' pointer */
107 u16 txin
; /* tx ring buffer 'in' and 'last' pointers */
109 u8 rxs
, txs
, tmc
; /* SCA registers */
110 u8 phy_node
; /* physical port # - 0 or 1 */
111 u8 log_node
; /* logical port # */
116 typedef struct card_s
{
117 u8 __iomem
*winbase
; /* ISA window base address */
118 u32 phy_winbase
; /* ISA physical base address */
119 u32 ram_size
; /* number of bytes */
120 u16 io
; /* IO Base address */
121 u16 buff_offset
; /* offset of first buffer of first channel */
122 u16 rx_ring_buffers
; /* number of buffers in a ring */
124 u8 irq
; /* IRQ (3-15) */
127 struct card_s
*next_card
;
131 static card_t
*first_card
;
132 static card_t
**new_card
= &first_card
;
135 #define sca_reg(reg, card) (0x8000 | (card)->io | \
136 ((reg) & 0x0F) | (((reg) & 0xF0) << 6))
137 #define sca_in(reg, card) inb(sca_reg(reg, card))
138 #define sca_out(value, reg, card) outb(value, sca_reg(reg, card))
139 #define sca_inw(reg, card) inw(sca_reg(reg, card))
140 #define sca_outw(value, reg, card) outw(value, sca_reg(reg, card))
142 #define port_to_card(port) ((port)->card)
143 #define log_node(port) ((port)->log_node)
144 #define phy_node(port) ((port)->phy_node)
145 #define winsize(card) (USE_WINDOWSIZE)
146 #define winbase(card) ((card)->winbase)
147 #define get_port(card, port) ((card)->ports[port].valid ? \
148 &(card)->ports[port] : NULL)
151 static __inline__ u8
sca_get_page(card_t
*card
)
153 return inb(card
->io
+ N2_PSR
) & PSR_PAGEBITS
;
157 static __inline__
void openwin(card_t
*card
, u8 page
)
159 u8 psr
= inb(card
->io
+ N2_PSR
);
160 outb((psr
& ~PSR_PAGEBITS
) | page
, card
->io
+ N2_PSR
);
167 static void n2_set_iface(port_t
*port
)
169 card_t
*card
= port
->card
;
171 u8 mcr
= inb(io
+ N2_MCR
);
172 u8 msci
= get_msci(port
);
173 u8 rxs
= port
->rxs
& CLK_BRG_MASK
;
174 u8 txs
= port
->txs
& CLK_BRG_MASK
;
176 switch(port
->settings
.clock_type
) {
178 mcr
|= port
->phy_node
? CLOCK_OUT_PORT1
: CLOCK_OUT_PORT0
;
179 rxs
|= CLK_BRG_RX
; /* BRG output */
180 txs
|= CLK_RXCLK_TX
; /* RX clock */
184 mcr
|= port
->phy_node
? CLOCK_OUT_PORT1
: CLOCK_OUT_PORT0
;
185 rxs
|= CLK_LINE_RX
; /* RXC input */
186 txs
|= CLK_BRG_TX
; /* BRG output */
190 mcr
|= port
->phy_node
? CLOCK_OUT_PORT1
: CLOCK_OUT_PORT0
;
191 rxs
|= CLK_LINE_RX
; /* RXC input */
192 txs
|= CLK_RXCLK_TX
; /* RX clock */
195 default: /* Clock EXTernal */
196 mcr
&= port
->phy_node
? ~CLOCK_OUT_PORT1
: ~CLOCK_OUT_PORT0
;
197 rxs
|= CLK_LINE_RX
; /* RXC input */
198 txs
|= CLK_LINE_TX
; /* TXC input */
201 outb(mcr
, io
+ N2_MCR
);
204 sca_out(rxs
, msci
+ RXS
, card
);
205 sca_out(txs
, msci
+ TXS
, card
);
211 static int n2_open(struct net_device
*dev
)
213 port_t
*port
= dev_to_port(dev
);
214 int io
= port
->card
->io
;
215 u8 mcr
= inb(io
+ N2_MCR
) | (port
->phy_node
? TX422_PORT1
:TX422_PORT0
);
218 result
= hdlc_open(dev
);
222 mcr
&= port
->phy_node
? ~DTR_PORT1
: ~DTR_PORT0
; /* set DTR ON */
223 outb(mcr
, io
+ N2_MCR
);
225 outb(inb(io
+ N2_PCR
) | PCR_ENWIN
, io
+ N2_PCR
); /* open window */
226 outb(inb(io
+ N2_PSR
) | PSR_DMAEN
, io
+ N2_PSR
); /* enable dma */
234 static int n2_close(struct net_device
*dev
)
236 port_t
*port
= dev_to_port(dev
);
237 int io
= port
->card
->io
;
238 u8 mcr
= inb(io
+N2_MCR
) | (port
->phy_node
? TX422_PORT1
: TX422_PORT0
);
241 mcr
|= port
->phy_node
? DTR_PORT1
: DTR_PORT0
; /* set DTR OFF */
242 outb(mcr
, io
+ N2_MCR
);
249 static int n2_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
251 const size_t size
= sizeof(sync_serial_settings
);
252 sync_serial_settings new_line
;
253 sync_serial_settings __user
*line
= ifr
->ifr_settings
.ifs_ifsu
.sync
;
254 port_t
*port
= dev_to_port(dev
);
257 if (cmd
== SIOCDEVPRIVATE
) {
262 if (cmd
!= SIOCWANDEV
)
263 return hdlc_ioctl(dev
, ifr
, cmd
);
265 switch(ifr
->ifr_settings
.type
) {
267 ifr
->ifr_settings
.type
= IF_IFACE_SYNC_SERIAL
;
268 if (ifr
->ifr_settings
.size
< size
) {
269 ifr
->ifr_settings
.size
= size
; /* data size wanted */
272 if (copy_to_user(line
, &port
->settings
, size
))
276 case IF_IFACE_SYNC_SERIAL
:
277 if(!capable(CAP_NET_ADMIN
))
280 if (copy_from_user(&new_line
, line
, size
))
283 if (new_line
.clock_type
!= CLOCK_EXT
&&
284 new_line
.clock_type
!= CLOCK_TXFROMRX
&&
285 new_line
.clock_type
!= CLOCK_INT
&&
286 new_line
.clock_type
!= CLOCK_TXINT
)
287 return -EINVAL
; /* No such clock setting */
289 if (new_line
.loopback
!= 0 && new_line
.loopback
!= 1)
292 memcpy(&port
->settings
, &new_line
, size
); /* Update settings */
297 return hdlc_ioctl(dev
, ifr
, cmd
);
303 static void n2_destroy_card(card_t
*card
)
307 for (cnt
= 0; cnt
< 2; cnt
++)
308 if (card
->ports
[cnt
].card
) {
309 struct net_device
*dev
= port_to_dev(&card
->ports
[cnt
]);
310 unregister_hdlc_device(dev
);
314 free_irq(card
->irq
, card
);
317 iounmap(card
->winbase
);
318 release_mem_region(card
->phy_winbase
, USE_WINDOWSIZE
);
322 release_region(card
->io
, N2_IOPORTS
);
323 if (card
->ports
[0].dev
)
324 free_netdev(card
->ports
[0].dev
);
325 if (card
->ports
[1].dev
)
326 free_netdev(card
->ports
[1].dev
);
330 static const struct net_device_ops n2_ops
= {
332 .ndo_stop
= n2_close
,
333 .ndo_change_mtu
= hdlc_change_mtu
,
334 .ndo_start_xmit
= hdlc_start_xmit
,
335 .ndo_do_ioctl
= n2_ioctl
,
338 static int __init
n2_run(unsigned long io
, unsigned long irq
,
339 unsigned long winbase
, long valid0
, long valid1
)
345 if (io
< 0x200 || io
> 0x3FF || (io
% N2_IOPORTS
) != 0) {
346 pr_err("invalid I/O port value\n");
350 if (irq
< 3 || irq
> 15 || irq
== 6) /* FIXME */ {
351 pr_err("invalid IRQ value\n");
355 if (winbase
< 0xA0000 || winbase
> 0xFFFFF || (winbase
& 0xFFF) != 0) {
356 pr_err("invalid RAM value\n");
360 card
= kzalloc(sizeof(card_t
), GFP_KERNEL
);
362 pr_err("unable to allocate memory\n");
366 card
->ports
[0].dev
= alloc_hdlcdev(&card
->ports
[0]);
367 card
->ports
[1].dev
= alloc_hdlcdev(&card
->ports
[1]);
368 if (!card
->ports
[0].dev
|| !card
->ports
[1].dev
) {
369 pr_err("unable to allocate memory\n");
370 n2_destroy_card(card
);
374 if (!request_region(io
, N2_IOPORTS
, devname
)) {
375 pr_err("I/O port region in use\n");
376 n2_destroy_card(card
);
381 if (request_irq(irq
, sca_intr
, 0, devname
, card
)) {
382 pr_err("could not allocate IRQ\n");
383 n2_destroy_card(card
);
388 if (!request_mem_region(winbase
, USE_WINDOWSIZE
, devname
)) {
389 pr_err("could not request RAM window\n");
390 n2_destroy_card(card
);
393 card
->phy_winbase
= winbase
;
394 card
->winbase
= ioremap(winbase
, USE_WINDOWSIZE
);
395 if (!card
->winbase
) {
396 pr_err("ioremap() failed\n");
397 n2_destroy_card(card
);
401 outb(0, io
+ N2_PCR
);
402 outb(winbase
>> 12, io
+ N2_BAR
);
404 switch (USE_WINDOWSIZE
) {
406 outb(WIN16K
, io
+ N2_PSR
);
410 outb(WIN32K
, io
+ N2_PSR
);
414 outb(WIN64K
, io
+ N2_PSR
);
418 pr_err("invalid window size\n");
419 n2_destroy_card(card
);
423 pcr
= PCR_ENWIN
| PCR_VPM
| (USE_BUS16BITS
? PCR_BUS16
: 0);
424 outb(pcr
, io
+ N2_PCR
);
426 card
->ram_size
= sca_detect_ram(card
, card
->winbase
, MAX_RAM_SIZE
);
428 /* number of TX + RX buffers for one port */
429 i
= card
->ram_size
/ ((valid0
+ valid1
) * (sizeof(pkt_desc
) +
432 card
->tx_ring_buffers
= min(i
/ 2, MAX_TX_BUFFERS
);
433 card
->rx_ring_buffers
= i
- card
->tx_ring_buffers
;
435 card
->buff_offset
= (valid0
+ valid1
) * sizeof(pkt_desc
) *
436 (card
->tx_ring_buffers
+ card
->rx_ring_buffers
);
438 pr_info("RISCom/N2 %u KB RAM, IRQ%u, using %u TX + %u RX packets rings\n",
439 card
->ram_size
/ 1024, card
->irq
,
440 card
->tx_ring_buffers
, card
->rx_ring_buffers
);
442 if (card
->tx_ring_buffers
< 1) {
443 pr_err("RAM test failed\n");
444 n2_destroy_card(card
);
448 pcr
|= PCR_RUNSCA
; /* run SCA */
449 outb(pcr
, io
+ N2_PCR
);
450 outb(0, io
+ N2_MCR
);
453 for (cnt
= 0; cnt
< 2; cnt
++) {
454 port_t
*port
= &card
->ports
[cnt
];
455 struct net_device
*dev
= port_to_dev(port
);
456 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
458 if ((cnt
== 0 && !valid0
) || (cnt
== 1 && !valid1
))
461 port
->phy_node
= cnt
;
464 if ((cnt
== 1) && valid0
)
467 spin_lock_init(&port
->lock
);
469 dev
->mem_start
= winbase
;
470 dev
->mem_end
= winbase
+ USE_WINDOWSIZE
- 1;
471 dev
->tx_queue_len
= 50;
472 dev
->netdev_ops
= &n2_ops
;
473 hdlc
->attach
= sca_attach
;
474 hdlc
->xmit
= sca_xmit
;
475 port
->settings
.clock_type
= CLOCK_EXT
;
478 if (register_hdlc_device(dev
)) {
479 pr_warn("unable to register hdlc device\n");
481 n2_destroy_card(card
);
484 sca_init_port(port
); /* Set up SCA memory */
486 netdev_info(dev
, "RISCom/N2 node %d\n", port
->phy_node
);
490 new_card
= &card
->next_card
;
497 static int __init
n2_init(void)
501 pr_info("no card initialized\n");
503 return -EINVAL
; /* no parameters specified, abort */
506 pr_info("%s\n", version
);
509 unsigned long io
, irq
, ram
;
510 long valid
[2] = { 0, 0 }; /* Default = both ports disabled */
512 io
= simple_strtoul(hw
, &hw
, 0);
516 irq
= simple_strtoul(hw
, &hw
, 0);
520 ram
= simple_strtoul(hw
, &hw
, 0);
525 if (*hw
== '0' && !valid
[0])
526 valid
[0] = 1; /* Port 0 enabled */
527 else if (*hw
== '1' && !valid
[1])
528 valid
[1] = 1; /* Port 1 enabled */
534 if (!valid
[0] && !valid
[1])
535 break; /* at least one port must be used */
537 if (*hw
== ':' || *hw
== '\x0')
538 n2_run(io
, irq
, ram
, valid
[0], valid
[1]);
541 return first_card
? 0 : -EINVAL
;
542 }while(*hw
++ == ':');
544 pr_err("invalid hardware parameters\n");
545 return first_card
? 0 : -EINVAL
;
549 static void __exit
n2_cleanup(void)
551 card_t
*card
= first_card
;
555 card
= card
->next_card
;
556 n2_destroy_card(ptr
);
561 module_init(n2_init
);
562 module_exit(n2_cleanup
);
564 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
565 MODULE_DESCRIPTION("RISCom/N2 serial port driver");
566 MODULE_LICENSE("GPL v2");
567 module_param(hw
, charp
, 0444);
568 MODULE_PARM_DESC(hw
, "io,irq,ram,ports:io,irq,...");