Linux Audio

Check our new training course

Loading...
v4.17
 
  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, see <http://www.gnu.org/licenses/>.
 30 */
 31
 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>
 39#include <linux/io.h>
 40
 41#include "sja1000.h"
 42
 43#define DRV_NAME  "kvaser_pci"
 44
 45MODULE_AUTHOR("Per Dalen <per.dalen@cnw.se>");
 46MODULE_DESCRIPTION("Socket-CAN driver for KVASER PCAN PCI cards");
 47MODULE_SUPPORTED_DEVICE("KVASER PCAN PCI CAN card");
 48MODULE_LICENSE("GPL v2");
 49
 50#define MAX_NO_OF_CHANNELS        4 /* max no of channels on a single card */
 51
 52struct kvaser_pci {
 53	int channel;
 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;
 58	int no_channels;
 59	u8 xilinx_ver;
 60};
 61
 62#define KVASER_PCI_CAN_CLOCK      (16000000 / 2)
 63
 64/*
 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.
 71 */
 72#define KVASER_PCI_OCR            (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
 73
 74/*
 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).
 79 */
 80#define KVASER_PCI_CDR            (CDR_CBP | CDR_CLKOUT_MASK)
 81
 82/*
 83 * These register values are valid for revision 14 of the Xilinx logic.
 84 */
 85#define XILINX_VERINT             7   /* Lower nibble simulate interrupts,
 86					 high nibble version number. */
 87
 88#define XILINX_PRESUMED_VERSION   14
 89
 90/*
 91 * Important S5920 registers
 92 */
 93#define S5920_INTCSR              0x38
 94#define S5920_PTCR                0x60
 95#define INTCSR_ADDON_INTENABLE_M  0x2000
 96
 97
 98#define KVASER_PCI_PORT_BYTES     0x20
 99
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 */
103
104#define KVASER_PCI_VENDOR_ID1     0x10e8    /* the PCI device and vendor IDs */
105#define KVASER_PCI_DEVICE_ID1     0x8406
106
107#define KVASER_PCI_VENDOR_ID2     0x1a07    /* the PCI device and vendor IDs */
108#define KVASER_PCI_DEVICE_ID2     0x0008
109
110static 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,},
113	{ 0,}
114};
115
116MODULE_DEVICE_TABLE(pci, kvaser_pci_tbl);
117
118static u8 kvaser_pci_read_reg(const struct sja1000_priv *priv, int port)
119{
120	return ioread8(priv->reg_base + port);
121}
122
123static void kvaser_pci_write_reg(const struct sja1000_priv *priv,
124				 int port, u8 val)
125{
126	iowrite8(val, priv->reg_base + port);
127}
128
129static void kvaser_pci_disable_irq(struct net_device *dev)
130{
131	struct sja1000_priv *priv = netdev_priv(dev);
132	struct kvaser_pci *board = priv->priv;
133	u32 intcsr;
134
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);
139}
140
141static void kvaser_pci_enable_irq(struct net_device *dev)
142{
143	struct sja1000_priv *priv = netdev_priv(dev);
144	struct kvaser_pci *board = priv->priv;
145	u32 tmp_en_io;
146
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);
151}
152
153static int number_of_sja1000_chip(void __iomem *base_addr)
154{
155	u8 status;
156	int i;
157
158	for (i = 0; i < MAX_NO_OF_CHANNELS; i++) {
159		/* reset chip */
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))
166			break;
167	}
168
169	return i;
170}
171
172static void kvaser_pci_del_chan(struct net_device *dev)
173{
174	struct sja1000_priv *priv;
175	struct kvaser_pci *board;
176	int i;
177
178	if (!dev)
179		return;
180	priv = netdev_priv(dev);
181	board = priv->priv;
182	if (!board)
183		return;
184
185	dev_info(&board->pci_dev->dev, "Removing device %s\n",
186		 dev->name);
187
188	/* Disable PCI interrupts */
189	kvaser_pci_disable_irq(dev);
190
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]);
197		}
198	}
199	unregister_sja1000dev(dev);
200
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);
204
205	free_sja1000dev(dev);
206}
207
208static 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)
213{
214	struct net_device *dev;
215	struct sja1000_priv *priv;
216	struct kvaser_pci *board;
217	int err;
218
219	dev = alloc_sja1000dev(sizeof(struct kvaser_pci));
220	if (dev == NULL)
221		return -ENOMEM;
222
223	priv = netdev_priv(dev);
224	board = priv->priv;
225
226	board->pci_dev = pdev;
227	board->channel = channel;
228
229	/* S5920 */
230	board->conf_addr = conf_addr;
231
232	/* XILINX board wide address */
233	board->res_addr = res_addr;
234
235	if (channel == 0) {
236		board->xilinx_ver =
237			ioread8(board->res_addr + XILINX_VERINT) >> 4;
238
239		/* Assert PTADR# - we're in passive mode so the other bits are
240		   not important */
241		iowrite32(0x80808080UL, board->conf_addr + S5920_PTCR);
242
243		/* Enable interrupts from card */
244		kvaser_pci_enable_irq(dev);
245	} else {
246		struct sja1000_priv *master_priv = netdev_priv(*master_dev);
247		struct kvaser_pci *master_board = master_priv->priv;
248		master_board->slave_dev[channel - 1] = dev;
249		master_board->no_channels = channel + 1;
250		board->xilinx_ver = master_board->xilinx_ver;
251	}
252
253	priv->reg_base = base_addr + channel * KVASER_PCI_PORT_BYTES;
254
255	priv->read_reg = kvaser_pci_read_reg;
256	priv->write_reg = kvaser_pci_write_reg;
257
258	priv->can.clock.freq = KVASER_PCI_CAN_CLOCK;
259
260	priv->ocr = KVASER_PCI_OCR;
261	priv->cdr = KVASER_PCI_CDR;
262
263	priv->irq_flags = IRQF_SHARED;
264	dev->irq = pdev->irq;
265
266	dev_info(&pdev->dev, "reg_base=%p conf_addr=%p irq=%d\n",
267		 priv->reg_base, board->conf_addr, dev->irq);
268
269	SET_NETDEV_DEV(dev, &pdev->dev);
270	dev->dev_id = channel;
271
272	/* Register SJA1000 device */
273	err = register_sja1000dev(dev);
274	if (err) {
275		dev_err(&pdev->dev, "Registering device failed (err=%d)\n",
276			err);
277		goto failure;
278	}
279
280	if (channel == 0)
281		*master_dev = dev;
282
283	return 0;
284
285failure:
286	kvaser_pci_del_chan(dev);
287	return err;
288}
289
290static int kvaser_pci_init_one(struct pci_dev *pdev,
291			       const struct pci_device_id *ent)
292{
293	int err;
294	struct net_device *master_dev = NULL;
295	struct sja1000_priv *priv;
296	struct kvaser_pci *board;
297	int no_channels;
298	void __iomem *base_addr = NULL;
299	void __iomem *conf_addr = NULL;
300	void __iomem *res_addr = NULL;
301	int i;
302
303	dev_info(&pdev->dev, "initializing device %04x:%04x\n",
304		 pdev->vendor, pdev->device);
305
306	err = pci_enable_device(pdev);
307	if (err)
308		goto failure;
309
310	err = pci_request_regions(pdev, DRV_NAME);
311	if (err)
312		goto failure_release_pci;
313
314	/* S5920 */
315	conf_addr = pci_iomap(pdev, 0, PCI_CONFIG_PORT_SIZE);
316	if (conf_addr == NULL) {
317		err = -ENODEV;
318		goto failure_release_regions;
319	}
320
321	/* XILINX board wide address */
322	res_addr = pci_iomap(pdev, 2, PCI_PORT_XILINX_SIZE);
323	if (res_addr == NULL) {
324		err = -ENOMEM;
325		goto failure_iounmap;
326	}
327
328	base_addr = pci_iomap(pdev, 1, PCI_PORT_SIZE);
329	if (base_addr == NULL) {
330		err = -ENOMEM;
331		goto failure_iounmap;
332	}
333
334	no_channels = number_of_sja1000_chip(base_addr);
335	if (no_channels == 0) {
336		err = -ENOMEM;
337		goto failure_iounmap;
338	}
339
340	for (i = 0; i < no_channels; i++) {
341		err = kvaser_pci_add_chan(pdev, i, &master_dev,
342					  conf_addr, res_addr,
343					  base_addr);
344		if (err)
345			goto failure_cleanup;
346	}
347
348	priv = netdev_priv(master_dev);
349	board = priv->priv;
350
351	dev_info(&pdev->dev, "xilinx version=%d number of channels=%d\n",
352		 board->xilinx_ver, board->no_channels);
353
354	pci_set_drvdata(pdev, master_dev);
355	return 0;
356
357failure_cleanup:
358	kvaser_pci_del_chan(master_dev);
359
360failure_iounmap:
361	if (conf_addr != NULL)
362		pci_iounmap(pdev, conf_addr);
363	if (res_addr != NULL)
364		pci_iounmap(pdev, res_addr);
365	if (base_addr != NULL)
366		pci_iounmap(pdev, base_addr);
367
368failure_release_regions:
369	pci_release_regions(pdev);
370
371failure_release_pci:
372	pci_disable_device(pdev);
373
374failure:
375	return err;
376
377}
378
379static void kvaser_pci_remove_one(struct pci_dev *pdev)
380{
381	struct net_device *dev = pci_get_drvdata(pdev);
382
383	kvaser_pci_del_chan(dev);
384
385	pci_release_regions(pdev);
386	pci_disable_device(pdev);
387}
388
389static struct pci_driver kvaser_pci_driver = {
390	.name = DRV_NAME,
391	.id_table = kvaser_pci_tbl,
392	.probe = kvaser_pci_init_one,
393	.remove = kvaser_pci_remove_one,
394};
395
396module_pci_driver(kvaser_pci_driver);
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2008 Per Dalen <per.dalen@cnw.se>
  4 *
  5 * Parts of this software are based on (derived) the following:
  6 *
  7 * - Kvaser linux driver, version 4.72 BETA
  8 *   Copyright (C) 2002-2007 KVASER AB
  9 *
 10 * - Lincan driver, version 0.3.3, OCERA project
 11 *   Copyright (C) 2004 Pavel Pisa
 12 *   Copyright (C) 2001 Arnaud Westenberg
 13 *
 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
 
 
 
 
 
 
 
 
 
 
 
 
 19 */
 20
 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>
 28#include <linux/io.h>
 29
 30#include "sja1000.h"
 31
 32#define DRV_NAME  "kvaser_pci"
 33
 34MODULE_AUTHOR("Per Dalen <per.dalen@cnw.se>");
 35MODULE_DESCRIPTION("Socket-CAN driver for KVASER PCAN PCI cards");
 36MODULE_SUPPORTED_DEVICE("KVASER PCAN PCI CAN card");
 37MODULE_LICENSE("GPL v2");
 38
 39#define MAX_NO_OF_CHANNELS        4 /* max no of channels on a single card */
 40
 41struct kvaser_pci {
 42	int channel;
 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;
 47	int no_channels;
 48	u8 xilinx_ver;
 49};
 50
 51#define KVASER_PCI_CAN_CLOCK      (16000000 / 2)
 52
 53/*
 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.
 60 */
 61#define KVASER_PCI_OCR            (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
 62
 63/*
 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).
 68 */
 69#define KVASER_PCI_CDR            (CDR_CBP | CDR_CLKOUT_MASK)
 70
 71/*
 72 * These register values are valid for revision 14 of the Xilinx logic.
 73 */
 74#define XILINX_VERINT             7   /* Lower nibble simulate interrupts,
 75					 high nibble version number. */
 76
 77#define XILINX_PRESUMED_VERSION   14
 78
 79/*
 80 * Important S5920 registers
 81 */
 82#define S5920_INTCSR              0x38
 83#define S5920_PTCR                0x60
 84#define INTCSR_ADDON_INTENABLE_M  0x2000
 85
 86
 87#define KVASER_PCI_PORT_BYTES     0x20
 88
 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 */
 92
 93#define KVASER_PCI_VENDOR_ID1     0x10e8    /* the PCI device and vendor IDs */
 94#define KVASER_PCI_DEVICE_ID1     0x8406
 95
 96#define KVASER_PCI_VENDOR_ID2     0x1a07    /* the PCI device and vendor IDs */
 97#define KVASER_PCI_DEVICE_ID2     0x0008
 98
 99static 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,},
