2 * Copyright (C) 2008 Per Dalen <per.dalen@cnw.se>
4 * Parts of this software are based on (derived) the following:
6 * - Kvaser linux driver, version 4.72 BETA
7 * Copyright (C) 2002-2007 KVASER AB
9 * - Lincan driver, version 0.3.3, OCERA project
10 * Copyright (C) 2004 Pavel Pisa
11 * Copyright (C) 2001 Arnaud Westenberg
13 * - Socketcan SJA1000 drivers
14 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
15 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
16 * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
17 * 38106 Braunschweig, GERMANY
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the version 2 of the GNU General Public License
21 * as published by the Free Software Foundation
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, see <http://www.gnu.org/licenses/>.
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/interrupt.h>
35 #include <linux/netdevice.h>
36 #include <linux/delay.h>
37 #include <linux/pci.h>
38 #include <linux/can/dev.h>
43 #define DRV_NAME "kvaser_pci"
45 MODULE_AUTHOR("Per Dalen <per.dalen@cnw.se>");
46 MODULE_DESCRIPTION("Socket-CAN driver for KVASER PCAN PCI cards");
47 MODULE_SUPPORTED_DEVICE("KVASER PCAN PCI CAN card");
48 MODULE_LICENSE("GPL v2");
50 #define MAX_NO_OF_CHANNELS 4 /* max no of channels on a single card */
54 struct pci_dev
*pci_dev
;
55 struct net_device
*slave_dev
[MAX_NO_OF_CHANNELS
-1];
56 void __iomem
*conf_addr
;
57 void __iomem
*res_addr
;
62 #define KVASER_PCI_CAN_CLOCK (16000000 / 2)
65 * The board configuration is probably following:
66 * RX1 is connected to ground.
67 * TX1 is not connected.
68 * CLKO is not connected.
69 * Setting the OCR register to 0xDA is a good idea.
70 * This means normal output mode , push-pull and the correct polarity.
72 #define KVASER_PCI_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
75 * In the CDR register, you should set CBP to 1.
76 * You will probably also want to set the clock divider value to 0
77 * (meaning divide-by-2), the Pelican bit, and the clock-off bit
78 * (you will have no need for CLKOUT anyway).
80 #define KVASER_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
83 * These register values are valid for revision 14 of the Xilinx logic.
85 #define XILINX_VERINT 7 /* Lower nibble simulate interrupts,
86 high nibble version number. */
88 #define XILINX_PRESUMED_VERSION 14
91 * Important S5920 registers
93 #define S5920_INTCSR 0x38
94 #define S5920_PTCR 0x60
95 #define INTCSR_ADDON_INTENABLE_M 0x2000
98 #define KVASER_PCI_PORT_BYTES 0x20
100 #define PCI_CONFIG_PORT_SIZE 0x80 /* size of the config io-memory */
101 #define PCI_PORT_SIZE 0x80 /* size of a channel io-memory */
102 #define PCI_PORT_XILINX_SIZE 0x08 /* size of a xilinx io-memory */
104 #define KVASER_PCI_VENDOR_ID1 0x10e8 /* the PCI device and vendor IDs */
105 #define KVASER_PCI_DEVICE_ID1 0x8406
107 #define KVASER_PCI_VENDOR_ID2 0x1a07 /* the PCI device and vendor IDs */
108 #define KVASER_PCI_DEVICE_ID2 0x0008
110 static const struct pci_device_id kvaser_pci_tbl
[] = {
111 {KVASER_PCI_VENDOR_ID1
, KVASER_PCI_DEVICE_ID1
, PCI_ANY_ID
, PCI_ANY_ID
,},
112 {KVASER_PCI_VENDOR_ID2
, KVASER_PCI_DEVICE_ID2
, PCI_ANY_ID
, PCI_ANY_ID
,},
116 MODULE_DEVICE_TABLE(pci
, kvaser_pci_tbl
);
118 static u8
kvaser_pci_read_reg(const struct sja1000_priv
*priv
, int port
)
120 return ioread8(priv
->reg_base
+ port
);
123 static void kvaser_pci_write_reg(const struct sja1000_priv
*priv
,
126 iowrite8(val
, priv
->reg_base
+ port
);
129 static void kvaser_pci_disable_irq(struct net_device
*dev
)
131 struct sja1000_priv
*priv
= netdev_priv(dev
);
132 struct kvaser_pci
*board
= priv
->priv
;
135 /* Disable interrupts from card */
136 intcsr
= ioread32(board
->conf_addr
+ S5920_INTCSR
);
137 intcsr
&= ~INTCSR_ADDON_INTENABLE_M
;
138 iowrite32(intcsr
, board
->conf_addr
+ S5920_INTCSR
);
141 static void kvaser_pci_enable_irq(struct net_device
*dev
)
143 struct sja1000_priv
*priv
= netdev_priv(dev
);
144 struct kvaser_pci
*board
= priv
->priv
;
147 /* Enable interrupts from card */
148 tmp_en_io
= ioread32(board
->conf_addr
+ S5920_INTCSR
);
149 tmp_en_io
|= INTCSR_ADDON_INTENABLE_M
;
150 iowrite32(tmp_en_io
, board
->conf_addr
+ S5920_INTCSR
);
153 static int number_of_sja1000_chip(void __iomem
*base_addr
)
158 for (i
= 0; i
< MAX_NO_OF_CHANNELS
; i
++) {
160 iowrite8(MOD_RM
, base_addr
+
161 (i
* KVASER_PCI_PORT_BYTES
) + SJA1000_MOD
);
162 status
= ioread8(base_addr
+
163 (i
* KVASER_PCI_PORT_BYTES
) + SJA1000_MOD
);
164 /* check reset bit */
165 if (!(status
& MOD_RM
))
172 static void kvaser_pci_del_chan(struct net_device
*dev
)
174 struct sja1000_priv
*priv
;
175 struct kvaser_pci
*board
;
180 priv
= netdev_priv(dev
);
185 dev_info(&board
->pci_dev
->dev
, "Removing device %s\n",
188 /* Disable PCI interrupts */
189 kvaser_pci_disable_irq(dev
);
191 for (i
= 0; i
< board
->no_channels
- 1; i
++) {
192 if (board
->slave_dev
[i
]) {
193 dev_info(&board
->pci_dev
->dev
, "Removing device %s\n",
194 board
->slave_dev
[i
]->name
);
195 unregister_sja1000dev(board
->slave_dev
[i
]);
196 free_sja1000dev(board
->slave_dev
[i
]);
199 unregister_sja1000dev(dev
);
201 pci_iounmap(board
->pci_dev
, priv
->reg_base
);
202 pci_iounmap(board
->pci_dev
, board
->conf_addr
);
203 pci_iounmap(board
->pci_dev
, board
->res_addr
);
205 free_sja1000dev(dev
);
208 static int kvaser_pci_add_chan(struct pci_dev
*pdev
, int channel
,
209 struct net_device
**master_dev
,
210 void __iomem
*conf_addr
,
211 void __iomem
*res_addr
,
212 void __iomem
*base_addr
)
214 struct net_device
*dev
;
215 struct sja1000_priv
*priv
;
216 struct kvaser_pci
*board
;
219 dev
= alloc_sja1000dev(sizeof(struct kvaser_pci
));
223 priv
= netdev_priv(dev
);
226 board
->pci_dev
= pdev
;
227 board
->channel
= channel
;
230 board
->conf_addr
= conf_addr
;
232 /* XILINX board wide address */
233 board
->res_addr
= res_addr
;
237 ioread8(board
->res_addr
+ XILINX_VERINT
) >> 4;
240 /* Assert PTADR# - we're in passive mode so the other bits are
242 iowrite32(0x80808080UL
, board
->conf_addr
+ S5920_PTCR
);
244 /* Enable interrupts from card */
245 kvaser_pci_enable_irq(dev
);
247 struct sja1000_priv
*master_priv
= netdev_priv(*master_dev
);
248 struct kvaser_pci
*master_board
= master_priv
->priv
;
249 master_board
->slave_dev
[channel
- 1] = dev
;
250 master_board
->no_channels
= channel
+ 1;
251 board
->xilinx_ver
= master_board
->xilinx_ver
;
254 priv
->reg_base
= base_addr
+ channel
* KVASER_PCI_PORT_BYTES
;
256 priv
->read_reg
= kvaser_pci_read_reg
;
257 priv
->write_reg
= kvaser_pci_write_reg
;
259 priv
->can
.clock
.freq
= KVASER_PCI_CAN_CLOCK
;
261 priv
->ocr
= KVASER_PCI_OCR
;
262 priv
->cdr
= KVASER_PCI_CDR
;
264 priv
->irq_flags
= IRQF_SHARED
;
265 dev
->irq
= pdev
->irq
;
269 dev_info(&pdev
->dev
, "reg_base=%p conf_addr=%p irq=%d\n",
270 priv
->reg_base
, board
->conf_addr
, dev
->irq
);
272 SET_NETDEV_DEV(dev
, &pdev
->dev
);
273 dev
->dev_id
= channel
;
275 /* Register SJA1000 device */
276 err
= register_sja1000dev(dev
);
278 dev_err(&pdev
->dev
, "Registering device failed (err=%d)\n",
289 kvaser_pci_del_chan(dev
);
293 static int kvaser_pci_init_one(struct pci_dev
*pdev
,
294 const struct pci_device_id
*ent
)
297 struct net_device
*master_dev
= NULL
;
298 struct sja1000_priv
*priv
;
299 struct kvaser_pci
*board
;
301 void __iomem
*base_addr
= NULL
;
302 void __iomem
*conf_addr
= NULL
;
303 void __iomem
*res_addr
= NULL
;
306 dev_info(&pdev
->dev
, "initializing device %04x:%04x\n",
307 pdev
->vendor
, pdev
->device
);
309 err
= pci_enable_device(pdev
);
313 err
= pci_request_regions(pdev
, DRV_NAME
);
315 goto failure_release_pci
;
318 conf_addr
= pci_iomap(pdev
, 0, PCI_CONFIG_PORT_SIZE
);
319 if (conf_addr
== NULL
) {
321 goto failure_release_regions
;
324 /* XILINX board wide address */
325 res_addr
= pci_iomap(pdev
, 2, PCI_PORT_XILINX_SIZE
);
326 if (res_addr
== NULL
) {
328 goto failure_iounmap
;
331 base_addr
= pci_iomap(pdev
, 1, PCI_PORT_SIZE
);
332 if (base_addr
== NULL
) {
334 goto failure_iounmap
;
337 no_channels
= number_of_sja1000_chip(base_addr
);
338 if (no_channels
== 0) {
340 goto failure_iounmap
;
343 for (i
= 0; i
< no_channels
; i
++) {
344 err
= kvaser_pci_add_chan(pdev
, i
, &master_dev
,
348 goto failure_cleanup
;
351 priv
= netdev_priv(master_dev
);
354 dev_info(&pdev
->dev
, "xilinx version=%d number of channels=%d\n",
355 board
->xilinx_ver
, board
->no_channels
);
357 pci_set_drvdata(pdev
, master_dev
);
361 kvaser_pci_del_chan(master_dev
);
364 if (conf_addr
!= NULL
)
365 pci_iounmap(pdev
, conf_addr
);
366 if (res_addr
!= NULL
)
367 pci_iounmap(pdev
, res_addr
);
368 if (base_addr
!= NULL
)
369 pci_iounmap(pdev
, base_addr
);
371 failure_release_regions
:
372 pci_release_regions(pdev
);
375 pci_disable_device(pdev
);
382 static void kvaser_pci_remove_one(struct pci_dev
*pdev
)
384 struct net_device
*dev
= pci_get_drvdata(pdev
);
386 kvaser_pci_del_chan(dev
);
388 pci_release_regions(pdev
);
389 pci_disable_device(pdev
);
392 static struct pci_driver kvaser_pci_driver
= {
394 .id_table
= kvaser_pci_tbl
,
395 .probe
= kvaser_pci_init_one
,
396 .remove
= kvaser_pci_remove_one
,
399 module_pci_driver(kvaser_pci_driver
);