]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/net/can/sja1000/kvaser_pci.c
Added socketcan from SVN r903
[lisovros/linux_canprio.git] / drivers / net / can / sja1000 / kvaser_pci.c
1 /*
2  * Copyright (C) 2008 Per Dalen <per.dalen@cnw.se>
3  *
4  * Parts of this software are based on (derived) the following:
5  *
6  * - Kvaser linux driver, version 4.72 BETA
7  *   Copyright (C) 2002-2007 KVASER AB
8  *
9  * - Lincan driver, version 0.3.3, OCERA project
10  *   Copyright (C) 2004 Pavel Pisa
11  *   Copyright (C) 2001 Arnaud Westenberg
12  *
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
18  *
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
22  *
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.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software Foundation,
30  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/interrupt.h>
36 #include <linux/netdevice.h>
37 #include <linux/delay.h>
38 #include <linux/pci.h>
39 #include <linux/can.h>
40 #include <linux/can/dev.h>
41 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
42 #include <linux/io.h>
43 #else
44 #include <asm/io.h>
45 #endif
46
47 #include "sja1000.h"
48
49 #define DRV_NAME  "kvaser_pci"
50
51 MODULE_AUTHOR("Per Dalen <per.dalen@cnw.se>");
52 MODULE_DESCRIPTION("Socket-CAN driver for KVASER PCAN PCI cards");
53 MODULE_SUPPORTED_DEVICE("KVASER PCAN PCI CAN card");
54 MODULE_LICENSE("GPL v2");
55
56 #define MAX_NO_OF_CHANNELS        4 /* max no of channels on
57                                        a single card */
58
59 struct kvaser_pci {
60         int channel;
61         struct pci_dev *pci_dev;
62         struct net_device *slave_dev[MAX_NO_OF_CHANNELS-1];
63         void __iomem *conf_addr;
64         void __iomem *res_addr;
65         int no_channels;
66         u8 xilinx_ver;
67 };
68
69 #define KVASER_PCI_CAN_CLOCK      (16000000 / 2)
70
71 /*
72  * The board configuration is probably following:
73  * RX1 is connected to ground.
74  * TX1 is not connected.
75  * CLKO is not connected.
76  * Setting the OCR register to 0xDA is a good idea.
77  * This means  normal output mode , push-pull and the correct polarity.
78  */
79 #define KVASER_PCI_OCR            (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
80
81 /*
82  * In the CDR register, you should set CBP to 1.
83  * You will probably also want to set the clock divider value to 0
84  * (meaning divide-by-2), the Pelican bit, and the clock-off bit
85  * (you will have no need for CLKOUT anyway).
86  */
87 #define KVASER_PCI_CDR            (CDR_CBP | CDR_CLKOUT_MASK)
88
89 /*
90  * These register values are valid for revision 14 of the Xilinx logic.
91  */
92 #define XILINX_VERINT             7   /* Lower nibble simulate interrupts,
93                                          high nibble version number. */
94
95 #define XILINX_PRESUMED_VERSION   14
96
97 /*
98  * Important S5920 registers
99  */
100 #define S5920_INTCSR              0x38
101 #define S5920_PTCR                0x60
102 #define INTCSR_ADDON_INTENABLE_M  0x2000
103
104
105 #define KVASER_PCI_PORT_BYTES     0x20
106
107 #define PCI_CONFIG_PORT_SIZE      0x80      /* size of the config io-memory */
108 #define PCI_PORT_SIZE             0x80      /* size of a channel io-memory */
109 #define PCI_PORT_XILINX_SIZE      0x08      /* size of a xilinx io-memory */
110
111 #define KVASER_PCI_VENDOR_ID1     0x10e8    /* the PCI device and vendor IDs */
112 #define KVASER_PCI_DEVICE_ID1     0x8406
113
114 #define KVASER_PCI_VENDOR_ID2     0x1a07    /* the PCI device and vendor IDs */
115 #define KVASER_PCI_DEVICE_ID2     0x0008
116
117 static struct pci_device_id kvaser_pci_tbl[] = {
118         {KVASER_PCI_VENDOR_ID1, KVASER_PCI_DEVICE_ID1, PCI_ANY_ID, PCI_ANY_ID,},
119         {KVASER_PCI_VENDOR_ID2, KVASER_PCI_DEVICE_ID2, PCI_ANY_ID, PCI_ANY_ID,},
120         { 0,}
121 };
122
123 MODULE_DEVICE_TABLE(pci, kvaser_pci_tbl);
124
125 static u8 kvaser_pci_read_reg(struct net_device *dev, int port)
126 {
127         return ioread8((void __iomem *)(dev->base_addr + port));
128 }
129
130 static void kvaser_pci_write_reg(struct net_device *dev, int port, u8 val)
131 {
132         iowrite8(val, (void __iomem *)(dev->base_addr + port));
133 }
134
135 static void kvaser_pci_disable_irq(struct net_device *dev)
136 {
137         struct sja1000_priv *priv = netdev_priv(dev);
138         struct kvaser_pci *board = priv->priv;
139         u32 tmp;
140
141         /* Disable interrupts from card */
142         tmp = ioread32(board->conf_addr + S5920_INTCSR);
143         tmp &= ~INTCSR_ADDON_INTENABLE_M;
144         iowrite32(tmp, board->conf_addr + S5920_INTCSR);
145 }
146
147 static void kvaser_pci_enable_irq(struct net_device *dev)
148 {
149         struct sja1000_priv *priv = netdev_priv(dev);
150         struct kvaser_pci *board = priv->priv;
151         u32 tmp;
152
153         /* Enable interrupts from card */
154         tmp = ioread32(board->conf_addr + S5920_INTCSR);
155         tmp |= INTCSR_ADDON_INTENABLE_M;
156         iowrite32(tmp, board->conf_addr + S5920_INTCSR);
157 }
158
159 static int number_of_sja1000_chip(void __iomem *base_addr)
160 {
161         u8 status;
162         int i;
163
164         for (i = 0; i < MAX_NO_OF_CHANNELS; i++) {
165                 /* reset chip */
166                 iowrite8(MOD_RM, base_addr +
167                          (i * KVASER_PCI_PORT_BYTES) + REG_MOD);
168                 status = ioread8(base_addr +
169                                  (i * KVASER_PCI_PORT_BYTES) + REG_MOD);
170                 udelay(10);
171                 /* check reset bit */
172                 if (!(status & MOD_RM))
173                         break;
174         }
175
176         return i;
177 }
178
179 static void kvaser_pci_del_chan(struct net_device *dev)
180 {
181         struct sja1000_priv *priv;
182         struct kvaser_pci *board;
183         int i;
184
185         if (!dev)
186                 return;
187         priv = netdev_priv(dev);
188         if (!priv)
189                 return;
190         board = priv->priv;
191         if (!board)
192                 return;
193
194         dev_info(&board->pci_dev->dev, "Removing device %s\n",
195                  dev->name);
196
197         for (i = 0; i < board->no_channels - 1; i++) {
198                 if (board->slave_dev[i]) {
199                         dev_info(&board->pci_dev->dev, "Removing device %s\n",
200                                  board->slave_dev[i]->name);
201                         unregister_sja1000dev(board->slave_dev[i]);
202                         free_sja1000dev(board->slave_dev[i]);
203                 }
204         }
205         unregister_sja1000dev(dev);
206
207         /* Disable PCI interrupts */
208         kvaser_pci_disable_irq(dev);
209
210         pci_iounmap(board->pci_dev, (void __iomem *)dev->base_addr);
211         pci_iounmap(board->pci_dev, board->conf_addr);
212         pci_iounmap(board->pci_dev, board->res_addr);
213
214         free_sja1000dev(dev);
215 }
216
217 static int kvaser_pci_add_chan(struct pci_dev *pdev, int channel,
218                                struct net_device **master_dev,
219                                void __iomem *conf_addr,
220                                void __iomem *res_addr,
221                                unsigned long base_addr)
222 {
223         struct net_device *dev;
224         struct sja1000_priv *priv;
225         struct kvaser_pci *board;
226         int err, init_step;
227
228         dev = alloc_sja1000dev(sizeof(struct kvaser_pci));
229         if (dev == NULL)
230                 return -ENOMEM;
231
232         priv = netdev_priv(dev);
233         board = priv->priv;
234
235         board->pci_dev = pdev;
236         board->channel = channel;
237
238         /*S5920*/
239         board->conf_addr = conf_addr;
240
241         /*XILINX board wide address*/
242         board->res_addr = res_addr;
243
244         if (channel == 0) {
245                 board->xilinx_ver =
246                         ioread8(board->res_addr + XILINX_VERINT) >> 4;
247                 init_step = 2;
248
249                 /* Assert PTADR# - we're in passive mode so the other bits are
250                    not important */
251                 iowrite32(0x80808080UL, board->conf_addr + S5920_PTCR);
252
253                 /* Disable interrupts from card */
254                 kvaser_pci_disable_irq(dev);
255                 /* Enable interrupts from card */
256                 kvaser_pci_enable_irq(dev);
257         } else {
258                 struct sja1000_priv *master_priv = netdev_priv(*master_dev);
259                 struct kvaser_pci *master_board = master_priv->priv;
260                 master_board->slave_dev[channel - 1] = dev;
261                 master_board->no_channels = channel + 1;
262                 board->xilinx_ver = master_board->xilinx_ver;
263         }
264
265         dev->base_addr = base_addr + channel * KVASER_PCI_PORT_BYTES;
266
267         priv->read_reg = kvaser_pci_read_reg;
268         priv->write_reg = kvaser_pci_write_reg;
269
270         priv->can.bittiming.clock = KVASER_PCI_CAN_CLOCK;
271
272         priv->ocr = KVASER_PCI_OCR;
273         priv->cdr = KVASER_PCI_CDR;
274
275         /* Register and setup interrupt handling */
276         dev->irq = pdev->irq;
277         init_step = 4;
278
279         dev_info(&pdev->dev, "base_addr=%#lx conf_addr=%p irq=%d\n",
280                  dev->base_addr, board->conf_addr, dev->irq);
281
282         SET_NETDEV_DEV(dev, &pdev->dev);
283
284         /* Register SJA1000 device */
285         err = register_sja1000dev(dev);
286         if (err) {
287                 dev_err(&pdev->dev, "Registering device failed (err=%d)\n",
288                         err);
289                 goto failure;
290         }
291
292         if (channel == 0)
293                 *master_dev = dev;
294
295         return 0;
296
297 failure:
298         kvaser_pci_del_chan(dev);
299         return err;
300 }
301
302 static int __devinit kvaser_pci_init_one(struct pci_dev *pdev,
303                                          const struct pci_device_id *ent)
304 {
305         int err;
306         struct net_device *master_dev = NULL;
307         struct sja1000_priv *priv;
308         struct kvaser_pci *board;
309         int no_channels;
310         void __iomem *base_addr = NULL;
311         void __iomem *conf_addr = NULL;
312         void __iomem *res_addr = NULL;
313         int i;
314
315         dev_info(&pdev->dev, "initializing device %04x:%04x\n",
316                  pdev->vendor, pdev->device);
317
318         err = pci_enable_device(pdev);
319         if (err)
320                 goto failure;
321
322         err = pci_request_regions(pdev, DRV_NAME);
323         if (err)
324                 goto failure_release_pci;
325
326         /*S5920*/
327         conf_addr = pci_iomap(pdev, 0, PCI_CONFIG_PORT_SIZE);
328         if (conf_addr == 0) {
329                 err = -ENODEV;
330                 goto failure_iounmap;
331         }
332
333         /*XILINX board wide address*/
334         res_addr = pci_iomap(pdev, 2, PCI_PORT_XILINX_SIZE);
335         if (res_addr == 0) {
336                 err = -ENOMEM;
337                 goto failure_iounmap;
338         }
339
340         base_addr = pci_iomap(pdev, 1, PCI_PORT_SIZE);
341         if (base_addr == 0) {
342                 err = -ENOMEM;
343                 goto failure_iounmap;
344         }
345
346         no_channels = number_of_sja1000_chip(base_addr);
347         if (no_channels == 0) {
348                 err = -ENOMEM;
349                 goto failure_iounmap;
350         }
351
352         for (i = 0; i < no_channels; i++) {
353                 err = kvaser_pci_add_chan(pdev, i, &master_dev,
354                                           conf_addr, res_addr,
355                                           (unsigned long)base_addr);
356                 if (err)
357                         goto failure_cleanup;
358         }
359
360         priv = netdev_priv(master_dev);
361         board = priv->priv;
362
363         dev_info(&pdev->dev, "xilinx version=%d number of channels=%d\n",
364                  board->xilinx_ver, board->no_channels);
365
366         pci_set_drvdata(pdev, master_dev);
367         return 0;
368
369 failure_cleanup:
370         kvaser_pci_del_chan(master_dev);
371
372 failure_iounmap:
373         if (conf_addr == 0)
374                 pci_iounmap(pdev, conf_addr);
375         if (res_addr == 0)
376                 pci_iounmap(pdev, res_addr);
377         if (base_addr == 0)
378                 pci_iounmap(pdev, base_addr);
379
380         pci_release_regions(pdev);
381
382 failure_release_pci:
383         pci_disable_device(pdev);
384
385 failure:
386         return err;
387
388 }
389
390 static void __devexit kvaser_pci_remove_one(struct pci_dev *pdev)
391 {
392         struct net_device *dev = pci_get_drvdata(pdev);
393
394         kvaser_pci_del_chan(dev);
395
396         pci_release_regions(pdev);
397         pci_disable_device(pdev);
398         pci_set_drvdata(pdev, NULL);
399 }
400
401 static struct pci_driver kvaser_pci_driver = {
402         .name = DRV_NAME,
403         .id_table = kvaser_pci_tbl,
404         .probe = kvaser_pci_init_one,
405         .remove = __devexit_p(kvaser_pci_remove_one),
406 };
407
408 static int __init kvaser_pci_init(void)
409 {
410         return pci_register_driver(&kvaser_pci_driver);
411 }
412
413 static void __exit kvaser_pci_exit(void)
414 {
415         pci_unregister_driver(&kvaser_pci_driver);
416 }
417
418 module_init(kvaser_pci_init);
419 module_exit(kvaser_pci_exit);