2 * Moxa C101 synchronous serial card driver for Linux
4 * Copyright (C) 2000-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 * Sources of information:
13 * Hitachi HD64570 SCA User's Manual
14 * Moxa C101 User's Manual
17 #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/string.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/moduleparam.h>
28 #include <linux/netdevice.h>
29 #include <linux/hdlc.h>
30 #include <linux/delay.h>
36 static const char* version
= "Moxa C101 driver version: 1.15";
37 static const char* devname
= "C101";
42 #define C101_PAGE 0x1D00
43 #define C101_DTR 0x1E00
44 #define C101_SCA 0x1F00
45 #define C101_WINDOW_SIZE 0x2000
46 #define C101_MAPPED_RAM_SIZE 0x4000
48 #define RAM_SIZE (256 * 1024)
49 #define TX_RING_BUFFERS 10
50 #define RX_RING_BUFFERS ((RAM_SIZE - C101_WINDOW_SIZE) / \
51 (sizeof(pkt_desc) + HDLC_MAX_MRU) - TX_RING_BUFFERS)
53 #define CLOCK_BASE 9830400 /* 9.8304 MHz */
54 #define PAGE0_ALWAYS_MAPPED
56 static char *hw
; /* pointer to hw=xxx command line string */
59 typedef struct card_s
{
60 struct net_device
*dev
;
61 spinlock_t lock
; /* TX lock */
62 u8 __iomem
*win0base
; /* ISA window base address */
63 u32 phy_winbase
; /* ISA physical base address */
64 sync_serial_settings settings
;
65 int rxpart
; /* partial frame received, next frame invalid*/
66 unsigned short encoding
;
67 unsigned short parity
;
68 u16 rx_ring_buffers
; /* number of buffers in a ring */
70 u16 buff_offset
; /* offset of first buffer of first channel */
71 u16 rxin
; /* rx ring buffer 'in' pointer */
72 u16 txin
; /* tx ring buffer 'in' and 'last' pointers */
74 u8 rxs
, txs
, tmc
; /* SCA registers */
75 u8 irq
; /* IRQ (3-15) */
78 struct card_s
*next_card
;
81 typedef card_t port_t
;
83 static card_t
*first_card
;
84 static card_t
**new_card
= &first_card
;
87 #define sca_in(reg, card) readb((card)->win0base + C101_SCA + (reg))
88 #define sca_out(value, reg, card) writeb(value, (card)->win0base + C101_SCA + (reg))
89 #define sca_inw(reg, card) readw((card)->win0base + C101_SCA + (reg))
91 /* EDA address register must be set in EDAL, EDAH order - 8 bit ISA bus */
92 #define sca_outw(value, reg, card) do { \
93 writeb(value & 0xFF, (card)->win0base + C101_SCA + (reg)); \
94 writeb((value >> 8 ) & 0xFF, (card)->win0base + C101_SCA + (reg + 1));\
97 #define port_to_card(port) (port)
98 #define log_node(port) (0)
99 #define phy_node(port) (0)
100 #define winsize(card) (C101_WINDOW_SIZE)
101 #define win0base(card) ((card)->win0base)
102 #define winbase(card) ((card)->win0base + 0x2000)
103 #define get_port(card, port) (card)
104 static void sca_msci_intr(port_t
*port
);
107 static inline u8
sca_get_page(card_t
*card
)
112 static inline void openwin(card_t
*card
, u8 page
)
115 writeb(page
, card
->win0base
+ C101_PAGE
);
122 static inline void set_carrier(port_t
*port
)
124 if (!(sca_in(MSCI1_OFFSET
+ ST3
, port
) & ST3_DCD
))
125 netif_carrier_on(port_to_dev(port
));
127 netif_carrier_off(port_to_dev(port
));
131 static void sca_msci_intr(port_t
*port
)
133 u8 stat
= sca_in(MSCI0_OFFSET
+ ST1
, port
); /* read MSCI ST1 status */
135 /* Reset MSCI TX underrun and CDCD (ignored) status bit */
136 sca_out(stat
& (ST1_UDRN
| ST1_CDCD
), MSCI0_OFFSET
+ ST1
, port
);
138 if (stat
& ST1_UDRN
) {
139 /* TX Underrun error detected */
140 port_to_dev(port
)->stats
.tx_errors
++;
141 port_to_dev(port
)->stats
.tx_fifo_errors
++;
144 stat
= sca_in(MSCI1_OFFSET
+ ST1
, port
); /* read MSCI1 ST1 status */
145 /* Reset MSCI CDCD status bit - uses ch#2 DCD input */
146 sca_out(stat
& ST1_CDCD
, MSCI1_OFFSET
+ ST1
, port
);
153 static void c101_set_iface(port_t
*port
)
155 u8 rxs
= port
->rxs
& CLK_BRG_MASK
;
156 u8 txs
= port
->txs
& CLK_BRG_MASK
;
158 switch(port
->settings
.clock_type
) {
160 rxs
|= CLK_BRG_RX
; /* TX clock */
161 txs
|= CLK_RXCLK_TX
; /* BRG output */
165 rxs
|= CLK_LINE_RX
; /* RXC input */
166 txs
|= CLK_BRG_TX
; /* BRG output */
170 rxs
|= CLK_LINE_RX
; /* RXC input */
171 txs
|= CLK_RXCLK_TX
; /* RX clock */
174 default: /* EXTernal clock */
175 rxs
|= CLK_LINE_RX
; /* RXC input */
176 txs
|= CLK_LINE_TX
; /* TXC input */
181 sca_out(rxs
, MSCI1_OFFSET
+ RXS
, port
);
182 sca_out(txs
, MSCI1_OFFSET
+ TXS
, port
);
187 static int c101_open(struct net_device
*dev
)
189 port_t
*port
= dev_to_port(dev
);
192 result
= hdlc_open(dev
);
196 writeb(1, port
->win0base
+ C101_DTR
);
197 sca_out(0, MSCI1_OFFSET
+ CTL
, port
); /* RTS uses ch#2 output */
199 /* DCD is connected to port 2 !@#$%^& - disable MSCI0 CDCD interrupt */
200 sca_out(IE1_UDRN
, MSCI0_OFFSET
+ IE1
, port
);
201 sca_out(IE0_TXINT
, MSCI0_OFFSET
+ IE0
, port
);
205 /* enable MSCI1 CDCD interrupt */
206 sca_out(IE1_CDCD
, MSCI1_OFFSET
+ IE1
, port
);
207 sca_out(IE0_RXINTA
, MSCI1_OFFSET
+ IE0
, port
);
208 sca_out(0x48, IER0
, port
); /* TXINT #0 and RXINT #1 */
209 c101_set_iface(port
);
214 static int c101_close(struct net_device
*dev
)
216 port_t
*port
= dev_to_port(dev
);
219 writeb(0, port
->win0base
+ C101_DTR
);
220 sca_out(CTL_NORTS
, MSCI1_OFFSET
+ CTL
, port
);
226 static int c101_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
228 const size_t size
= sizeof(sync_serial_settings
);
229 sync_serial_settings new_line
;
230 sync_serial_settings __user
*line
= ifr
->ifr_settings
.ifs_ifsu
.sync
;
231 port_t
*port
= dev_to_port(dev
);
234 if (cmd
== SIOCDEVPRIVATE
) {
236 printk(KERN_DEBUG
"MSCI1: ST: %02x %02x %02x %02x\n",
237 sca_in(MSCI1_OFFSET
+ ST0
, port
),
238 sca_in(MSCI1_OFFSET
+ ST1
, port
),
239 sca_in(MSCI1_OFFSET
+ ST2
, port
),
240 sca_in(MSCI1_OFFSET
+ ST3
, port
));
244 if (cmd
!= SIOCWANDEV
)
245 return hdlc_ioctl(dev
, ifr
, cmd
);
247 switch(ifr
->ifr_settings
.type
) {
249 ifr
->ifr_settings
.type
= IF_IFACE_SYNC_SERIAL
;
250 if (ifr
->ifr_settings
.size
< size
) {
251 ifr
->ifr_settings
.size
= size
; /* data size wanted */
254 if (copy_to_user(line
, &port
->settings
, size
))
258 case IF_IFACE_SYNC_SERIAL
:
259 if(!capable(CAP_NET_ADMIN
))
262 if (copy_from_user(&new_line
, line
, size
))
265 if (new_line
.clock_type
!= CLOCK_EXT
&&
266 new_line
.clock_type
!= CLOCK_TXFROMRX
&&
267 new_line
.clock_type
!= CLOCK_INT
&&
268 new_line
.clock_type
!= CLOCK_TXINT
)
269 return -EINVAL
; /* No such clock setting */
271 if (new_line
.loopback
!= 0 && new_line
.loopback
!= 1)
274 memcpy(&port
->settings
, &new_line
, size
); /* Update settings */
275 c101_set_iface(port
);
279 return hdlc_ioctl(dev
, ifr
, cmd
);
285 static void c101_destroy_card(card_t
*card
)
287 readb(card
->win0base
+ C101_PAGE
); /* Resets SCA? */
290 free_irq(card
->irq
, card
);
292 if (card
->win0base
) {
293 iounmap(card
->win0base
);
294 release_mem_region(card
->phy_winbase
, C101_MAPPED_RAM_SIZE
);
297 free_netdev(card
->dev
);
302 static const struct net_device_ops c101_ops
= {
303 .ndo_open
= c101_open
,
304 .ndo_stop
= c101_close
,
305 .ndo_start_xmit
= hdlc_start_xmit
,
306 .ndo_do_ioctl
= c101_ioctl
,
309 static int __init
c101_run(unsigned long irq
, unsigned long winbase
)
311 struct net_device
*dev
;
316 if (irq
<3 || irq
>15 || irq
== 6) /* FIXME */ {
317 pr_err("invalid IRQ value\n");
321 if (winbase
< 0xC0000 || winbase
> 0xDFFFF || (winbase
& 0x3FFF) !=0) {
322 pr_err("invalid RAM value\n");
326 card
= kzalloc(sizeof(card_t
), GFP_KERNEL
);
330 card
->dev
= alloc_hdlcdev(card
);
332 pr_err("unable to allocate memory\n");
337 if (request_irq(irq
, sca_intr
, 0, devname
, card
)) {
338 pr_err("could not allocate IRQ\n");
339 c101_destroy_card(card
);
344 if (!request_mem_region(winbase
, C101_MAPPED_RAM_SIZE
, devname
)) {
345 pr_err("could not request RAM window\n");
346 c101_destroy_card(card
);
349 card
->phy_winbase
= winbase
;
350 card
->win0base
= ioremap(winbase
, C101_MAPPED_RAM_SIZE
);
351 if (!card
->win0base
) {
352 pr_err("could not map I/O address\n");
353 c101_destroy_card(card
);
357 card
->tx_ring_buffers
= TX_RING_BUFFERS
;
358 card
->rx_ring_buffers
= RX_RING_BUFFERS
;
359 card
->buff_offset
= C101_WINDOW_SIZE
; /* Bytes 1D00-1FFF reserved */
361 readb(card
->win0base
+ C101_PAGE
); /* Resets SCA? */
363 writeb(0, card
->win0base
+ C101_PAGE
);
364 writeb(0, card
->win0base
+ C101_DTR
); /* Power-up for RAM? */
368 dev
= port_to_dev(card
);
369 hdlc
= dev_to_hdlc(dev
);
371 spin_lock_init(&card
->lock
);
373 dev
->mem_start
= winbase
;
374 dev
->mem_end
= winbase
+ C101_MAPPED_RAM_SIZE
- 1;
375 dev
->tx_queue_len
= 50;
376 dev
->netdev_ops
= &c101_ops
;
377 hdlc
->attach
= sca_attach
;
378 hdlc
->xmit
= sca_xmit
;
379 card
->settings
.clock_type
= CLOCK_EXT
;
381 result
= register_hdlc_device(dev
);
383 pr_warn("unable to register hdlc device\n");
384 c101_destroy_card(card
);
388 sca_init_port(card
); /* Set up C101 memory */
391 netdev_info(dev
, "Moxa C101 on IRQ%u, using %u TX + %u RX packets rings\n",
392 card
->irq
, card
->tx_ring_buffers
, card
->rx_ring_buffers
);
395 new_card
= &card
->next_card
;
401 static int __init
c101_init(void)
405 pr_info("no card initialized\n");
407 return -EINVAL
; /* no parameters specified, abort */
410 pr_info("%s\n", version
);
413 unsigned long irq
, ram
;
415 irq
= simple_strtoul(hw
, &hw
, 0);
419 ram
= simple_strtoul(hw
, &hw
, 0);
421 if (*hw
== ':' || *hw
== '\x0')
425 return first_card
? 0 : -EINVAL
;
426 }while(*hw
++ == ':');
428 pr_err("invalid hardware parameters\n");
429 return first_card
? 0 : -EINVAL
;
433 static void __exit
c101_cleanup(void)
435 card_t
*card
= first_card
;
439 card
= card
->next_card
;
440 unregister_hdlc_device(port_to_dev(ptr
));
441 c101_destroy_card(ptr
);
446 module_init(c101_init
);
447 module_exit(c101_cleanup
);
449 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
450 MODULE_DESCRIPTION("Moxa C101 serial port driver");
451 MODULE_LICENSE("GPL v2");
452 module_param(hw
, charp
, 0444);
453 MODULE_PARM_DESC(hw
, "irq,ram:irq,...");