102	{ 0,}
103};
104
105MODULE_DEVICE_TABLE(pci, kvaser_pci_tbl);
106
107static u8 kvaser_pci_read_reg(const struct sja1000_priv *priv, int port)
108{
109	return ioread8(priv->reg_base + port);
110}
111
112static void kvaser_pci_write_reg(const struct sja1000_priv *priv,
113				 int port, u8 val)
114{
115	iowrite8(val, priv->reg_base + port);
116}
117
118static void kvaser_pci_disable_irq(struct net_device *dev)
119{
120	struct sja1000_priv *priv = netdev_priv(dev);
121	struct kvaser_pci *board = priv->priv;
122	u32 intcsr;
123
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);
128}
129
130static void kvaser_pci_enable_irq(struct net_device *dev)
131{
132	struct sja1000_priv *priv = netdev_priv(dev);
133	struct kvaser_pci *board = priv->priv;
134	u32 tmp_en_io;
135
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);
140}
141
142static int number_of_sja1000_chip(void __iomem *base_addr)
143{
144	u8 status;
145	int i;
146
147	for (i = 0; i < MAX_NO_OF_CHANNELS; i++) {
148		/* reset chip */
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))
155			break;
156	}
157
158	return i;
159}
160
161static void kvaser_pci_del_chan(struct net_device *dev)
162{
163	struct sja1000_priv *priv;
164	struct kvaser_pci *board;
165	int i;
166
167	if (!dev)
168		return;
169	priv = netdev_priv(dev);
170	board = priv->priv;
171	if (!board)
172		return;
173
174	dev_info(&board->pci_dev->dev, "Removing device %s\n",
175		 dev->name);
176
177	/* Disable PCI interrupts */
178	kvaser_pci_disable_irq(dev);
179
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]);
186		}
187	}
188	unregister_sja1000dev(dev);
189
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);
193
194	free_sja1000dev(dev);
195}
196
197static 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)
202{
203	struct net_device *dev;
204	struct sja1000_priv *priv;
205	struct kvaser_pci *board;
206	int err;
207
208	dev = alloc_sja1000dev(sizeof(struct kvaser_pci));
209	if (dev == NULL)
210		return -ENOMEM;
211
212	priv = netdev_priv(dev);
213	board = priv->priv;
214
215	board->pci_dev = pdev;
216	board->channel = channel;
217
218	/* S5920 */
219	board->conf_addr = conf_addr;
220
221	/* XILINX board wide address */
222	board->res_addr = res_addr;
223
224	if (channel == 0) {
225		board->xilinx_ver =
226			ioread8(board->res_addr + XILINX_VERINT) >> 4;
227
228		/* Assert PTADR# - we're in passive mode so the other bits are
229		   not important */
230		iowrite32(0x80808080UL, board->conf_addr + S5920_PTCR);
231
232		/* Enable interrupts from card */
233		kvaser_pci_enable_irq(dev);
234	} else {
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;
240	}
241
242	priv->reg_base = base_addr + channel * KVASER_PCI_PORT_BYTES;
243
244	priv->read_reg = kvaser_pci_read_reg;
245	priv->write_reg = kvaser_pci_write_reg;
246
247	priv->can.clock.freq = KVASER_PCI_CAN_CLOCK;
248
249	priv->ocr = KVASER_PCI_OCR;
250	priv->cdr = KVASER_PCI_CDR;
251
252	priv->irq_flags = IRQF_SHARED;
253	dev->irq = pdev->irq;
254
255	dev_info(&pdev->dev, "reg_base=%p conf_addr=%p irq=%d\n",
256		 priv->reg_base, board->conf_addr, dev->irq);
257
258	SET_NETDEV_DEV(dev, &pdev->dev);
259	dev->dev_id = channel;
260
261	/* Register SJA1000 device */
262	err = register_sja1000dev(dev);
263	if (err) {
264		dev_err(&pdev->dev, "Registering device failed (err=%d)\n",
265			err);
266		goto failure;
267	}
268
269	if (channel == 0)
270		*master_dev = dev;
271
272	return 0;
273
274failure:
275	kvaser_pci_del_chan(dev);
276	return err;
277}
278
279static int kvaser_pci_init_one(struct pci_dev *pdev,
280			       const struct pci_device_id *ent)
281{
282	int err;
283	struct net_device *master_dev = NULL;
284	struct sja1000_priv *priv;
285	struct kvaser_pci *board;
286	int no_channels;
287	void __iomem *base_addr = NULL;
288	void __iomem *conf_addr = NULL;
289	void __iomem *res_addr = NULL;
290	int i;
291
292	dev_info(&pdev->dev, "initializing device %04x:%04x\n",
293		 pdev->vendor, pdev->device);
294
295	err = pci_enable_device(pdev);
296	if (err)
297		goto failure;
298
299	err = pci_request_regions(pdev, DRV_NAME);
300	if (err)
301		goto failure_release_pci;
302
303	/* S5920 */
304	conf_addr = pci_iomap(pdev, 0, PCI_CONFIG_PORT_SIZE);
305	if (conf_addr == NULL) {
306		err = -ENODEV;
307		goto failure_release_regions;
308	}
309
310	/* XILINX board wide address */
311	res_addr = pci_iomap(pdev, 2, PCI_PORT_XILINX_SIZE);
312	if (res_addr == NULL) {
313		err = -ENOMEM;
314		goto failure_iounmap;
315	}
316
317	base_addr = pci_iomap(pdev, 1, PCI_PORT_SIZE);
318	if (base_addr == NULL) {
319		err = -ENOMEM;
320		goto failure_iounmap;
321	}
322
323	no_channels = number_of_sja1000_chip(base_addr);
324	if (no_channels == 0) {
325		err = -ENOMEM;
326		goto failure_iounmap;
327	}
328
329	for (i = 0; i < no_channels; i++) {
330		err = kvaser_pci_add_chan(pdev, i, &master_dev,
331					  conf_addr, res_addr,
332					  base_addr);
333		if (err)
334			goto failure_cleanup;
335	}
336
337	priv = netdev_priv(master_dev);
338	board = priv->priv;
339
340	dev_info(&pdev->dev, "xilinx version=%d number of channels=%d\n",
341		 board->xilinx_ver, board->no_channels);
342
343	pci_set_drvdata(pdev, master_dev);
344	return 0;
345
346failure_cleanup:
347	kvaser_pci_del_chan(master_dev);
348
349failure_iounmap:
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);
356
357failure_release_regions:
358	pci_release_regions(pdev);
359
360failure_release_pci:
361	pci_disable_device(pdev);
362
363failure:
364	return err;
365
366}
367
368static void kvaser_pci_remove_one(struct pci_dev *pdev)
369{
370	struct net_device *dev = pci_get_drvdata(pdev);
371
372	kvaser_pci_del_chan(dev);
373
374	pci_release_regions(pdev);
375	pci_disable_device(pdev);
376}
377
378static struct pci_driver kvaser_pci_driver = {
379	.name = DRV_NAME,
380	.id_table = kvaser_pci_tbl,
381	.probe = kvaser_pci_init_one,
382	.remove = kvaser_pci_remove_one,
383};
384
385module_pci_driver(kvaser_pci_driver);