2 * Goramo PCI200SYN synchronous serial card driver for Linux
4 * Copyright (C) 2002-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://hq.pm.waw.pl/hdlc/
12 * Sources of information:
13 * Hitachi HD64572 SCA-II User's Manual
14 * PLX Technology Inc. PCI9052 Data Book
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/sched.h>
21 #include <linux/types.h>
22 #include <linux/fcntl.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/moduleparam.h>
29 #include <linux/netdevice.h>
30 #include <linux/hdlc.h>
31 #include <linux/pci.h>
32 #include <linux/delay.h>
37 static const char* version = "Goramo PCI200SYN driver version: 1.16";
38 static const char* devname = "PCI200SYN";
43 #define PCI200SYN_PLX_SIZE 0x80 /* PLX control window size (128b) */
44 #define PCI200SYN_SCA_SIZE 0x400 /* SCA window size (1Kb) */
45 #define ALL_PAGES_ALWAYS_MAPPED
46 #define NEED_DETECT_RAM
47 #define NEED_SCA_MSCI_INTR
48 #define MAX_TX_BUFFERS 10
50 static int pci_clock_freq = 33000000;
51 #define CLOCK_BASE pci_clock_freq
54 * PLX PCI9052 local configuration and shared runtime registers.
55 * This structure can be used to access 9052 registers (memory mapped).
58 u32 loc_addr_range[4]; /* 00-0Ch : Local Address Ranges */
59 u32 loc_rom_range; /* 10h : Local ROM Range */
60 u32 loc_addr_base[4]; /* 14-20h : Local Address Base Addrs */
61 u32 loc_rom_base; /* 24h : Local ROM Base */
62 u32 loc_bus_descr[4]; /* 28-34h : Local Bus Descriptors */
63 u32 rom_bus_descr; /* 38h : ROM Bus Descriptor */
64 u32 cs_base[4]; /* 3C-48h : Chip Select Base Addrs */
65 u32 intr_ctrl_stat; /* 4Ch : Interrupt Control/Status */
66 u32 init_ctrl; /* 50h : EEPROM ctrl, Init Ctrl, etc */
71 typedef struct port_s {
72 struct net_device *dev;
74 spinlock_t lock; /* TX lock */
75 sync_serial_settings settings;
76 int rxpart; /* partial frame received, next frame invalid*/
77 unsigned short encoding;
78 unsigned short parity;
79 u16 rxin; /* rx ring buffer 'in' pointer */
80 u16 txin; /* tx ring buffer 'in' and 'last' pointers */
82 u8 rxs, txs, tmc; /* SCA registers */
83 u8 phy_node; /* physical port # - 0 or 1 */
88 typedef struct card_s {
89 u8 __iomem *rambase; /* buffer memory base (virtual) */
90 u8 __iomem *scabase; /* SCA memory base (virtual) */
91 plx9052 __iomem *plxbase;/* PLX registers memory base (virtual) */
92 u16 rx_ring_buffers; /* number of buffers in a ring */
94 u16 buff_offset; /* offset of first buffer of first channel */
95 u8 irq; /* interrupt request level */
101 #define sca_in(reg, card) readb(card->scabase + (reg))
102 #define sca_out(value, reg, card) writeb(value, card->scabase + (reg))
103 #define sca_inw(reg, card) readw(card->scabase + (reg))
104 #define sca_outw(value, reg, card) writew(value, card->scabase + (reg))
105 #define sca_inl(reg, card) readl(card->scabase + (reg))
106 #define sca_outl(value, reg, card) writel(value, card->scabase + (reg))
108 #define port_to_card(port) (port->card)
109 #define log_node(port) (port->phy_node)
110 #define phy_node(port) (port->phy_node)
111 #define winbase(card) (card->rambase)
112 #define get_port(card, port) (&card->ports[port])
113 #define sca_flush(card) (sca_in(IER0, card));
115 static inline void new_memcpy_toio(char __iomem *dest, char *src, int length)
119 len = length > 256 ? 256 : length;
120 memcpy_toio(dest, src, len);
129 #define memcpy_toio new_memcpy_toio
134 static void pci200_set_iface(port_t *port)
136 card_t *card = port->card;
137 u16 msci = get_msci(port);
138 u8 rxs = port->rxs & CLK_BRG_MASK;
139 u8 txs = port->txs & CLK_BRG_MASK;
141 sca_out(EXS_TES1, (phy_node(port) ? MSCI1_OFFSET : MSCI0_OFFSET) + EXS,
143 switch(port->settings.clock_type) {
145 rxs |= CLK_BRG; /* BRG output */
146 txs |= CLK_PIN_OUT | CLK_TX_RXCLK; /* RX clock */
150 rxs |= CLK_LINE; /* RXC input */
151 txs |= CLK_PIN_OUT | CLK_BRG; /* BRG output */
155 rxs |= CLK_LINE; /* RXC input */
156 txs |= CLK_PIN_OUT | CLK_TX_RXCLK; /* RX clock */
159 default: /* EXTernal clock */
160 rxs |= CLK_LINE; /* RXC input */
161 txs |= CLK_PIN_OUT | CLK_LINE; /* TXC input */
167 sca_out(rxs, msci + RXS, card);
168 sca_out(txs, msci + TXS, card);
174 static int pci200_open(struct net_device *dev)
176 port_t *port = dev_to_port(dev);
178 int result = hdlc_open(dev);
183 pci200_set_iface(port);
184 sca_flush(port_to_card(port));
190 static int pci200_close(struct net_device *dev)
193 sca_flush(port_to_card(dev_to_port(dev)));
200 static int pci200_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
202 const size_t size = sizeof(sync_serial_settings);
203 sync_serial_settings new_line;
204 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
205 port_t *port = dev_to_port(dev);
208 if (cmd == SIOCDEVPRIVATE) {
213 if (cmd != SIOCWANDEV)
214 return hdlc_ioctl(dev, ifr, cmd);
216 switch(ifr->ifr_settings.type) {
218 ifr->ifr_settings.type = IF_IFACE_V35;
219 if (ifr->ifr_settings.size < size) {
220 ifr->ifr_settings.size = size; /* data size wanted */
223 if (copy_to_user(line, &port->settings, size))
228 case IF_IFACE_SYNC_SERIAL:
229 if (!capable(CAP_NET_ADMIN))
232 if (copy_from_user(&new_line, line, size))
235 if (new_line.clock_type != CLOCK_EXT &&
236 new_line.clock_type != CLOCK_TXFROMRX &&
237 new_line.clock_type != CLOCK_INT &&
238 new_line.clock_type != CLOCK_TXINT)
239 return -EINVAL; /* No such clock setting */
241 if (new_line.loopback != 0 && new_line.loopback != 1)
244 memcpy(&port->settings, &new_line, size); /* Update settings */
245 pci200_set_iface(port);
246 sca_flush(port_to_card(port));
250 return hdlc_ioctl(dev, ifr, cmd);
256 static void pci200_pci_remove_one(struct pci_dev *pdev)
259 card_t *card = pci_get_drvdata(pdev);
261 for (i = 0; i < 2; i++)
262 if (card->ports[i].card) {
263 struct net_device *dev = port_to_dev(&card->ports[i]);
264 unregister_hdlc_device(dev);
268 free_irq(card->irq, card);
271 iounmap(card->rambase);
273 iounmap(card->scabase);
275 iounmap(card->plxbase);
277 pci_release_regions(pdev);
278 pci_disable_device(pdev);
279 pci_set_drvdata(pdev, NULL);
280 if (card->ports[0].dev)
281 free_netdev(card->ports[0].dev);
282 if (card->ports[1].dev)
283 free_netdev(card->ports[1].dev);
289 static int __devinit pci200_pci_init_one(struct pci_dev *pdev,
290 const struct pci_device_id *ent)
297 u32 ramphys; /* buffer memory base */
298 u32 scaphys; /* SCA memory base */
299 u32 plxphys; /* PLX registers memory base */
302 static int printed_version;
303 if (!printed_version++)
304 printk(KERN_INFO "%s\n", version);
307 i = pci_enable_device(pdev);
311 i = pci_request_regions(pdev, "PCI200SYN");
313 pci_disable_device(pdev);
317 card = kmalloc(sizeof(card_t), GFP_KERNEL);
319 printk(KERN_ERR "pci200syn: unable to allocate memory\n");
320 pci_release_regions(pdev);
321 pci_disable_device(pdev);
324 memset(card, 0, sizeof(card_t));
325 pci_set_drvdata(pdev, card);
326 card->ports[0].dev = alloc_hdlcdev(&card->ports[0]);
327 card->ports[1].dev = alloc_hdlcdev(&card->ports[1]);
328 if (!card->ports[0].dev || !card->ports[1].dev) {
329 printk(KERN_ERR "pci200syn: unable to allocate memory\n");
330 pci200_pci_remove_one(pdev);
334 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
335 if (pci_resource_len(pdev, 0) != PCI200SYN_PLX_SIZE ||
336 pci_resource_len(pdev, 2) != PCI200SYN_SCA_SIZE ||
337 pci_resource_len(pdev, 3) < 16384) {
338 printk(KERN_ERR "pci200syn: invalid card EEPROM parameters\n");
339 pci200_pci_remove_one(pdev);
343 plxphys = pci_resource_start(pdev,0) & PCI_BASE_ADDRESS_MEM_MASK;
344 card->plxbase = ioremap(plxphys, PCI200SYN_PLX_SIZE);
346 scaphys = pci_resource_start(pdev,2) & PCI_BASE_ADDRESS_MEM_MASK;
347 card->scabase = ioremap(scaphys, PCI200SYN_SCA_SIZE);
349 ramphys = pci_resource_start(pdev,3) & PCI_BASE_ADDRESS_MEM_MASK;
350 card->rambase = ioremap(ramphys, pci_resource_len(pdev,3));
352 if (card->plxbase == NULL ||
353 card->scabase == NULL ||
354 card->rambase == NULL) {
355 printk(KERN_ERR "pci200syn: ioremap() failed\n");
356 pci200_pci_remove_one(pdev);
360 p = &card->plxbase->init_ctrl;
361 writel(readl(p) | 0x40000000, p);
362 readl(p); /* Flush the write - do not use sca_flush */
365 writel(readl(p) & ~0x40000000, p);
366 readl(p); /* Flush the write - do not use sca_flush */
369 ramsize = sca_detect_ram(card, card->rambase,
370 pci_resource_len(pdev, 3));
372 /* number of TX + RX buffers for one port - this is dual port card */
373 i = ramsize / (2 * (sizeof(pkt_desc) + HDLC_MAX_MRU));
374 card->tx_ring_buffers = min(i / 2, MAX_TX_BUFFERS);
375 card->rx_ring_buffers = i - card->tx_ring_buffers;
377 card->buff_offset = 2 * sizeof(pkt_desc) * (card->tx_ring_buffers +
378 card->rx_ring_buffers);
380 printk(KERN_INFO "pci200syn: %u KB RAM at 0x%x, IRQ%u, using %u TX +"
381 " %u RX packets rings\n", ramsize / 1024, ramphys,
382 pdev->irq, card->tx_ring_buffers, card->rx_ring_buffers);
384 if (pdev->subsystem_device == PCI_DEVICE_ID_PLX_9050) {
385 printk(KERN_ERR "Detected PCI200SYN card with old "
386 "configuration data.\n");
387 printk(KERN_ERR "See <http://www.kernel.org/pub/"
388 "linux/utils/net/hdlc/pci200syn/> for update.\n");
389 printk(KERN_ERR "The card will stop working with"
390 " future versions of Linux if not updated.\n");
393 if (card->tx_ring_buffers < 1) {
394 printk(KERN_ERR "pci200syn: RAM test failed\n");
395 pci200_pci_remove_one(pdev);
399 /* Enable interrupts on the PCI bridge */
400 p = &card->plxbase->intr_ctrl_stat;
401 writew(readw(p) | 0x0040, p);
404 if (request_irq(pdev->irq, sca_intr, SA_SHIRQ, devname, card)) {
405 printk(KERN_WARNING "pci200syn: could not allocate IRQ%d.\n",
407 pci200_pci_remove_one(pdev);
410 card->irq = pdev->irq;
414 for (i = 0; i < 2; i++) {
415 port_t *port = &card->ports[i];
416 struct net_device *dev = port_to_dev(port);
417 hdlc_device *hdlc = dev_to_hdlc(dev);
420 spin_lock_init(&port->lock);
421 SET_MODULE_OWNER(dev);
422 dev->irq = card->irq;
423 dev->mem_start = ramphys;
424 dev->mem_end = ramphys + ramsize - 1;
425 dev->tx_queue_len = 50;
426 dev->do_ioctl = pci200_ioctl;
427 dev->open = pci200_open;
428 dev->stop = pci200_close;
429 hdlc->attach = sca_attach;
430 hdlc->xmit = sca_xmit;
431 port->settings.clock_type = CLOCK_EXT;
433 if (register_hdlc_device(dev)) {
434 printk(KERN_ERR "pci200syn: unable to register hdlc "
437 pci200_pci_remove_one(pdev);
440 sca_init_sync_port(port); /* Set up SCA memory */
442 printk(KERN_INFO "%s: PCI200SYN node %d\n",
443 dev->name, port->phy_node);
452 static struct pci_device_id pci200_pci_tbl[] __devinitdata = {
453 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_PLX,
454 PCI_DEVICE_ID_PLX_9050, 0, 0, 0 },
455 { PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_PLX,
456 PCI_DEVICE_ID_PLX_PCI200SYN, 0, 0, 0 },
461 static struct pci_driver pci200_pci_driver = {
463 .id_table = pci200_pci_tbl,
464 .probe = pci200_pci_init_one,
465 .remove = pci200_pci_remove_one,
469 static int __init pci200_init_module(void)
472 printk(KERN_INFO "%s\n", version);
474 if (pci_clock_freq < 1000000 || pci_clock_freq > 80000000) {
475 printk(KERN_ERR "pci200syn: Invalid PCI clock frequency\n");
478 return pci_module_init(&pci200_pci_driver);
483 static void __exit pci200_cleanup_module(void)
485 pci_unregister_driver(&pci200_pci_driver);
488 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
489 MODULE_DESCRIPTION("Goramo PCI200SYN serial port driver");
490 MODULE_LICENSE("GPL v2");
491 MODULE_DEVICE_TABLE(pci, pci200_pci_tbl);
492 module_param(pci_clock_freq, int, 0444);
493 MODULE_PARM_DESC(pci_clock_freq, "System PCI clock frequency in Hz");
494 module_init(pci200_init_module);
495 module_exit(pci200_cleanup_module);