blob: f247e5d9002a3b8837f64e390583c3efcb1470c5 [file] [log] [blame]
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +01001/*
2 * Cyclades PC300 synchronous serial card driver for Linux
3 *
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +02004 * Copyright (C) 2000-2008 Krzysztof Halasa <khc@pm.waw.pl>
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +01005 *
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.
9 *
10 * For information see <http://www.kernel.org/pub/linux/utils/net/hdlc/>.
11 *
12 * Sources of information:
13 * Hitachi HD64572 SCA-II User's Manual
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020014 * Original Cyclades PC300 Linux driver
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +010015 *
16 * This driver currently supports only PC300/RSV (V.24/V.35) and
17 * PC300/X21 cards.
18 */
19
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>
26#include <linux/in.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>
36#include <asm/io.h>
37
38#include "hd64572.h"
39
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +010040#undef DEBUG_PKT
41#define DEBUG_RINGS
42
43#define PC300_PLX_SIZE 0x80 /* PLX control window size (128 B) */
44#define PC300_SCA_SIZE 0x400 /* SCA window size (1 KB) */
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +010045#define MAX_TX_BUFFERS 10
46
47static int pci_clock_freq = 33000000;
48static int use_crystal_clock = 0;
49static unsigned int CLOCK_BASE;
50
51/* Masks to access the init_ctrl PLX register */
52#define PC300_CLKSEL_MASK (0x00000004UL)
53#define PC300_CHMEDIA_MASK(port) (0x00000020UL << ((port) * 3))
54#define PC300_CTYPE_MASK (0x00000800UL)
55
56
57enum { PC300_RSV = 1, PC300_X21, PC300_TE }; /* card types */
58
59/*
60 * PLX PCI9050-1 local configuration and shared runtime registers.
61 * This structure can be used to access 9050 registers (memory mapped).
62 */
63typedef struct {
64 u32 loc_addr_range[4]; /* 00-0Ch : Local Address Ranges */
65 u32 loc_rom_range; /* 10h : Local ROM Range */
66 u32 loc_addr_base[4]; /* 14-20h : Local Address Base Addrs */
67 u32 loc_rom_base; /* 24h : Local ROM Base */
68 u32 loc_bus_descr[4]; /* 28-34h : Local Bus Descriptors */
69 u32 rom_bus_descr; /* 38h : ROM Bus Descriptor */
70 u32 cs_base[4]; /* 3C-48h : Chip Select Base Addrs */
71 u32 intr_ctrl_stat; /* 4Ch : Interrupt Control/Status */
72 u32 init_ctrl; /* 50h : EEPROM ctrl, Init Ctrl, etc */
73}plx9050;
74
75
76
77typedef struct port_s {
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +020078 struct napi_struct napi;
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020079 struct net_device *netdev;
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +010080 struct card_s *card;
81 spinlock_t lock; /* TX lock */
82 sync_serial_settings settings;
83 int rxpart; /* partial frame received, next frame invalid*/
84 unsigned short encoding;
85 unsigned short parity;
86 unsigned int iface;
87 u16 rxin; /* rx ring buffer 'in' pointer */
88 u16 txin; /* tx ring buffer 'in' and 'last' pointers */
89 u16 txlast;
90 u8 rxs, txs, tmc; /* SCA registers */
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020091 u8 chan; /* physical port # - 0 or 1 */
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +010092}port_t;
93
94
95
96typedef struct card_s {
97 int type; /* RSV, X21, etc. */
98 int n_ports; /* 1 or 2 ports */
Al Viro184123d2007-02-09 16:40:05 +000099 u8 __iomem *rambase; /* buffer memory base (virtual) */
100 u8 __iomem *scabase; /* SCA memory base (virtual) */
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100101 plx9050 __iomem *plxbase; /* PLX registers memory base (virtual) */
102 u32 init_ctrl_value; /* Saved value - 9050 bug workaround */
103 u16 rx_ring_buffers; /* number of buffers in a ring */
104 u16 tx_ring_buffers;
105 u16 buff_offset; /* offset of first buffer of first channel */
106 u8 irq; /* interrupt request level */
107
108 port_t ports[2];
109}card_t;
110
111
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100112#define get_port(card, port) ((port) < (card)->n_ports ? \
113 (&(card)->ports[port]) : (NULL))
114
Krzysztof Hałasa6b40aba2008-03-24 16:39:02 +0100115#include "hd64572.c"
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100116
117
118static void pc300_set_iface(port_t *port)
119{
120 card_t *card = port->card;
Al Viro184123d2007-02-09 16:40:05 +0000121 u32 __iomem * init_ctrl = &card->plxbase->init_ctrl;
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100122 u16 msci = get_msci(port);
123 u8 rxs = port->rxs & CLK_BRG_MASK;
124 u8 txs = port->txs & CLK_BRG_MASK;
125
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200126 sca_out(EXS_TES1, (port->chan ? MSCI1_OFFSET : MSCI0_OFFSET) + EXS,
127 port->card);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100128 switch(port->settings.clock_type) {
129 case CLOCK_INT:
130 rxs |= CLK_BRG; /* BRG output */
131 txs |= CLK_PIN_OUT | CLK_TX_RXCLK; /* RX clock */
132 break;
133
134 case CLOCK_TXINT:
135 rxs |= CLK_LINE; /* RXC input */
136 txs |= CLK_PIN_OUT | CLK_BRG; /* BRG output */
137 break;
138
139 case CLOCK_TXFROMRX:
140 rxs |= CLK_LINE; /* RXC input */
141 txs |= CLK_PIN_OUT | CLK_TX_RXCLK; /* RX clock */
142 break;
143
144 default: /* EXTernal clock */
145 rxs |= CLK_LINE; /* RXC input */
146 txs |= CLK_PIN_OUT | CLK_LINE; /* TXC input */
147 break;
148 }
149
150 port->rxs = rxs;
151 port->txs = txs;
152 sca_out(rxs, msci + RXS, card);
153 sca_out(txs, msci + TXS, card);
154 sca_set_port(port);
155
156 if (port->card->type == PC300_RSV) {
157 if (port->iface == IF_IFACE_V35)
158 writel(card->init_ctrl_value |
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200159 PC300_CHMEDIA_MASK(port->chan), init_ctrl);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100160 else
161 writel(card->init_ctrl_value &
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200162 ~PC300_CHMEDIA_MASK(port->chan), init_ctrl);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100163 }
164}
165
166
167
168static int pc300_open(struct net_device *dev)
169{
170 port_t *port = dev_to_port(dev);
171
172 int result = hdlc_open(dev);
173 if (result)
174 return result;
175
176 sca_open(dev);
177 pc300_set_iface(port);
178 return 0;
179}
180
181
182
183static int pc300_close(struct net_device *dev)
184{
185 sca_close(dev);
186 hdlc_close(dev);
187 return 0;
188}
189
190
191
192static int pc300_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
193{
194 const size_t size = sizeof(sync_serial_settings);
195 sync_serial_settings new_line;
196 sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
197 int new_type;
198 port_t *port = dev_to_port(dev);
199
200#ifdef DEBUG_RINGS
201 if (cmd == SIOCDEVPRIVATE) {
202 sca_dump_rings(dev);
203 return 0;
204 }
205#endif
206 if (cmd != SIOCWANDEV)
207 return hdlc_ioctl(dev, ifr, cmd);
208
209 if (ifr->ifr_settings.type == IF_GET_IFACE) {
210 ifr->ifr_settings.type = port->iface;
211 if (ifr->ifr_settings.size < size) {
212 ifr->ifr_settings.size = size; /* data size wanted */
213 return -ENOBUFS;
214 }
215 if (copy_to_user(line, &port->settings, size))
216 return -EFAULT;
217 return 0;
218
219 }
220
221 if (port->card->type == PC300_X21 &&
222 (ifr->ifr_settings.type == IF_IFACE_SYNC_SERIAL ||
223 ifr->ifr_settings.type == IF_IFACE_X21))
224 new_type = IF_IFACE_X21;
225
226 else if (port->card->type == PC300_RSV &&
227 (ifr->ifr_settings.type == IF_IFACE_SYNC_SERIAL ||
228 ifr->ifr_settings.type == IF_IFACE_V35))
229 new_type = IF_IFACE_V35;
230
231 else if (port->card->type == PC300_RSV &&
232 ifr->ifr_settings.type == IF_IFACE_V24)
233 new_type = IF_IFACE_V24;
234
235 else
236 return hdlc_ioctl(dev, ifr, cmd);
237
238 if (!capable(CAP_NET_ADMIN))
239 return -EPERM;
240
241 if (copy_from_user(&new_line, line, size))
242 return -EFAULT;
243
244 if (new_line.clock_type != CLOCK_EXT &&
245 new_line.clock_type != CLOCK_TXFROMRX &&
246 new_line.clock_type != CLOCK_INT &&
247 new_line.clock_type != CLOCK_TXINT)
248 return -EINVAL; /* No such clock setting */
249
250 if (new_line.loopback != 0 && new_line.loopback != 1)
251 return -EINVAL;
252
253 memcpy(&port->settings, &new_line, size); /* Update settings */
254 port->iface = new_type;
255 pc300_set_iface(port);
256 return 0;
257}
258
259
260
261static void pc300_pci_remove_one(struct pci_dev *pdev)
262{
263 int i;
264 card_t *card = pci_get_drvdata(pdev);
265
266 for (i = 0; i < 2; i++)
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200267 if (card->ports[i].card)
268 unregister_hdlc_device(card->ports[i].netdev);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100269
270 if (card->irq)
271 free_irq(card->irq, card);
272
273 if (card->rambase)
274 iounmap(card->rambase);
275 if (card->scabase)
276 iounmap(card->scabase);
277 if (card->plxbase)
278 iounmap(card->plxbase);
279
280 pci_release_regions(pdev);
281 pci_disable_device(pdev);
282 pci_set_drvdata(pdev, NULL);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200283 if (card->ports[0].netdev)
284 free_netdev(card->ports[0].netdev);
285 if (card->ports[1].netdev)
286 free_netdev(card->ports[1].netdev);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100287 kfree(card);
288}
289
290
291
292static int __devinit pc300_pci_init_one(struct pci_dev *pdev,
293 const struct pci_device_id *ent)
294{
295 card_t *card;
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100296 u32 __iomem *p;
297 int i;
298 u32 ramsize;
299 u32 ramphys; /* buffer memory base */
300 u32 scaphys; /* SCA memory base */
301 u32 plxphys; /* PLX registers memory base */
302
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100303 i = pci_enable_device(pdev);
304 if (i)
305 return i;
306
307 i = pci_request_regions(pdev, "PC300");
308 if (i) {
309 pci_disable_device(pdev);
310 return i;
311 }
312
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700313 card = kzalloc(sizeof(card_t), GFP_KERNEL);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100314 if (card == NULL) {
315 printk(KERN_ERR "pc300: unable to allocate memory\n");
316 pci_release_regions(pdev);
317 pci_disable_device(pdev);
318 return -ENOBUFS;
319 }
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100320 pci_set_drvdata(pdev, card);
321
Krzysztof Hałasa6476a902008-11-20 15:51:05 +0100322 if (pci_resource_len(pdev, 0) != PC300_PLX_SIZE ||
323 pci_resource_len(pdev, 2) != PC300_SCA_SIZE ||
324 pci_resource_len(pdev, 3) < 16384) {
325 printk(KERN_ERR "pc300: invalid card EEPROM parameters\n");
326 pc300_pci_remove_one(pdev);
327 return -EFAULT;
328 }
329
330 plxphys = pci_resource_start(pdev, 0) & PCI_BASE_ADDRESS_MEM_MASK;
331 card->plxbase = ioremap(plxphys, PC300_PLX_SIZE);
332
333 scaphys = pci_resource_start(pdev, 2) & PCI_BASE_ADDRESS_MEM_MASK;
334 card->scabase = ioremap(scaphys, PC300_SCA_SIZE);
335
336 ramphys = pci_resource_start(pdev, 3) & PCI_BASE_ADDRESS_MEM_MASK;
337 card->rambase = pci_ioremap_bar(pdev, 3);
338
339 if (card->plxbase == NULL ||
340 card->scabase == NULL ||
341 card->rambase == NULL) {
342 printk(KERN_ERR "pc300: ioremap() failed\n");
343 pc300_pci_remove_one(pdev);
344 }
345
346 /* PLX PCI 9050 workaround for local configuration register read bug */
347 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, scaphys);
348 card->init_ctrl_value = readl(&((plx9050 __iomem *)card->scabase)->init_ctrl);
349 pci_write_config_dword(pdev, PCI_BASE_ADDRESS_0, plxphys);
350
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100351 if (pdev->device == PCI_DEVICE_ID_PC300_TE_1 ||
352 pdev->device == PCI_DEVICE_ID_PC300_TE_2)
353 card->type = PC300_TE; /* not fully supported */
354 else if (card->init_ctrl_value & PC300_CTYPE_MASK)
355 card->type = PC300_X21;
356 else
357 card->type = PC300_RSV;
358
359 if (pdev->device == PCI_DEVICE_ID_PC300_RX_1 ||
360 pdev->device == PCI_DEVICE_ID_PC300_TE_1)
361 card->n_ports = 1;
362 else
363 card->n_ports = 2;
364
365 for (i = 0; i < card->n_ports; i++)
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200366 if (!(card->ports[i].netdev = alloc_hdlcdev(&card->ports[i]))) {
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100367 printk(KERN_ERR "pc300: unable to allocate memory\n");
368 pc300_pci_remove_one(pdev);
369 return -ENOMEM;
370 }
371
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100372 /* Reset PLX */
373 p = &card->plxbase->init_ctrl;
374 writel(card->init_ctrl_value | 0x40000000, p);
375 readl(p); /* Flush the write - do not use sca_flush */
376 udelay(1);
377
378 writel(card->init_ctrl_value, p);
379 readl(p); /* Flush the write - do not use sca_flush */
380 udelay(1);
381
382 /* Reload Config. Registers from EEPROM */
383 writel(card->init_ctrl_value | 0x20000000, p);
384 readl(p); /* Flush the write - do not use sca_flush */
385 udelay(1);
386
387 writel(card->init_ctrl_value, p);
388 readl(p); /* Flush the write - do not use sca_flush */
389 udelay(1);
390
391 ramsize = sca_detect_ram(card, card->rambase,
392 pci_resource_len(pdev, 3));
393
394 if (use_crystal_clock)
395 card->init_ctrl_value &= ~PC300_CLKSEL_MASK;
396 else
397 card->init_ctrl_value |= PC300_CLKSEL_MASK;
398
399 writel(card->init_ctrl_value, &card->plxbase->init_ctrl);
400 /* number of TX + RX buffers for one port */
401 i = ramsize / (card->n_ports * (sizeof(pkt_desc) + HDLC_MAX_MRU));
402 card->tx_ring_buffers = min(i / 2, MAX_TX_BUFFERS);
403 card->rx_ring_buffers = i - card->tx_ring_buffers;
404
405 card->buff_offset = card->n_ports * sizeof(pkt_desc) *
406 (card->tx_ring_buffers + card->rx_ring_buffers);
407
408 printk(KERN_INFO "pc300: PC300/%s, %u KB RAM at 0x%x, IRQ%u, "
409 "using %u TX + %u RX packets rings\n",
410 card->type == PC300_X21 ? "X21" :
411 card->type == PC300_TE ? "TE" : "RSV",
412 ramsize / 1024, ramphys, pdev->irq,
413 card->tx_ring_buffers, card->rx_ring_buffers);
414
415 if (card->tx_ring_buffers < 1) {
416 printk(KERN_ERR "pc300: RAM test failed\n");
417 pc300_pci_remove_one(pdev);
418 return -EFAULT;
419 }
420
421 /* Enable interrupts on the PCI bridge, LINTi1 active low */
422 writew(0x0041, &card->plxbase->intr_ctrl_stat);
423
424 /* Allocate IRQ */
Krzysztof Hałasa96783432008-07-09 21:30:17 +0200425 if (request_irq(pdev->irq, sca_intr, IRQF_SHARED, "pc300", card)) {
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100426 printk(KERN_WARNING "pc300: could not allocate IRQ%d.\n",
427 pdev->irq);
428 pc300_pci_remove_one(pdev);
429 return -EBUSY;
430 }
431 card->irq = pdev->irq;
432
433 sca_init(card, 0);
434
435 // COTE not set - allows better TX DMA settings
436 // sca_out(sca_in(PCR, card) | PCR_COTE, PCR, card);
437
438 sca_out(0x10, BTCR, card);
439
440 for (i = 0; i < card->n_ports; i++) {
441 port_t *port = &card->ports[i];
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200442 struct net_device *dev = port->netdev;
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100443 hdlc_device *hdlc = dev_to_hdlc(dev);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200444 port->chan = i;
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100445
446 spin_lock_init(&port->lock);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100447 dev->irq = card->irq;
448 dev->mem_start = ramphys;
449 dev->mem_end = ramphys + ramsize - 1;
450 dev->tx_queue_len = 50;
451 dev->do_ioctl = pc300_ioctl;
452 dev->open = pc300_open;
453 dev->stop = pc300_close;
454 hdlc->attach = sca_attach;
455 hdlc->xmit = sca_xmit;
456 port->settings.clock_type = CLOCK_EXT;
457 port->card = card;
458 if (card->type == PC300_X21)
459 port->iface = IF_IFACE_X21;
460 else
461 port->iface = IF_IFACE_V35;
462
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200463 sca_init_port(port);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100464 if (register_hdlc_device(dev)) {
465 printk(KERN_ERR "pc300: unable to register hdlc "
466 "device\n");
467 port->card = NULL;
468 pc300_pci_remove_one(pdev);
469 return -ENOBUFS;
470 }
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100471
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200472 printk(KERN_INFO "%s: PC300 channel %d\n",
473 dev->name, port->chan);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100474 }
475 return 0;
476}
477
478
479
480static struct pci_device_id pc300_pci_tbl[] __devinitdata = {
481 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_RX_1, PCI_ANY_ID,
482 PCI_ANY_ID, 0, 0, 0 },
483 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_RX_2, PCI_ANY_ID,
484 PCI_ANY_ID, 0, 0, 0 },
485 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_TE_1, PCI_ANY_ID,
486 PCI_ANY_ID, 0, 0, 0 },
487 { PCI_VENDOR_ID_CYCLADES, PCI_DEVICE_ID_PC300_TE_2, PCI_ANY_ID,
488 PCI_ANY_ID, 0, 0, 0 },
489 { 0, }
490};
491
492
493static struct pci_driver pc300_pci_driver = {
Al Viro184123d2007-02-09 16:40:05 +0000494 .name = "PC300",
495 .id_table = pc300_pci_tbl,
496 .probe = pc300_pci_init_one,
497 .remove = pc300_pci_remove_one,
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100498};
499
500
501static int __init pc300_init_module(void)
502{
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100503 if (pci_clock_freq < 1000000 || pci_clock_freq > 80000000) {
504 printk(KERN_ERR "pc300: Invalid PCI clock frequency\n");
505 return -EINVAL;
506 }
507 if (use_crystal_clock != 0 && use_crystal_clock != 1) {
508 printk(KERN_ERR "pc300: Invalid 'use_crystal_clock' value\n");
509 return -EINVAL;
510 }
511
512 CLOCK_BASE = use_crystal_clock ? 24576000 : pci_clock_freq;
513
Richard Knutsson11cc3bb2007-02-14 01:40:21 +0100514 return pci_register_driver(&pc300_pci_driver);
Krzysztof Halasa7517c1b2007-01-30 16:10:24 +0100515}
516
517
518
519static void __exit pc300_cleanup_module(void)
520{
521 pci_unregister_driver(&pc300_pci_driver);
522}
523
524MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
525MODULE_DESCRIPTION("Cyclades PC300 serial port driver");
526MODULE_LICENSE("GPL v2");
527MODULE_DEVICE_TABLE(pci, pc300_pci_tbl);
528module_param(pci_clock_freq, int, 0444);
529MODULE_PARM_DESC(pci_clock_freq, "System PCI clock frequency in Hz");
530module_param(use_crystal_clock, int, 0444);
531MODULE_PARM_DESC(use_crystal_clock,
532 "Use 24.576 MHz clock instead of PCI clock");
533module_init(pc300_init_module);
534module_exit(pc300_cleanup_module);