Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  4 * Copyright (C) 2008 Markus Plessing <plessing@ems-wuensche.com>
  5 * Copyright (C) 2008 Sebastian Haas <haas@ems-wuensche.com>
  6 * Copyright (C) 2023 EMS Dr. Thomas Wuensche
  7 */
  8
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/interrupt.h>
 12#include <linux/netdevice.h>
 13#include <linux/delay.h>
 14#include <linux/slab.h>
 15#include <linux/pci.h>
 16#include <linux/can/dev.h>
 17#include <linux/io.h>
 18
 19#include "sja1000.h"
 20
 21#define DRV_NAME  "ems_pci"
 22
 23MODULE_AUTHOR("Sebastian Haas <support@ems-wuensche.com>");
 24MODULE_AUTHOR("Gerhard Uttenthaler <uttenthaler@ems-wuensche.com>");
 25MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-PCI/PCIe/104P CAN cards");
 
 26MODULE_LICENSE("GPL v2");
 27
 28#define EMS_PCI_V1_MAX_CHAN 2
 29#define EMS_PCI_V2_MAX_CHAN 4
 30#define EMS_PCI_V3_MAX_CHAN 4
 31#define EMS_PCI_MAX_CHAN    EMS_PCI_V2_MAX_CHAN
 32
 33struct ems_pci_card {
 34	int version;
 35	int channels;
 36
 37	struct pci_dev *pci_dev;
 38	struct net_device *net_dev[EMS_PCI_MAX_CHAN];
 39
 40	void __iomem *conf_addr;
 41	void __iomem *base_addr;
 42};
 43
 44#define EMS_PCI_CAN_CLOCK (16000000 / 2)
 45
 46/* Register definitions and descriptions are from LinCAN 0.3.3.
 
 47 *
 48 * PSB4610 PITA-2 bridge control registers
 49 */
 50#define PITA2_ICR           0x00	/* Interrupt Control Register */
 51#define PITA2_ICR_INT0      0x00000002	/* [RC] INT0 Active/Clear */
 52#define PITA2_ICR_INT0_EN   0x00020000	/* [RW] Enable INT0 */
 53
 54#define PITA2_MISC          0x1c	/* Miscellaneous Register */
 55#define PITA2_MISC_CONFIG   0x04000000	/* Multiplexed parallel interface */
 56
 57/* Register definitions for the PLX 9030
 
 58 */
 59#define PLX_ICSR            0x4c   /* Interrupt Control/Status register */
 60#define PLX_ICSR_LINTI1_ENA 0x0001 /* LINTi1 Enable */
 61#define PLX_ICSR_PCIINT_ENA 0x0040 /* PCI Interrupt Enable */
 62#define PLX_ICSR_LINTI1_CLR 0x0400 /* Local Edge Triggerable Interrupt Clear */
 63#define PLX_ICSR_ENA_CLR    (PLX_ICSR_LINTI1_ENA | PLX_ICSR_PCIINT_ENA | \
 64			     PLX_ICSR_LINTI1_CLR)
 65
 66/* Register definitions for the ASIX99100
 67 */
 68#define ASIX_LINTSR 0x28 /* Interrupt Control/Status register */
 69#define ASIX_LINTSR_INT0AC BIT(0) /* Writing 1 enables or clears interrupt */
 70
 71#define ASIX_LIEMR 0x24 /* Local Interrupt Enable / Miscellaneous Register */
 72#define ASIX_LIEMR_L0EINTEN BIT(16) /* Local INT0 input assertion enable */
 73#define ASIX_LIEMR_LRST BIT(14) /* Local Reset assert */
 74
 75/* The board configuration is probably following:
 76 * RX1 is connected to ground.
 77 * TX1 is not connected.
 78 * CLKO is not connected.
 79 * Setting the OCR register to 0xDA is a good idea.
 80 * This means normal output mode, push-pull and the correct polarity.
 81 */
 82#define EMS_PCI_OCR         (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
 83
 84/* In the CDR register, you should set CBP to 1.
 
 85 * You will probably also want to set the clock divider value to 7
 86 * (meaning direct oscillator output) because the second SJA1000 chip
 87 * is driven by the first one CLKOUT output.
 88 */
 89#define EMS_PCI_CDR             (CDR_CBP | CDR_CLKOUT_MASK)
 90
 91#define EMS_PCI_V1_BASE_BAR 1
 92#define EMS_PCI_V1_CONF_BAR 0
 93#define EMS_PCI_V1_CONF_SIZE 4096 /* size of PITA control area */
 94#define EMS_PCI_V1_CAN_BASE_OFFSET 0x400 /* offset where the controllers start */
 95#define EMS_PCI_V1_CAN_CTRL_SIZE 0x200 /* memory size for each controller */
 96
 97#define EMS_PCI_V2_BASE_BAR 2
 98#define EMS_PCI_V2_CONF_BAR 0
 99#define EMS_PCI_V2_CONF_SIZE 128 /* size of PLX control area */
