2 * Cyclades PC300 synchronous serial card driver for Linux
4 * Copyright (C) 2000-2007 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 HD64572 SCA-II User's Manual
14 * Cyclades PC300 Linux driver
16 * This driver currently supports only PC300/RSV (V.24/V.35) and
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/types.h>
25 #include <linux/fcntl.h>
27 #include <linux/string.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/ioport.h>
31 #include <linux/moduleparam.h>
32 #include <linux/netdevice.h>
33 #include <linux/hdlc.h>
34 #include <linux/pci.h>
35 #include <linux/delay.h>
40 static const char* version
= "Cyclades PC300 driver version: 1.17";
41 static const char* devname
= "PC300";
46 #define PC300_PLX_SIZE 0x80 /* PLX control window size (128 B) */
47 #define PC300_SCA_SIZE 0x400 /* SCA window size (1 KB) */
48 #define ALL_PAGES_ALWAYS_MAPPED
49 #define NEED_DETECT_RAM
50 #define NEED_SCA_MSCI_INTR
51 #define MAX_TX_BUFFERS 10
53 static int pci_clock_freq
= 33000000;
54 static int use_crystal_clock
= 0;
55 static unsigned int CLOCK_BASE
;
57 /* Masks to access the init_ctrl PLX register */
58 #define PC300_CLKSEL_MASK (0x00000004UL)
59 #define PC300_CHMEDIA_MASK(port) (0x00000020UL << ((port) * 3))
60 #define PC300_CTYPE_MASK (0x00000800UL)
63 enum { PC300_RSV
= 1, PC300_X21
, PC300_TE
}; /* card types */
66 * PLX PCI9050-1 local configuration and shared runtime registers.
67 * This structure can be used to access 9050 registers (memory mapped).
70 u32 loc_addr_range
[4]; /* 00-0Ch : Local Address Ranges */
71 u32 loc_rom_range
; /* 10h : Local ROM Range */
72 u32 loc_addr_base
[4]; /* 14-20h : Local Address Base Addrs */
73 u32 loc_rom_base
; /* 24h : Local ROM Base */
74 u32 loc_bus_descr
[4]; /* 28-34h : Local Bus Descriptors */
75 u32 rom_bus_descr
; /* 38h : ROM Bus Descriptor */
76 u32 cs_base
[4]; /* 3C-48h : Chip Select Base Addrs */
77 u32 intr_ctrl_stat
; /* 4Ch : Interrupt Control/Status */
78 u32 init_ctrl
; /* 50h : EEPROM ctrl, Init Ctrl, etc */
83 typedef struct port_s
{
84 struct net_device
*dev
;
86 spinlock_t lock
; /* TX lock */
87 sync_serial_settings settings
;
88 int rxpart
; /* partial frame received, next frame invalid*/
89 unsigned short encoding
;
90 unsigned short parity
;
92 u16 rxin
; /* rx ring buffer 'in' pointer */
93 u16 txin
; /* tx ring buffer 'in' and 'last' pointers */
95 u8 rxs
, txs
, tmc
; /* SCA registers */
96 u8 phy_node
; /* physical port # - 0 or 1 */
101 typedef struct card_s
{
102 int type
; /* RSV, X21, etc. */
103 int n_ports
; /* 1 or 2 ports */
104 u8 __iomem
*rambase
; /* buffer memory base (virtual) */
105 u8 __iomem
*scabase
; /* SCA memory base (virtual) */
106 plx9050 __iomem
*plxbase
; /* PLX registers memory base (virtual) */
107 u32 init_ctrl_value
; /* Saved value - 9050 bug workaround */
108 u16 rx_ring_buffers
; /* number of buffers in a ring */
110 u16 buff_offset
; /* offset of first buffer of first channel */
111 u8 irq
; /* interrupt request level */
117 #define sca_in(reg, card) readb(card->scabase + (reg))
118 #define sca_out(value, reg, card) writeb(value, card->scabase + (reg))
119 #define sca_inw(reg, card) readw(card->scabase + (reg))
120 #define sca_outw(value, reg, card) writew(value, card->scabase + (reg))
121 #define sca_inl(reg, card) readl(card->scabase + (reg))
122 #define sca_outl(value, reg, card) writel(value, card->scabase + (reg))
124 #define port_to_card(port) (port->card)
125 #define log_node(port) (port->phy_node)
126 #define phy_node(port) (port->phy_node)
127 #define winbase(card) (card->rambase)
128 #define get_port(card, port) ((port) < (card)->n_ports ? \
129 (&(card)->ports[port]) : (NULL))
134 static void pc300_set_iface(port_t
*port
)
136 card_t
*card
= port
->card
;
137 u32 __iomem
* init_ctrl
= &card
->plxbase
->init_ctrl
;
138 u16 msci
= get_msci(port
);
139 u8 rxs
= port
->rxs
& CLK_BRG_MASK
;
140 u8 txs
= port
->txs
& CLK_BRG_MASK
;
142 sca_out(EXS_TES1
, (phy_node(port
) ? MSCI1_OFFSET
: MSCI0_OFFSET
) + EXS
,
144 switch(port
->settings
.clock_type
) {
146 rxs
|= CLK_BRG
; /* BRG output */
147 txs
|= CLK_PIN_OUT
| CLK_TX_RXCLK
; /* RX clock */
151 rxs
|= CLK_LINE
; /* RXC input */
152 txs
|= CLK_PIN_OUT
| CLK_BRG
; /* BRG output */
156 rxs
|= CLK_LINE
; /* RXC input */
157 txs
|= CLK_PIN_OUT
| CLK_TX_RXCLK
; /* RX clock */
160 default: /* EXTernal clock */
161 rxs
|= CLK_LINE
; /* RXC input */
162 txs
|= CLK_PIN_OUT
| CLK_LINE
; /* TXC input */
168 sca_out(rxs
, msci
+ RXS
, card
);
169 sca_out(txs
, msci
+ TXS
, card
);
172 if (port
->card
->type
== PC300_RSV
) {
173 if (port
->iface
== IF_IFACE_V35
)
174 writel(card
->init_ctrl_value
|
175 PC300_CHMEDIA_MASK(port
->phy_node
), init_ctrl
);
177 writel(card
->init_ctrl_value
&
178 ~PC300_CHMEDIA_MASK(port
->phy_node
), init_ctrl
);
184 static int pc300_open(struct net_device
*dev
)
186 port_t
*port
= dev_to_port(dev
);
188 int result
= hdlc_open(dev
);
193 pc300_set_iface(port
);
199 static int pc300_close(struct net_device
*dev
)
208 static int pc300_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
210 const size_t size
= sizeof(sync_serial_settings
);
211 sync_serial_settings new_line
;
212 sync_serial_settings __user
*line
= ifr
->ifr_settings
.ifs_ifsu
.sync
;
214 port_t
*port
= dev_to_port(dev
);
217 if (cmd
== SIOCDEVPRIVATE
) {
222 if (cmd
!= SIOCWANDEV
)
223 return hdlc_ioctl(dev
, ifr
, cmd
);
225 if (ifr
->ifr_settings
.type
== IF_GET_IFACE
) {
226 ifr
->ifr_settings
.type
= port
->iface
;
227 if (ifr
->ifr_settings
.size
< size
) {
228 ifr
->ifr_settings
.size
= size
; /* data size wanted */
231 if (copy_to_user(line
, &port
->settings
, size
))
237 if (port
->card
->type
== PC300_X21
&&
238 (ifr
->ifr_settings
.type
== IF_IFACE_SYNC_SERIAL
||
239 ifr
->ifr_settings
.type
== IF_IFACE_X21
))
240 new_type
= IF_IFACE_X21
;
242 else if (port
->card
->type
== PC300_RSV
&&
243 (ifr
->ifr_settings
.type
== IF_IFACE_SYNC_SERIAL
||
244 ifr
->ifr_settings
.type
== IF_IFACE_V35
))
245 new_type
= IF_IFACE_V35
;
247 else if (port
->card
->type
== PC300_RSV
&&
248 ifr
->ifr_settings
.type
== IF_IFACE_V24
)
249 new_type
= IF_IFACE_V24
;
252 return hdlc_ioctl(dev
, ifr
, cmd
);
254 if (!capable(CAP_NET_ADMIN
))
257 if (copy_from_user(&new_line
, line
, size
))
260 if (new_line
.clock_type
!= CLOCK_EXT
&&
261 new_line
.clock_type
!= CLOCK_TXFROMRX
&&
262 new_line
.clock_type
!= CLOCK_INT
&&
263 new_line
.clock_type
!= CLOCK_TXINT
)
264 return -EINVAL
; /* No such clock setting */
266 if (new_line
.loopback
!= 0 && new_line
.loopback
!= 1)
269 memcpy(&port
->settings
, &new_line
, size
); /* Update settings */
270 port
->iface
= new_type
;
271 pc300_set_iface(port
);
277 static void pc300_pci_remove_one(struct pci_dev
*pdev
)
280 card_t
*card
= pci_get_drvdata(pdev
);
282 for (i
= 0; i
< 2; i
++)
283 if (card
->ports
[i
].card
) {
284 struct net_device
*dev
= port_to_dev(&card
->ports
[i
]);
285 unregister_hdlc_device(dev
);
289 free_irq(card
->irq
, card
);
292 iounmap(card
->rambase
);
294 iounmap(card
->scabase
);
296 iounmap(card
->plxbase
);
298 pci_release_regions(pdev
);
299 pci_disable_device(pdev
);
300 pci_set_drvdata(pdev
, NULL
);
301 if (card
->ports
[0].dev
)
302 free_netdev(card
->ports
[0].dev
);
303 if (card
->ports
[1].dev
)
304 free_netdev(card
->ports
[1].dev
);
310 static int __devinit
pc300_pci_init_one(struct pci_dev
*pdev
,
311 const struct pci_device_id
*ent
)
317 u32 ramphys
; /* buffer memory base */
318 u32 scaphys
; /* SCA memory base */
319 u32 plxphys
; /* PLX registers memory base */
322 static int printed_version
;
323 if (!printed_version
++)
324 printk(KERN_INFO
"%s\n", version
);
327 i
= pci_enable_device(pdev
);
331 i
= pci_request_regions(pdev
, "PC300");
333 pci_disable_device(pdev
);
337 card
= kzalloc(sizeof(card_t
), GFP_KERNEL
);
339 printk(KERN_ERR
"pc300: unable to allocate memory\n");
340 pci_release_regions(pdev
);
341 pci_disable_device(pdev
);
344 pci_set_drvdata(pdev
, card
);
346 if (pdev
->device
== PCI_DEVICE_ID_PC300_TE_1
||
347 pdev
->device
== PCI_DEVICE_ID_PC300_TE_2
)
348 card
->type
= PC300_TE
; /* not fully supported */
349 else if (card
->init_ctrl_value
& PC300_CTYPE_MASK
)
350 card
->type
= PC300_X21
;
352 card
->type
= PC300_RSV
;
354 if (pdev
->device
== PCI_DEVICE_ID_PC300_RX_1
||
355 pdev
->device
== PCI_DEVICE_ID_PC300_TE_1
)
360 for (i
= 0; i
< card
->n_ports
; i
++)
361 if (!(card
->ports
[i
].dev
= alloc_hdlcdev(&card
->ports
[i
]))) {
362 printk(KERN_ERR
"pc300: unable to allocate memory\n");
363 pc300_pci_remove_one(pdev
);
367 if (pci_resource_len(pdev
, 0) != PC300_PLX_SIZE
||
368 pci_resource_len(pdev
, 2) != PC300_SCA_SIZE
||
369 pci_resource_len(pdev
, 3) < 16384) {
370 printk(KERN_ERR
"pc300: invalid card EEPROM parameters\n");
371 pc300_pci_remove_one(pdev
);
375 plxphys
= pci_resource_start(pdev
,0) & PCI_BASE_ADDRESS_MEM_MASK
;
376 card
->plxbase
= ioremap(plxphys
, PC300_PLX_SIZE
);
378 scaphys
= pci_resource_start(pdev
,2) & PCI_BASE_ADDRESS_MEM_MASK
;
379 card
->scabase
= ioremap(scaphys
, PC300_SCA_SIZE
);
381 ramphys
= pci_resource_start(pdev
,3) & PCI_BASE_ADDRESS_MEM_MASK
;
382 card
->rambase
= ioremap(ramphys
, pci_resource_len(pdev
,3));
384 if (card
->plxbase
== NULL
||
385 card
->scabase
== NULL
||
386 card
->rambase
== NULL
) {
387 printk(KERN_ERR
"pc300: ioremap() failed\n");
388 pc300_pci_remove_one(pdev
);
391 /* PLX PCI 9050 workaround for local configuration register read bug */
392 pci_write_config_dword(pdev
, PCI_BASE_ADDRESS_0
, scaphys
);
393 card
->init_ctrl_value
= readl(&((plx9050 __iomem
*)card
->scabase
)->init_ctrl
);
394 pci_write_config_dword(pdev
, PCI_BASE_ADDRESS_0
, plxphys
);
397 p
= &card
->plxbase
->init_ctrl
;
398 writel(card
->init_ctrl_value
| 0x40000000, p
);
399 readl(p
); /* Flush the write - do not use sca_flush */
402 writel(card
->init_ctrl_value
, p
);
403 readl(p
); /* Flush the write - do not use sca_flush */
406 /* Reload Config. Registers from EEPROM */
407 writel(card
->init_ctrl_value
| 0x20000000, p
);
408 readl(p
); /* Flush the write - do not use sca_flush */
411 writel(card
->init_ctrl_value
, p
);
412 readl(p
); /* Flush the write - do not use sca_flush */
415 ramsize
= sca_detect_ram(card
, card
->rambase
,
416 pci_resource_len(pdev
, 3));
418 if (use_crystal_clock
)
419 card
->init_ctrl_value
&= ~PC300_CLKSEL_MASK
;
421 card
->init_ctrl_value
|= PC300_CLKSEL_MASK
;
423 writel(card
->init_ctrl_value
, &card
->plxbase
->init_ctrl
);
424 /* number of TX + RX buffers for one port */
425 i
= ramsize
/ (card
->n_ports
* (sizeof(pkt_desc
) + HDLC_MAX_MRU
));
426 card
->tx_ring_buffers
= min(i
/ 2, MAX_TX_BUFFERS
);
427 card
->rx_ring_buffers
= i
- card
->tx_ring_buffers
;
429 card
->buff_offset
= card
->n_ports
* sizeof(pkt_desc
) *
430 (card
->tx_ring_buffers
+ card
->rx_ring_buffers
);
432 printk(KERN_INFO
"pc300: PC300/%s, %u KB RAM at 0x%x, IRQ%u, "
433 "using %u TX + %u RX packets rings\n",
434 card
->type
== PC300_X21
? "X21" :
435 card
->type
== PC300_TE
? "TE" : "RSV",
436 ramsize
/ 1024, ramphys
, pdev
->irq
,
437 card
->tx_ring_buffers
, card
->rx_ring_buffers
);
439 if (card
->tx_ring_buffers
< 1) {
440 printk(KERN_ERR
"pc300: RAM test failed\n");
441 pc300_pci_remove_one(pdev
);
445 /* Enable interrupts on the PCI bridge, LINTi1 active low */
446 writew(0x0041, &card
->plxbase
->intr_ctrl_stat
);
449 if (request_irq(pdev
->irq
, sca_intr
, IRQF_SHARED
, devname
, card
)) {
450 printk(KERN_WARNING
"pc300: could not allocate IRQ%d.\n",
452 pc300_pci_remove_one(pdev
);
455 card
->irq
= pdev
->irq
;
459 // COTE not set - allows better TX DMA settings
460 // sca_out(sca_in(PCR, card) | PCR_COTE, PCR, card);
462 sca_out(0x10, BTCR
, card
);
464 for (i
= 0; i
< card
->n_ports
; i
++) {
465 port_t
*port
= &card
->ports
[i
];
466 struct net_device
*dev
= port_to_dev(port
);
467 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
470 spin_lock_init(&port
->lock
);
471 dev
->irq
= card
->irq
;
472 dev
->mem_start
= ramphys
;
473 dev
->mem_end
= ramphys
+ ramsize
- 1;
474 dev
->tx_queue_len
= 50;
475 dev
->do_ioctl
= pc300_ioctl
;
476 dev
->open
= pc300_open
;
477 dev
->stop
= pc300_close
;
478 hdlc
->attach
= sca_attach
;
479 hdlc
->xmit
= sca_xmit
;
480 port
->settings
.clock_type
= CLOCK_EXT
;
482 if (card
->type
== PC300_X21
)
483 port
->iface
= IF_IFACE_X21
;
485 port
->iface
= IF_IFACE_V35
;
487 if (register_hdlc_device(dev
)) {
488 printk(KERN_ERR
"pc300: unable to register hdlc "
491 pc300_pci_remove_one(pdev
);
494 sca_init_sync_port(port
); /* Set up SCA memory */
496 printk(KERN_INFO
"%s: PC300 node %d\n",
497 dev
->name
, port
->phy_node
);
504 static struct pci_device_id pc300_pci_tbl
[] __devinitdata
= {
505 { PCI_VENDOR_ID_CYCLADES
, PCI_DEVICE_ID_PC300_RX_1
, PCI_ANY_ID
,
506 PCI_ANY_ID
, 0, 0, 0 },
507 { PCI_VENDOR_ID_CYCLADES
, PCI_DEVICE_ID_PC300_RX_2
, PCI_ANY_ID
,
508 PCI_ANY_ID
, 0, 0, 0 },
509 { PCI_VENDOR_ID_CYCLADES
, PCI_DEVICE_ID_PC300_TE_1
, PCI_ANY_ID
,
510 PCI_ANY_ID
, 0, 0, 0 },
511 { PCI_VENDOR_ID_CYCLADES
, PCI_DEVICE_ID_PC300_TE_2
, PCI_ANY_ID
,
512 PCI_ANY_ID
, 0, 0, 0 },
517 static struct pci_driver pc300_pci_driver
= {
519 .id_table
= pc300_pci_tbl
,
520 .probe
= pc300_pci_init_one
,
521 .remove
= pc300_pci_remove_one
,
525 static int __init
pc300_init_module(void)
528 printk(KERN_INFO
"%s\n", version
);
530 if (pci_clock_freq
< 1000000 || pci_clock_freq
> 80000000) {
531 printk(KERN_ERR
"pc300: Invalid PCI clock frequency\n");
534 if (use_crystal_clock
!= 0 && use_crystal_clock
!= 1) {
535 printk(KERN_ERR
"pc300: Invalid 'use_crystal_clock' value\n");
539 CLOCK_BASE
= use_crystal_clock
? 24576000 : pci_clock_freq
;
541 return pci_register_driver(&pc300_pci_driver
);
546 static void __exit
pc300_cleanup_module(void)
548 pci_unregister_driver(&pc300_pci_driver
);
551 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
552 MODULE_DESCRIPTION("Cyclades PC300 serial port driver");
553 MODULE_LICENSE("GPL v2");
554 MODULE_DEVICE_TABLE(pci
, pc300_pci_tbl
);
555 module_param(pci_clock_freq
, int, 0444);
556 MODULE_PARM_DESC(pci_clock_freq
, "System PCI clock frequency in Hz");
557 module_param(use_crystal_clock
, int, 0444);
558 MODULE_PARM_DESC(use_crystal_clock
,
559 "Use 24.576 MHz clock instead of PCI clock");
560 module_init(pc300_init_module
);
561 module_exit(pc300_cleanup_module
);