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)
318 u32 ramphys; /* buffer memory base */
319 u32 scaphys; /* SCA memory base */
320 u32 plxphys; /* PLX registers memory base */
323 static int printed_version;
324 if (!printed_version++)
325 printk(KERN_INFO "%s\n", version);
328 i = pci_enable_device(pdev);
332 i = pci_request_regions(pdev, "PC300");
334 pci_disable_device(pdev);
338 card = kmalloc(sizeof(card_t), GFP_KERNEL);
340 printk(KERN_ERR "pc300: unable to allocate memory\n");
341 pci_release_regions(pdev);
342 pci_disable_device(pdev);
345 memset(card, 0, sizeof(card_t));
346 pci_set_drvdata(pdev, card);
348 if (pdev->device == PCI_DEVICE_ID_PC300_TE_1 ||
349 pdev->device == PCI_DEVICE_ID_PC300_TE_2)
350 card->type = PC300_TE; /* not fully supported */
351 else if (card->init_ctrl_value & PC300_CTYPE_MASK)
352 card->type = PC300_X21;
354 card->type = PC300_RSV;
356 if (pdev->device == PCI_DEVICE_ID_PC300_RX_1 ||
357 pdev->device == PCI_DEVICE_ID_PC300_TE_1)
362 for (i = 0; i < card->n_ports; i++)
363 if (!(card->ports[i].dev = alloc_hdlcdev(&card->ports[i]))) {
364 printk(KERN_ERR "pc300: unable to allocate memory\n");
365 pc300_pci_remove_one(pdev);
369 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
370 if (pci_resource_len(pdev, 0) != PC300_PLX_SIZE ||
371 pci_resource_len(pdev, 2) != PC300_SCA_SIZE ||
372 pci_resource_len(pdev, 3) < 16384) {
373 printk(KERN_ERR "pc300: invalid card EEPROM parameters\n");
374 pc300_pci_remove_one(pdev);
378 plxphys = pci_resource_start(pdev,0) & PCI_BASE_ADDRESS_MEM_MASK;
379 card->plxbase = ioremap(plxphys, PC300_PLX_SIZE);
381 scaphys = pci_resource_start(pdev,2) & PCI_BASE_ADDRESS_MEM_MASK;
382 card->scabase = ioremap(scaphys, PC300_SCA_SIZE);
384 ramphys = pci_resource_start(pdev,3) & PCI_BASE_ADDRESS_MEM_MASK;
385 card->rambase = ioremap(ramphys, pci_resource_len(pdev,3));
387 if (card->plxbase == NULL ||
388 card->scabase == NULL ||
389 card->rambase == NULL) {
390 printk(KERN_ERR "pc300: ioremap() failed\n");
391 pc300_pci_remove_one(pdev);
394 /* PLX PCI 9050 workaround for local configuration register read bug */
395 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, scaphys);
396 card->init_ctrl_value = readl(&((plx9050 __iomem *)card->scabase)->init_ctrl);
397 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, plxphys);
400 p = &card->plxbase->init_ctrl;
401 writel(card->init_ctrl_value | 0x40000000, p);
402 readl(p); /* Flush the write - do not use sca_flush */
405 writel(card->init_ctrl_value, p);
406 readl(p); /* Flush the write - do not use sca_flush */
409 /* Reload Config. Registers from EEPROM */
410 writel(card->init_ctrl_value | 0x20000000, p);
411 readl(p); /* Flush the write - do not use sca_flush */
414 writel(card->init_ctrl_value, p);
415 readl(p); /* Flush the write - do not use sca_flush */
418 ramsize = sca_detect_ram(card, card->rambase,
419 pci_resource_len(pdev, 3));
421 if (use_crystal_clock)
422 card->init_ctrl_value &= ~PC300_CLKSEL_MASK;
424 card->init_ctrl_value |= PC300_CLKSEL_MASK;
426 writel(card->init_ctrl_value, &card->plxbase->init_ctrl);
427 /* number of TX + RX buffers for one port */
428 i = ramsize / (card->n_ports * (sizeof(pkt_desc) + HDLC_MAX_MRU));
429 card->tx_ring_buffers = min(i / 2, MAX_TX_BUFFERS);
430 card->rx_ring_buffers = i - card->tx_ring_buffers;
432 card->buff_offset = card->n_ports * sizeof(pkt_desc) *
433 (card->tx_ring_buffers + card->rx_ring_buffers);
435 printk(KERN_INFO "pc300: PC300/%s, %u KB RAM at 0x%x, IRQ%u, "
436 "using %u TX + %u RX packets rings\n",
437 card->type == PC300_X21 ? "X21" :
438 card->type == PC300_TE ? "TE" : "RSV",
439 ramsize / 1024, ramphys, pdev->irq,
440 card->tx_ring_buffers, card->rx_ring_buffers);
442 if (card->tx_ring_buffers < 1) {
443 printk(KERN_ERR "pc300: RAM test failed\n");
444 pc300_pci_remove_one(pdev);
448 /* Enable interrupts on the PCI bridge, LINTi1 active low */
449 writew(0x0041, &card->plxbase->intr_ctrl_stat);
452 if (request_irq(pdev->irq, sca_intr, IRQF_SHARED, devname, card)) {
453 printk(KERN_WARNING "pc300: could not allocate IRQ%d.\n",
455 pc300_pci_remove_one(pdev);
458 card->irq = pdev->irq;
462 // COTE not set - allows better TX DMA settings
463 // sca_out(sca_in(PCR, card) | PCR_COTE, PCR, card);
465 sca_out(0x10, BTCR, card);
467 for (i = 0; i < card->n_ports; i++) {
468 port_t *port = &card->ports[i];
469 struct net_device *dev = port_to_dev(port);
470 hdlc_device *hdlc = dev_to_hdlc(dev);
473 spin_lock_init(&port->lock);
474 SET_MODULE_OWNER(dev);
475 dev->irq = card->irq;
476 dev->mem_start = ramphys;
477 dev->mem_end = ramphys + ramsize - 1;
478 dev->tx_queue_len = 50;
479 dev->do_ioctl = pc300_ioctl;
480 dev->open = pc300_open;
481 dev->stop = pc300_close;
482 hdlc->attach = sca_attach;
483 hdlc->xmit = sca_xmit;
484 port->settings.clock_type = CLOCK_EXT;
486 if (card->type == PC300_X21)
487 port->iface = IF_IFACE_X21;
489 port->iface = IF_IFACE_V35;
491 if (register_hdlc_device(dev)) {
492 printk(KERN_ERR "pc300: unable to register hdlc "
495 pc300_pci_remove_one(pdev);
498 sca_init_sync_port(port); /* Set up SCA memory */
500 printk(KERN_INFO "%s: PC300 node %d\n",
501 dev->name, port->phy_node);
508 static struct pci_device_id pc300_pci_tbl[] __devinitdata = {
509 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_RX_1, PCI_ANY_ID,
510 PCI_ANY_ID, 0, 0, 0 },
511 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_RX_2, PCI_ANY_ID,
512 PCI_ANY_ID, 0, 0, 0 },
513 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_TE_1, PCI_ANY_ID,
514 PCI_ANY_ID, 0, 0, 0 },
515 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_TE_2, PCI_ANY_ID,
516 PCI_ANY_ID, 0, 0, 0 },
521 static struct pci_driver pc300_pci_driver = {
523 .id_table = pc300_pci_tbl,
524 .probe = pc300_pci_init_one,
525 .remove = pc300_pci_remove_one,
529 static int __init pc300_init_module(void)
532 printk(KERN_INFO "%s\n", version);
534 if (pci_clock_freq < 1000000 || pci_clock_freq > 80000000) {
535 printk(KERN_ERR "pc300: Invalid PCI clock frequency\n");
538 if (use_crystal_clock != 0 && use_crystal_clock != 1) {
539 printk(KERN_ERR "pc300: Invalid 'use_crystal_clock' value\n");
543 CLOCK_BASE = use_crystal_clock ? 24576000 : pci_clock_freq;
545 return pci_register_driver(&pc300_pci_driver);
550 static void __exit pc300_cleanup_module(void)
552 pci_unregister_driver(&pc300_pci_driver);
555 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
556 MODULE_DESCRIPTION("Cyclades PC300 serial port driver");
557 MODULE_LICENSE("GPL v2");
558 MODULE_DEVICE_TABLE(pci, pc300_pci_tbl);
559 module_param(pci_clock_freq, int, 0444);
560 MODULE_PARM_DESC(pci_clock_freq, "System PCI clock frequency in Hz");
561 module_param(use_crystal_clock, int, 0444);
562 MODULE_PARM_DESC(use_crystal_clock,
563 "Use 24.576 MHz clock instead of PCI clock");
564 module_init(pc300_init_module);
565 module_exit(pc300_cleanup_module);