100#define EMS_PCI_V2_CAN_BASE_OFFSET 0x400 /* offset where the controllers start */
101#define EMS_PCI_V2_CAN_CTRL_SIZE 0x200 /* memory size for each controller */
102
103#define EMS_PCI_V3_BASE_BAR 0
104#define EMS_PCI_V3_CONF_BAR 5
105#define EMS_PCI_V3_CONF_SIZE 128 /* size of ASIX control area */
106#define EMS_PCI_V3_CAN_BASE_OFFSET 0x00 /* offset where the controllers starts */
107#define EMS_PCI_V3_CAN_CTRL_SIZE 0x100 /* memory size for each controller */
108
109#define EMS_PCI_BASE_SIZE  4096 /* size of controller area */
110
111#define PCI_SUBDEVICE_ID_EMS 0x4010
112
113static const struct pci_device_id ems_pci_tbl[] = {
114	/* CPC-PCI v1 */
115	{PCI_VENDOR_ID_SIEMENS, 0x2104, PCI_ANY_ID, PCI_ANY_ID,},
116	/* CPC-PCI v2 */
117	{PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4000},
118	/* CPC-104P v2 */
119	{PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4002},
120	/* CPC-PCIe v3 */
121	{PCI_VENDOR_ID_ASIX, PCI_DEVICE_ID_ASIX_AX99100_LB, 0xa000, PCI_SUBDEVICE_ID_EMS},
122	{0,}
123};
124MODULE_DEVICE_TABLE(pci, ems_pci_tbl);
125
126/* Helper to read internal registers from card logic (not CAN)
 
127 */
128static u8 ems_pci_v1_readb(struct ems_pci_card *card, unsigned int port)
129{
130	return readb(card->base_addr + (port * 4));
131}
132
133static u8 ems_pci_v1_read_reg(const struct sja1000_priv *priv, int port)
134{
135	return readb(priv->reg_base + (port * 4));
136}
137
138static void ems_pci_v1_write_reg(const struct sja1000_priv *priv,
139				 int port, u8 val)
140{
141	writeb(val, priv->reg_base + (port * 4));
142}
143
144static void ems_pci_v1_post_irq(const struct sja1000_priv *priv)
145{
146	struct ems_pci_card *card = priv->priv;
147
148	/* reset int flag of pita */
149	writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
150	       card->conf_addr + PITA2_ICR);
151}
152
153static u8 ems_pci_v2_read_reg(const struct sja1000_priv *priv, int port)
154{
155	return readb(priv->reg_base + port);
156}
157
158static void ems_pci_v2_write_reg(const struct sja1000_priv *priv,
159				 int port, u8 val)
160{
161	writeb(val, priv->reg_base + port);
162}
163
164static void ems_pci_v2_post_irq(const struct sja1000_priv *priv)
165{
166	struct ems_pci_card *card = priv->priv;
167
168	writel(PLX_ICSR_ENA_CLR, card->conf_addr + PLX_ICSR);
169}
170
171static u8 ems_pci_v3_read_reg(const struct sja1000_priv *priv, int port)
172{
173	return readb(priv->reg_base + port);
174}
175
176static void ems_pci_v3_write_reg(const struct sja1000_priv *priv,
177				 int port, u8 val)
178{
179	writeb(val, priv->reg_base + port);
180}
181
182static void ems_pci_v3_post_irq(const struct sja1000_priv *priv)
183{
184	struct ems_pci_card *card = priv->priv;
185
186	writel(ASIX_LINTSR_INT0AC, card->conf_addr + ASIX_LINTSR);
187}
188
189/* Check if a CAN controller is present at the specified location
190 * by trying to set 'em into the PeliCAN mode
191 */
192static inline int ems_pci_check_chan(const struct sja1000_priv *priv)
193{
194	unsigned char res;
195
196	/* Make sure SJA1000 is in reset mode */
197	priv->write_reg(priv, SJA1000_MOD, 1);
198
199	priv->write_reg(priv, SJA1000_CDR, CDR_PELICAN);
200
201	/* read reset-values */
202	res = priv->read_reg(priv, SJA1000_CDR);
203
204	if (res == CDR_PELICAN)
205		return 1;
206
207	return 0;
208}
209
210static void ems_pci_del_card(struct pci_dev *pdev)
211{
212	struct ems_pci_card *card = pci_get_drvdata(pdev);
213	struct net_device *dev;
214	int i = 0;
215
216	for (i = 0; i < card->channels; i++) {
217		dev = card->net_dev[i];
218
219		if (!dev)
220			continue;
221
222		dev_info(&pdev->dev, "Removing %s.\n", dev->name);
223		unregister_sja1000dev(dev);
224		free_sja1000dev(dev);
225	}
226
227	if (card->base_addr)
228		pci_iounmap(card->pci_dev, card->base_addr);
229
230	if (card->conf_addr)
231		pci_iounmap(card->pci_dev, card->conf_addr);
232
233	kfree(card);
234
235	pci_disable_device(pdev);
236}
237
238static void ems_pci_card_reset(struct ems_pci_card *card)
239{
240	/* Request board reset */
241	writeb(0, card->base_addr);
242}
243
244/* Probe PCI device for EMS CAN signature and register each available
 
245 * CAN channel to SJA1000 Socket-CAN subsystem.
246 */
247static int ems_pci_add_card(struct pci_dev *pdev,
248			    const struct pci_device_id *ent)
249{
250	struct sja1000_priv *priv;
251	struct net_device *dev;
252	struct ems_pci_card *card;
253	int max_chan, conf_size, base_bar, conf_bar;
254	int err, i;
255
256	/* Enabling PCI device */
257	if (pci_enable_device(pdev) < 0) {
258		dev_err(&pdev->dev, "Enabling PCI device failed\n");
259		return -ENODEV;
260	}
261
262	/* Allocating card structures to hold addresses, ... */
263	card = kzalloc(sizeof(*card), GFP_KERNEL);
264	if (!card) {
265		pci_disable_device(pdev);
266		return -ENOMEM;
267	}
268
269	pci_set_drvdata(pdev, card);
270
271	card->pci_dev = pdev;
272
273	card->channels = 0;
274
275	if (pdev->vendor == PCI_VENDOR_ID_ASIX) {
276		card->version = 3; /* CPC-PCI v3 */
277		max_chan = EMS_PCI_V3_MAX_CHAN;
278		base_bar = EMS_PCI_V3_BASE_BAR;
279		conf_bar = EMS_PCI_V3_CONF_BAR;
280		conf_size = EMS_PCI_V3_CONF_SIZE;
281	} else if (pdev->vendor == PCI_VENDOR_ID_PLX) {
282		card->version = 2; /* CPC-PCI v2 */
283		max_chan = EMS_PCI_V2_MAX_CHAN;
284		base_bar = EMS_PCI_V2_BASE_BAR;
285		conf_bar = EMS_PCI_V2_CONF_BAR;
286		conf_size = EMS_PCI_V2_CONF_SIZE;
287	} else {
288		card->version = 1; /* CPC-PCI v1 */
289		max_chan = EMS_PCI_V1_MAX_CHAN;
290		base_bar = EMS_PCI_V1_BASE_BAR;
291		conf_bar = EMS_PCI_V1_CONF_BAR;
292		conf_size = EMS_PCI_V1_CONF_SIZE;
293	}
294
295	/* Remap configuration space and controller memory area */
296	card->conf_addr = pci_iomap(pdev, conf_bar, conf_size);
297	if (!card->conf_addr) {
298		err = -ENOMEM;
299		goto failure_cleanup;
300	}
301
302	card->base_addr = pci_iomap(pdev, base_bar, EMS_PCI_BASE_SIZE);
303	if (!card->base_addr) {
304		err = -ENOMEM;
305		goto failure_cleanup;
306	}
307
308	if (card->version == 1) {
309		/* Configure PITA-2 parallel interface (enable MUX) */
310		writel(PITA2_MISC_CONFIG, card->conf_addr + PITA2_MISC);
311
312		/* Check for unique EMS CAN signature */
313		if (ems_pci_v1_readb(card, 0) != 0x55 ||
314		    ems_pci_v1_readb(card, 1) != 0xAA ||
315		    ems_pci_v1_readb(card, 2) != 0x01 ||
316		    ems_pci_v1_readb(card, 3) != 0xCB ||
317		    ems_pci_v1_readb(card, 4) != 0x11) {
318			dev_err(&pdev->dev,
319				"Not EMS Dr. Thomas Wuensche interface\n");
320			err = -ENODEV;
321			goto failure_cleanup;
322		}
323	}
324
325	if (card->version == 3) {
326		/* ASIX chip asserts local reset to CAN controllers
327		 * after bootup until it is deasserted
328		 */
329		writel(readl(card->conf_addr + ASIX_LIEMR) & ~ASIX_LIEMR_LRST,
330		       card->conf_addr + ASIX_LIEMR);
331	}
332
333	ems_pci_card_reset(card);
334
335	/* Detect available channels */
336	for (i = 0; i < max_chan; i++) {
337		dev = alloc_sja1000dev(0);
338		if (!dev) {
339			err = -ENOMEM;
340			goto failure_cleanup;
341		}
342
343		card->net_dev[i] = dev;
344		priv = netdev_priv(dev);
345		priv->priv = card;
346		priv->irq_flags = IRQF_SHARED;
347
348		dev->irq = pdev->irq;
349
 
350		if (card->version == 1) {
351			priv->read_reg  = ems_pci_v1_read_reg;
352			priv->write_reg = ems_pci_v1_write_reg;
353			priv->post_irq  = ems_pci_v1_post_irq;
354			priv->reg_base = card->base_addr + EMS_PCI_V1_CAN_BASE_OFFSET
355					+ (i * EMS_PCI_V1_CAN_CTRL_SIZE);
356		} else if (card->version == 2) {
357			priv->read_reg  = ems_pci_v2_read_reg;
358			priv->write_reg = ems_pci_v2_write_reg;
359			priv->post_irq  = ems_pci_v2_post_irq;
360			priv->reg_base = card->base_addr + EMS_PCI_V2_CAN_BASE_OFFSET
361					+ (i * EMS_PCI_V2_CAN_CTRL_SIZE);
362		} else {
363			priv->read_reg  = ems_pci_v3_read_reg;
364			priv->write_reg = ems_pci_v3_write_reg;
365			priv->post_irq  = ems_pci_v3_post_irq;
366			priv->reg_base = card->base_addr + EMS_PCI_V3_CAN_BASE_OFFSET
367					+ (i * EMS_PCI_V3_CAN_CTRL_SIZE);
368		}
369
370		/* Check if channel is present */
371		if (ems_pci_check_chan(priv)) {
372			priv->can.clock.freq = EMS_PCI_CAN_CLOCK;
373			priv->ocr = EMS_PCI_OCR;
374			priv->cdr = EMS_PCI_CDR;
375
376			SET_NETDEV_DEV(dev, &pdev->dev);
377			dev->dev_id = i;
378
379			if (card->version == 1) {
380				/* reset int flag of pita */
381				writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
382				       card->conf_addr + PITA2_ICR);
383			} else if (card->version == 2) {
384				/* enable IRQ in PLX 9030 */
385				writel(PLX_ICSR_ENA_CLR,
386				       card->conf_addr + PLX_ICSR);
387			} else {
388				/* Enable IRQ in AX99100 */
389				writel(ASIX_LINTSR_INT0AC, card->conf_addr + ASIX_LINTSR);
390				/* Enable local INT0 input enable */
391				writel(readl(card->conf_addr + ASIX_LIEMR) | ASIX_LIEMR_L0EINTEN,
392				       card->conf_addr + ASIX_LIEMR);
393			}
394
395			/* Register SJA1000 device */
396			err = register_sja1000dev(dev);
397			if (err) {
398				dev_err(&pdev->dev,
399					"Registering device failed: %pe\n",
400					ERR_PTR(err));
401				free_sja1000dev(dev);
402				goto failure_cleanup;
403			}
404
405			card->channels++;
406
407			dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d\n",
408				 i + 1, priv->reg_base, dev->irq);
409		} else {
410			free_sja1000dev(dev);
411		}
412	}
413
414	return 0;
415
416failure_cleanup:
417	dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
418
419	ems_pci_del_card(pdev);
420
421	return err;
422}
423
424static struct pci_driver ems_pci_driver = {
425	.name = DRV_NAME,
426	.id_table = ems_pci_tbl,
427	.probe = ems_pci_add_card,
428	.remove = ems_pci_del_card,
429};
430
431module_pci_driver(ems_pci_driver);
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  4 * Copyright (C) 2008 Markus Plessing <plessing@ems-wuensche.com>
  5 * Copyright (C) 2008 Sebastian Haas <haas@ems-wuensche.com>
 
  6 */
  7
  8#include <linux/kernel.h>
  9#include <linux/module.h>
 10#include <linux/interrupt.h>
 11#include <linux/netdevice.h>
 12#include <linux/delay.h>
 13#include <linux/slab.h>
 14#include <linux/pci.h>
 15#include <linux/can/dev.h>
 16#include <linux/io.h>
 17
 18#include "sja1000.h"
 19
 20#define DRV_NAME  "ems_pci"
 21
 22MODULE_AUTHOR("Sebastian Haas <haas@ems-wuenche.com>");
 
 23MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-PCI/PCIe/104P CAN cards");
 24MODULE_SUPPORTED_DEVICE("EMS CPC-PCI/PCIe/104P CAN card");
 25MODULE_LICENSE("GPL v2");
 26
 27#define EMS_PCI_V1_MAX_CHAN 2
 28#define EMS_PCI_V2_MAX_CHAN 4
 
 29#define EMS_PCI_MAX_CHAN    EMS_PCI_V2_MAX_CHAN
 30
 31struct ems_pci_card {
 32	int version;
 33	int channels;
 34
 35	struct pci_dev *pci_dev;
 36	struct net_device *net_dev[EMS_PCI_MAX_CHAN];
 37
 38	void __iomem *conf_addr;
 39	void __iomem *base_addr;
 40};
 41
 42#define EMS_PCI_CAN_CLOCK (16000000 / 2)
 43
 44/*
 45 * Register definitions and descriptions are from LinCAN 0.3.3.
 46 *
 47 * PSB4610 PITA-2 bridge control registers
 48 */
 49#define PITA2_ICR           0x00	/* Interrupt Control Register */
 50#define PITA2_ICR_INT0      0x00000002	/* [RC] INT0 Active/Clear */
 51#define PITA2_ICR_INT0_EN   0x00020000	/* [RW] Enable INT0 */
 52
 53#define PITA2_MISC          0x1c	/* Miscellaneous Register */
 54#define PITA2_MISC_CONFIG   0x04000000	/* Multiplexed parallel interface */
 55
 56/*
 57 * Register definitions for the PLX 9030
 58 */
 59#define PLX_ICSR            0x4c   /* Interrupt Control/Status register */
 60#define PLX_ICSR_LINTI1_ENA 0x0001 /* LINTi1 Enable */
 61#define PLX_ICSR_PCIINT_ENA 0x0040 /* PCI Interrupt Enable */
 62#define PLX_ICSR_LINTI1_CLR 0x0400 /* Local Edge Triggerable Interrupt Clear */
 63#define PLX_ICSR_ENA_CLR    (PLX_ICSR_LINTI1_ENA | PLX_ICSR_PCIINT_ENA | \
 64			     PLX_ICSR_LINTI1_CLR)
 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 EMS_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 7
 79 * (meaning direct oscillator output) because the second SJA1000 chip
 80 * is driven by the first one CLKOUT output.
 81 */
 82#define EMS_PCI_CDR             (CDR_CBP | CDR_CLKOUT_MASK)
 83
 84#define EMS_PCI_V1_BASE_BAR     1
 85#define EMS_PCI_V1_CONF_SIZE    4096 /* size of PITA control area */
 86#define EMS_PCI_V2_BASE_BAR     2
 87#define EMS_PCI_V2_CONF_SIZE    128 /* size of PLX control area */
 88#define EMS_PCI_CAN_BASE_OFFSET 0x400 /* offset where the controllers starts */
 89#define EMS_PCI_CAN_CTRL_SIZE   0x200 /* memory size for each controller */
 
 
 
 
 
 
 
 
 
 
 
 90
 91#define EMS_PCI_BASE_SIZE  4096 /* size of controller area */
 92
 
 
 93static const struct pci_device_id ems_pci_tbl[] = {
 94	/* CPC-PCI v1 */
 95	{PCI_VENDOR_ID_SIEMENS, 0x2104, PCI_ANY_ID, PCI_ANY_ID,},
 96	/* CPC-PCI v2 */
 97	{PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4000},
 98	/* CPC-104P v2 */
 99	{PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4002},
 
 
100	{0,}
101};
102MODULE_DEVICE_TABLE(pci, ems_pci_tbl);
103
104/*
105 * Helper to read internal registers from card logic (not CAN)
106 */
107static u8 ems_pci_v1_readb(struct ems_pci_card *card, unsigned int port)
108{
109	return readb(card->base_addr + (port * 4));
110}
111
112static u8 ems_pci_v1_read_reg(const struct sja1000_priv *priv, int port)
113{
114	return readb(priv->reg_base + (port * 4));
115}
116
117static void ems_pci_v1_write_reg(const struct sja1000_priv *priv,
118				 int port, u8 val)
119{
120	writeb(val, priv->reg_base + (port * 4));
121}
122
123static void ems_pci_v1_post_irq(const struct sja1000_priv *priv)
124{
125	struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
126
127	/* reset int flag of pita */
128	writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
129	       card->conf_addr + PITA2_ICR);
130}
131
132static u8 ems_pci_v2_read_reg(const struct sja1000_priv *priv, int port)
133{
134	return readb(priv->reg_base + port);
135}
136
137static void ems_pci_v2_write_reg(const struct sja1000_priv *priv,
138				 int port, u8 val)
139{
140	writeb(val, priv->reg_base + port);
141}
142
143static void ems_pci_v2_post_irq(const struct sja1000_priv *priv)
144{
145	struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
146
147	writel(PLX_ICSR_ENA_CLR, card->conf_addr + PLX_ICSR);
148}
149
150/*
151 * Check if a CAN controller is present at the specified location
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152 * by trying to set 'em into the PeliCAN mode
153 */
154static inline int ems_pci_check_chan(const struct sja1000_priv *priv)
155{
156	unsigned char res;
157
158	/* Make sure SJA1000 is in reset mode */
159	priv->write_reg(priv, SJA1000_MOD, 1);
160
161	priv->write_reg(priv, SJA1000_CDR, CDR_PELICAN);
162
163	/* read reset-values */
164	res = priv->read_reg(priv, SJA1000_CDR);
165
166	if (res == CDR_PELICAN)
167		return 1;
168
169	return 0;
170}
171
172static void ems_pci_del_card(struct pci_dev *pdev)
173{
174	struct ems_pci_card *card = pci_get_drvdata(pdev);
175	struct net_device *dev;
176	int i = 0;
177
178	for (i = 0; i < card->channels; i++) {
179		dev = card->net_dev[i];
180
181		if (!dev)
182			continue;
183
184		dev_info(&pdev->dev, "Removing %s.\n", dev->name);
185		unregister_sja1000dev(dev);
186		free_sja1000dev(dev);
187	}
188
189	if (card->base_addr != NULL)
190		pci_iounmap(card->pci_dev, card->base_addr);
191
192	if (card->conf_addr != NULL)
193		pci_iounmap(card->pci_dev, card->conf_addr);
194
195	kfree(card);
196
197	pci_disable_device(pdev);
198}
199
200static void ems_pci_card_reset(struct ems_pci_card *card)
201{
202	/* Request board reset */
203	writeb(0, card->base_addr);
204}
205
206/*
207 * Probe PCI device for EMS CAN signature and register each available
208 * CAN channel to SJA1000 Socket-CAN subsystem.
209 */
210static int ems_pci_add_card(struct pci_dev *pdev,
211			    const struct pci_device_id *ent)
212{
213	struct sja1000_priv *priv;
214	struct net_device *dev;
215	struct ems_pci_card *card;
216	int max_chan, conf_size, base_bar;
217	int err, i;
218
219	/* Enabling PCI device */
220	if (pci_enable_device(pdev) < 0) {
221		dev_err(&pdev->dev, "Enabling PCI device failed\n");
222		return -ENODEV;
223	}
224
225	/* Allocating card structures to hold addresses, ... */
226	card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL);
227	if (card == NULL) {
228		pci_disable_device(pdev);
229		return -ENOMEM;
230	}
231
232	pci_set_drvdata(pdev, card);
233
234	card->pci_dev = pdev;
235
236	card->channels = 0;
237
238	if (pdev->vendor == PCI_VENDOR_ID_PLX) {
 
 
 
 
 
 
239		card->version = 2; /* CPC-PCI v2 */
240		max_chan = EMS_PCI_V2_MAX_CHAN;
241		base_bar = EMS_PCI_V2_BASE_BAR;
 
242		conf_size = EMS_PCI_V2_CONF_SIZE;
243	} else {
244		card->version = 1; /* CPC-PCI v1 */
245		max_chan = EMS_PCI_V1_MAX_CHAN;
246		base_bar = EMS_PCI_V1_BASE_BAR;
 
247		conf_size = EMS_PCI_V1_CONF_SIZE;
248	}
249
250	/* Remap configuration space and controller memory area */
251	card->conf_addr = pci_iomap(pdev, 0, conf_size);
252	if (card->conf_addr == NULL) {
253		err = -ENOMEM;
254		goto failure_cleanup;
255	}
256
257	card->base_addr = pci_iomap(pdev, base_bar, EMS_PCI_BASE_SIZE);
258	if (card->base_addr == NULL) {
259		err = -ENOMEM;
260		goto failure_cleanup;
261	}
262
263	if (card->version == 1) {
264		/* Configure PITA-2 parallel interface (enable MUX) */
265		writel(PITA2_MISC_CONFIG, card->conf_addr + PITA2_MISC);
266
267		/* Check for unique EMS CAN signature */
268		if (ems_pci_v1_readb(card, 0) != 0x55 ||
269		    ems_pci_v1_readb(card, 1) != 0xAA ||
270		    ems_pci_v1_readb(card, 2) != 0x01 ||
271		    ems_pci_v1_readb(card, 3) != 0xCB ||
272		    ems_pci_v1_readb(card, 4) != 0x11) {
273			dev_err(&pdev->dev,
274				"Not EMS Dr. Thomas Wuensche interface\n");
275			err = -ENODEV;
276			goto failure_cleanup;
277		}
278	}
279
 
 
 
 
 
 
 
 
280	ems_pci_card_reset(card);
281
282	/* Detect available channels */
283	for (i = 0; i < max_chan; i++) {
284		dev = alloc_sja1000dev(0);
285		if (dev == NULL) {
286			err = -ENOMEM;
287			goto failure_cleanup;
288		}
289
290		card->net_dev[i] = dev;
291		priv = netdev_priv(dev);
292		priv->priv = card;
293		priv->irq_flags = IRQF_SHARED;
294
295		dev->irq = pdev->irq;
296		priv->reg_base = card->base_addr + EMS_PCI_CAN_BASE_OFFSET
297					+ (i * EMS_PCI_CAN_CTRL_SIZE);
298		if (card->version == 1) {
299			priv->read_reg  = ems_pci_v1_read_reg;
300			priv->write_reg = ems_pci_v1_write_reg;
301			priv->post_irq  = ems_pci_v1_post_irq;
302		} else {
 
 
303			priv->read_reg  = ems_pci_v2_read_reg;
304			priv->write_reg = ems_pci_v2_write_reg;
305			priv->post_irq  = ems_pci_v2_post_irq;
 
 
 
 
 
 
 
 
306		}
307
308		/* Check if channel is present */
309		if (ems_pci_check_chan(priv)) {
310			priv->can.clock.freq = EMS_PCI_CAN_CLOCK;
311			priv->ocr = EMS_PCI_OCR;
312			priv->cdr = EMS_PCI_CDR;
313
314			SET_NETDEV_DEV(dev, &pdev->dev);
315			dev->dev_id = i;
316
317			if (card->version == 1)
318				/* reset int flag of pita */
319				writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
320				       card->conf_addr + PITA2_ICR);
321			else
322				/* enable IRQ in PLX 9030 */
323				writel(PLX_ICSR_ENA_CLR,
324				       card->conf_addr + PLX_ICSR);
 
 
 
 
 
 
 
325
326			/* Register SJA1000 device */
327			err = register_sja1000dev(dev);
328			if (err) {
329				dev_err(&pdev->dev, "Registering device failed "
330							"(err=%d)\n", err);
 
331				free_sja1000dev(dev);
332				goto failure_cleanup;
333			}
334
335			card->channels++;
336
337			dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d\n",
338					i + 1, priv->reg_base, dev->irq);
339		} else {
340			free_sja1000dev(dev);
341		}
342	}
343
344	return 0;
345
346failure_cleanup:
347	dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
348
349	ems_pci_del_card(pdev);
350
351	return err;
352}
353
354static struct pci_driver ems_pci_driver = {
355	.name = DRV_NAME,
356	.id_table = ems_pci_tbl,
357	.probe = ems_pci_add_card,
358	.remove = ems_pci_del_card,
359};
360
361module_pci_driver(ems_pci_driver);