ems_pci.c revision a61a8423c71d6da6f1f6f854d8adcea24b80bef6
1/*
2 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
3 * Copyright (C) 2008 Markus Plessing <plessing@ems-wuensche.com>
4 * Copyright (C) 2008 Sebastian Haas <haas@ems-wuensche.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the version 2 of the GNU General Public License
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/netdevice.h>
24#include <linux/delay.h>
25#include <linux/pci.h>
26#include <linux/can.h>
27#include <linux/can/dev.h>
28#include <linux/io.h>
29
30#include "sja1000.h"
31
32#define DRV_NAME  "ems_pci"
33
34MODULE_AUTHOR("Sebastian Haas <haas@ems-wuenche.com>");
35MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-PCI/PCIe CAN cards");
36MODULE_SUPPORTED_DEVICE("EMS CPC-PCI/PCIe CAN card");
37MODULE_LICENSE("GPL v2");
38
39#define EMS_PCI_MAX_CHAN 2
40
41struct ems_pci_card {
42	int channels;
43
44	struct pci_dev *pci_dev;
45	struct net_device *net_dev[EMS_PCI_MAX_CHAN];
46
47	void __iomem *conf_addr;
48	void __iomem *base_addr;
49};
50
51#define EMS_PCI_CAN_CLOCK (16000000 / 2)
52
53/*
54 * Register definitions and descriptions are from LinCAN 0.3.3.
55 *
56 * PSB4610 PITA-2 bridge control registers
57 */
58#define PITA2_ICR           0x00	/* Interrupt Control Register */
59#define PITA2_ICR_INT0      0x00000002	/* [RC] INT0 Active/Clear */
60#define PITA2_ICR_INT0_EN   0x00020000	/* [RW] Enable INT0 */
61
62#define PITA2_MISC          0x1c	/* Miscellaneous Register */
63#define PITA2_MISC_CONFIG   0x04000000	/* Multiplexed parallel interface */
64
65/*
66 * The board configuration is probably following:
67 * RX1 is connected to ground.
68 * TX1 is not connected.
69 * CLKO is not connected.
70 * Setting the OCR register to 0xDA is a good idea.
71 * This means  normal output mode , push-pull and the correct polarity.
72 */
73#define EMS_PCI_OCR         (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
74
75/*
76 * In the CDR register, you should set CBP to 1.
77 * You will probably also want to set the clock divider value to 7
78 * (meaning direct oscillator output) because the second SJA1000 chip
79 * is driven by the first one CLKOUT output.
80 */
81#define EMS_PCI_CDR             (CDR_CBP | CDR_CLKOUT_MASK)
82#define EMS_PCI_MEM_SIZE        4096  /* Size of the remapped io-memory */
83#define EMS_PCI_CAN_BASE_OFFSET 0x400 /* offset where the controllers starts */
84#define EMS_PCI_CAN_CTRL_SIZE   0x200 /* memory size for each controller */
85
86#define EMS_PCI_PORT_BYTES  0x4     /* Each register occupies 4 bytes */
87
88#define EMS_PCI_VENDOR_ID   0x110a  /* PCI device and vendor ID */
89#define EMS_PCI_DEVICE_ID   0x2104
90
91static struct pci_device_id ems_pci_tbl[] = {
92	{EMS_PCI_VENDOR_ID, EMS_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
93	{0,}
94};
95MODULE_DEVICE_TABLE(pci, ems_pci_tbl);
96
97/*
98 * Helper to read internal registers from card logic (not CAN)
99 */
100static u8 ems_pci_readb(struct ems_pci_card *card, unsigned int port)
101{
102	return readb((void __iomem *)card->base_addr
103			+ (port * EMS_PCI_PORT_BYTES));
104}
105
106static u8 ems_pci_read_reg(const struct net_device *dev, int port)
107{
108	return readb((void __iomem *)dev->base_addr
109			+ (port * EMS_PCI_PORT_BYTES));
110}
111
112static void ems_pci_write_reg(const struct net_device *dev, int port, u8 val)
113{
114	writeb(val, (void __iomem *)dev->base_addr
115		+ (port * EMS_PCI_PORT_BYTES));
116}
117
118static void ems_pci_post_irq(const struct net_device *dev)
119{
120	struct sja1000_priv *priv = netdev_priv(dev);
121	struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
122
123	/* reset int flag of pita */
124	writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0, card->conf_addr
125		+ PITA2_ICR);
126}
127
128/*
129 * Check if a CAN controller is present at the specified location
130 * by trying to set 'em into the PeliCAN mode
131 */
132static inline int ems_pci_check_chan(struct net_device *dev)
133{
134	unsigned char res;
135
136	/* Make sure SJA1000 is in reset mode */
137	ems_pci_write_reg(dev, REG_MOD, 1);
138
139	ems_pci_write_reg(dev, REG_CDR, CDR_PELICAN);
140
141	/* read reset-values */
142	res = ems_pci_read_reg(dev, REG_CDR);
143
144	if (res == CDR_PELICAN)
145		return 1;
146
147	return 0;
148}
149
150static void ems_pci_del_card(struct pci_dev *pdev)
151{
152	struct ems_pci_card *card = pci_get_drvdata(pdev);
153	struct net_device *dev;
154	int i = 0;
155
156	for (i = 0; i < card->channels; i++) {
157		dev = card->net_dev[i];
158
159		if (!dev)
160			continue;
161
162		dev_info(&pdev->dev, "Removing %s.\n", dev->name);
163		unregister_sja1000dev(dev);
164		free_sja1000dev(dev);
165	}
166
167	if (card->base_addr != NULL)
168		pci_iounmap(card->pci_dev, card->base_addr);
169
170	if (card->conf_addr != NULL)
171		pci_iounmap(card->pci_dev, card->conf_addr);
172
173	kfree(card);
174
175	pci_disable_device(pdev);
176	pci_set_drvdata(pdev, NULL);
177}
178
179static void ems_pci_card_reset(struct ems_pci_card *card)
180{
181	/* Request board reset */
182	writeb(0, card->base_addr);
183}
184
185/*
186 * Probe PCI device for EMS CAN signature and register each available
187 * CAN channel to SJA1000 Socket-CAN subsystem.
188 */
189static int __devinit ems_pci_add_card(struct pci_dev *pdev,
190					const struct pci_device_id *ent)
191{
192	struct sja1000_priv *priv;
193	struct net_device *dev;
194	struct ems_pci_card *card;
195	int err, i;
196
197	/* Enabling PCI device */
198	if (pci_enable_device(pdev) < 0) {
199		dev_err(&pdev->dev, "Enabling PCI device failed\n");
200		return -ENODEV;
201	}
202
203	/* Allocating card structures to hold addresses, ... */
204	card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL);
205	if (card == NULL) {
206		dev_err(&pdev->dev, "Unable to allocate memory\n");
207		pci_disable_device(pdev);
208		return -ENOMEM;
209	}
210
211	pci_set_drvdata(pdev, card);
212
213	card->pci_dev = pdev;
214
215	card->channels = 0;
216
217	/* Remap PITA configuration space, and controller memory area */
218	card->conf_addr = pci_iomap(pdev, 0, EMS_PCI_MEM_SIZE);
219	if (card->conf_addr == NULL) {
220		err = -ENOMEM;
221
222		goto failure_cleanup;
223	}
224
225	card->base_addr = pci_iomap(pdev, 1, EMS_PCI_MEM_SIZE);
226	if (card->base_addr == NULL) {
227		err = -ENOMEM;
228
229		goto failure_cleanup;
230	}
231
232	/* Configure PITA-2 parallel interface (enable MUX) */
233	writel(PITA2_MISC_CONFIG, card->conf_addr + PITA2_MISC);
234
235	/* Check for unique EMS CAN signature */
236	if (ems_pci_readb(card, 0) != 0x55 ||
237	    ems_pci_readb(card, 1) != 0xAA ||
238	    ems_pci_readb(card, 2) != 0x01 ||
239	    ems_pci_readb(card, 3) != 0xCB ||
240	    ems_pci_readb(card, 4) != 0x11) {
241		dev_err(&pdev->dev, "Not EMS Dr. Thomas Wuensche interface\n");
242
243		err = -ENODEV;
244		goto failure_cleanup;
245	}
246
247	ems_pci_card_reset(card);
248
249	/* Detect available channels */
250	for (i = 0; i < EMS_PCI_MAX_CHAN; i++) {
251		dev = alloc_sja1000dev(0);
252		if (dev == NULL) {
253			err = -ENOMEM;
254			goto failure_cleanup;
255		}
256
257		card->net_dev[i] = dev;
258		priv = netdev_priv(dev);
259		priv->priv = card;
260		priv->irq_flags = IRQF_SHARED;
261
262		dev->irq = pdev->irq;
263		dev->base_addr = (unsigned long)(card->base_addr
264						+ EMS_PCI_CAN_BASE_OFFSET
265						+ (i * EMS_PCI_CAN_CTRL_SIZE));
266
267		/* Check if channel is present */
268		if (ems_pci_check_chan(dev)) {
269			priv->read_reg  = ems_pci_read_reg;
270			priv->write_reg = ems_pci_write_reg;
271			priv->post_irq  = ems_pci_post_irq;
272			priv->can.clock.freq = EMS_PCI_CAN_CLOCK;
273			priv->ocr = EMS_PCI_OCR;
274			priv->cdr = EMS_PCI_CDR;
275
276			SET_NETDEV_DEV(dev, &pdev->dev);
277
278			/* Enable interrupts from card */
279			writel(PITA2_ICR_INT0_EN, card->conf_addr + PITA2_ICR);
280
281			/* Register SJA1000 device */
282			err = register_sja1000dev(dev);
283			if (err) {
284				dev_err(&pdev->dev, "Registering device failed "
285							"(err=%d)\n", err);
286				free_sja1000dev(dev);
287				goto failure_cleanup;
288			}
289
290			card->channels++;
291
292			dev_info(&pdev->dev, "Channel #%d at %#lX, irq %d\n",
293						i + 1, dev->base_addr,
294						dev->irq);
295		} else {
296			free_sja1000dev(dev);
297		}
298	}
299
300	return 0;
301
302failure_cleanup:
303	dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
304
305	ems_pci_del_card(pdev);
306
307	return err;
308}
309
310static struct pci_driver ems_pci_driver = {
311	.name = DRV_NAME,
312	.id_table = ems_pci_tbl,
313	.probe = ems_pci_add_card,
314	.remove = ems_pci_del_card,
315};
316
317static int __init ems_pci_init(void)
318{
319	return pci_register_driver(&ems_pci_driver);
320}
321
322static void __exit ems_pci_exit(void)
323{
324	pci_unregister_driver(&ems_pci_driver);
325}
326
327module_init(ems_pci_init);
328module_exit(ems_pci_exit);
329
330