1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2008 Per Dalen <per.dalen@cnw.se>
5 * Parts of this software are based on (derived) the following:
7 * - Kvaser linux driver, version 4.72 BETA
8 * Copyright (C) 2002-2007 KVASER AB
10 * - Lincan driver, version 0.3.3, OCERA project
11 * Copyright (C) 2004 Pavel Pisa
12 * Copyright (C) 2001 Arnaud Westenberg
14 * - Socketcan SJA1000 drivers
15 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
16 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
17 * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
18 * 38106 Braunschweig, GERMANY
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/interrupt.h>
24 #include <linux/netdevice.h>
25 #include <linux/delay.h>
26 #include <linux/pci.h>
27 #include <linux/can/dev.h>
32 #define DRV_NAME "kvaser_pci"
34 MODULE_AUTHOR("Per Dalen <per.dalen@cnw.se>");
35 MODULE_DESCRIPTION("Socket-CAN driver for KVASER PCAN PCI cards");
36 MODULE_SUPPORTED_DEVICE("KVASER PCAN PCI CAN card");
37 MODULE_LICENSE("GPL v2");
39 #define MAX_NO_OF_CHANNELS 4 /* max no of channels on a single card */
43 struct pci_dev
*pci_dev
;
44 struct net_device
*slave_dev
[MAX_NO_OF_CHANNELS
-1];
45 void __iomem
*conf_addr
;
46 void __iomem
*res_addr
;
51 #define KVASER_PCI_CAN_CLOCK (16000000 / 2)
54 * The board configuration is probably following:
55 * RX1 is connected to ground.
56 * TX1 is not connected.
57 * CLKO is not connected.
58 * Setting the OCR register to 0xDA is a good idea.
59 * This means normal output mode , push-pull and the correct polarity.
61 #define KVASER_PCI_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
64 * In the CDR register, you should set CBP to 1.
65 * You will probably also want to set the clock divider value to 0
66 * (meaning divide-by-2), the Pelican bit, and the clock-off bit
67 * (you will have no need for CLKOUT anyway).
69 #define KVASER_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
72 * These register values are valid for revision 14 of the Xilinx logic.
74 #define XILINX_VERINT 7 /* Lower nibble simulate interrupts,
75 high nibble version number. */
77 #define XILINX_PRESUMED_VERSION 14
80 * Important S5920 registers
82 #define S5920_INTCSR 0x38
83 #define S5920_PTCR 0x60
84 #define INTCSR_ADDON_INTENABLE_M 0x2000
87 #define KVASER_PCI_PORT_BYTES 0x20
89 #define PCI_CONFIG_PORT_SIZE 0x80 /* size of the config io-memory */
90 #define PCI_PORT_SIZE 0x80 /* size of a channel io-memory */
91 #define PCI_PORT_XILINX_SIZE 0x08 /* size of a xilinx io-memory */
93 #define KVASER_PCI_VENDOR_ID1 0x10e8 /* the PCI device and vendor IDs */
94 #define KVASER_PCI_DEVICE_ID1 0x8406
96 #define KVASER_PCI_VENDOR_ID2 0x1a07 /* the PCI device and vendor IDs */
97 #define KVASER_PCI_DEVICE_ID2 0x0008
99 static const struct pci_device_id kvaser_pci_tbl
[] = {
100 {KVASER_PCI_VENDOR_ID1
, KVASER_PCI_DEVICE_ID1
, PCI_ANY_ID
, PCI_ANY_ID
,},
101 {KVASER_PCI_VENDOR_ID2
, KVASER_PCI_DEVICE_ID2
, PCI_ANY_ID
, PCI_ANY_ID
,},
105 MODULE_DEVICE_TABLE(pci
, kvaser_pci_tbl
);
107 static u8
kvaser_pci_read_reg(const struct sja1000_priv
*priv
, int port
)
109 return ioread8(priv
->reg_base
+ port
);
112 static void kvaser_pci_write_reg(const struct sja1000_priv
*priv
,
115 iowrite8(val
, priv
->reg_base
+ port
);
118 static void kvaser_pci_disable_irq(struct net_device
*dev
)
120 struct sja1000_priv
*priv
= netdev_priv(dev
);
121 struct kvaser_pci
*board
= priv
->priv
;
124 /* Disable interrupts from card */
125 intcsr
= ioread32(board
->conf_addr
+ S5920_INTCSR
);
126 intcsr
&= ~INTCSR_ADDON_INTENABLE_M
;
127 iowrite32(intcsr
, board
->conf_addr
+ S5920_INTCSR
);
130 static void kvaser_pci_enable_irq(struct net_device
*dev
)
132 struct sja1000_priv
*priv
= netdev_priv(dev
);
133 struct kvaser_pci
*board
= priv
->priv
;
136 /* Enable interrupts from card */
137 tmp_en_io
= ioread32(board
->conf_addr
+ S5920_INTCSR
);
138 tmp_en_io
|= INTCSR_ADDON_INTENABLE_M
;
139 iowrite32(tmp_en_io
, board
->conf_addr
+ S5920_INTCSR
);
142 static int number_of_sja1000_chip(void __iomem
*base_addr
)
147 for (i
= 0; i
< MAX_NO_OF_CHANNELS
; i
++) {
149 iowrite8(MOD_RM
, base_addr
+
150 (i
* KVASER_PCI_PORT_BYTES
) + SJA1000_MOD
);
151 status
= ioread8(base_addr
+
152 (i
* KVASER_PCI_PORT_BYTES
) + SJA1000_MOD
);
153 /* check reset bit */
154 if (!(status
& MOD_RM
))
161 static void kvaser_pci_del_chan(struct net_device
*dev
)
163 struct sja1000_priv
*priv
;
164 struct kvaser_pci
*board
;
169 priv
= netdev_priv(dev
);
174 dev_info(&board
->pci_dev
->dev
, "Removing device %s\n",
177 /* Disable PCI interrupts */
178 kvaser_pci_disable_irq(dev
);
180 for (i
= 0; i
< board
->no_channels
- 1; i
++) {
181 if (board
->slave_dev
[i
]) {
182 dev_info(&board
->pci_dev
->dev
, "Removing device %s\n",
183 board
->slave_dev
[i
]->name
);
184 unregister_sja1000dev(board
->slave_dev
[i
]);
185 free_sja1000dev(board
->slave_dev
[i
]);
188 unregister_sja1000dev(dev
);
190 pci_iounmap(board
->pci_dev
, priv
->reg_base
);
191 pci_iounmap(board
->pci_dev
, board
->conf_addr
);
192 pci_iounmap(board
->pci_dev
, board
->res_addr
);
194 free_sja1000dev(dev
);
197 static int kvaser_pci_add_chan(struct pci_dev
*pdev
, int channel
,
198 struct net_device
**master_dev
,
199 void __iomem
*conf_addr
,
200 void __iomem
*res_addr
,
201 void __iomem
*base_addr
)
203 struct net_device
*dev
;
204 struct sja1000_priv
*priv
;
205 struct kvaser_pci
*board
;
208 dev
= alloc_sja1000dev(sizeof(struct kvaser_pci
));
212 priv
= netdev_priv(dev
);
215 board
->pci_dev
= pdev
;
216 board
->channel
= channel
;
219 board
->conf_addr
= conf_addr
;
221 /* XILINX board wide address */
222 board
->res_addr
= res_addr
;
226 ioread8(board
->res_addr
+ XILINX_VERINT
) >> 4;
228 /* Assert PTADR# - we're in passive mode so the other bits are
230 iowrite32(0x80808080UL
, board
->conf_addr
+ S5920_PTCR
);
232 /* Enable interrupts from card */
233 kvaser_pci_enable_irq(dev
);
235 struct sja1000_priv
*master_priv
= netdev_priv(*master_dev
);
236 struct kvaser_pci
*master_board
= master_priv
->priv
;
237 master_board
->slave_dev
[channel
- 1] = dev
;
238 master_board
->no_channels
= channel
+ 1;
239 board
->xilinx_ver
= master_board
->xilinx_ver
;
242 priv
->reg_base
= base_addr
+ channel
* KVASER_PCI_PORT_BYTES
;
244 priv
->read_reg
= kvaser_pci_read_reg
;
245 priv
->write_reg
= kvaser_pci_write_reg
;
247 priv
->can
.clock
.freq
= KVASER_PCI_CAN_CLOCK
;
249 priv
->ocr
= KVASER_PCI_OCR
;
250 priv
->cdr
= KVASER_PCI_CDR
;
252 priv
->irq_flags
= IRQF_SHARED
;
253 dev
->irq
= pdev
->irq
;
255 dev_info(&pdev
->dev
, "reg_base=%p conf_addr=%p irq=%d\n",
256 priv
->reg_base
, board
->conf_addr
, dev
->irq
);
258 SET_NETDEV_DEV(dev
, &pdev
->dev
);
259 dev
->dev_id
= channel
;
261 /* Register SJA1000 device */
262 err
= register_sja1000dev(dev
);
264 dev_err(&pdev
->dev
, "Registering device failed (err=%d)\n",
275 kvaser_pci_del_chan(dev
);
279 static int kvaser_pci_init_one(struct pci_dev
*pdev
,
280 const struct pci_device_id
*ent
)
283 struct net_device
*master_dev
= NULL
;
284 struct sja1000_priv
*priv
;
285 struct kvaser_pci
*board
;
287 void __iomem
*base_addr
= NULL
;
288 void __iomem
*conf_addr
= NULL
;
289 void __iomem
*res_addr
= NULL
;
292 dev_info(&pdev
->dev
, "initializing device %04x:%04x\n",
293 pdev
->vendor
, pdev
->device
);
295 err
= pci_enable_device(pdev
);
299 err
= pci_request_regions(pdev
, DRV_NAME
);
301 goto failure_release_pci
;
304 conf_addr
= pci_iomap(pdev
, 0, PCI_CONFIG_PORT_SIZE
);
305 if (conf_addr
== NULL
) {
307 goto failure_release_regions
;
310 /* XILINX board wide address */
311 res_addr
= pci_iomap(pdev
, 2, PCI_PORT_XILINX_SIZE
);
312 if (res_addr
== NULL
) {
314 goto failure_iounmap
;
317 base_addr
= pci_iomap(pdev
, 1, PCI_PORT_SIZE
);
318 if (base_addr
== NULL
) {
320 goto failure_iounmap
;
323 no_channels
= number_of_sja1000_chip(base_addr
);
324 if (no_channels
== 0) {
326 goto failure_iounmap
;
329 for (i
= 0; i
< no_channels
; i
++) {
330 err
= kvaser_pci_add_chan(pdev
, i
, &master_dev
,
334 goto failure_cleanup
;
337 priv
= netdev_priv(master_dev
);
340 dev_info(&pdev
->dev
, "xilinx version=%d number of channels=%d\n",
341 board
->xilinx_ver
, board
->no_channels
);
343 pci_set_drvdata(pdev
, master_dev
);
347 kvaser_pci_del_chan(master_dev
);
350 if (conf_addr
!= NULL
)
351 pci_iounmap(pdev
, conf_addr
);
352 if (res_addr
!= NULL
)
353 pci_iounmap(pdev
, res_addr
);
354 if (base_addr
!= NULL
)
355 pci_iounmap(pdev
, base_addr
);
357 failure_release_regions
:
358 pci_release_regions(pdev
);
361 pci_disable_device(pdev
);
368 static void kvaser_pci_remove_one(struct pci_dev
*pdev
)
370 struct net_device
*dev
= pci_get_drvdata(pdev
);
372 kvaser_pci_del_chan(dev
);
374 pci_release_regions(pdev
);
375 pci_disable_device(pdev
);
378 static struct pci_driver kvaser_pci_driver
= {
380 .id_table
= kvaser_pci_tbl
,
381 .probe
= kvaser_pci_init_one
,
382 .remove
= kvaser_pci_remove_one
,
385 module_pci_driver(kvaser_pci_driver
);