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