ems_pci.c revision 255a9154319d3cf475d527458037758935f6445b
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(card->base_addr + (port * EMS_PCI_PORT_BYTES));
103}
104
105static u8 ems_pci_read_reg(const struct sja1000_priv *priv, int port)
106{
107	return readb(priv->reg_base + (port * EMS_PCI_PORT_BYTES));
108}
109
110static void ems_pci_write_reg(const struct sja1000_priv *priv, int port, u8 val)
111{
112	writeb(val, priv->reg_base + (port * EMS_PCI_PORT_BYTES));
113}
114
115static void ems_pci_post_irq(const struct sja1000_priv *priv)
116{
117	struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
118
119	/* reset int flag of pita */
120	writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0, card->conf_addr
121		+ PITA2_ICR);
122}
123
124/*
125 * Check if a CAN controller is present at the specified location
126 * by trying to set 'em into the PeliCAN mode
127 */
128static inline int ems_pci_check_chan(const struct sja1000_priv *priv)
129{
130	unsigned char res;
131
132	/* Make sure SJA1000 is in reset mode */
133	ems_pci_write_reg(priv, REG_MOD, 1);
134
135	ems_pci_write_reg(priv, REG_CDR, CDR_PELICAN);
136
137	/* read reset-values */
138	res = ems_pci_read_reg(priv, REG_CDR);
139
140	if (res == CDR_PELICAN)
141		return 1;
142
143	return 0;
144}
145
146static void ems_pci_del_card(struct pci_dev *pdev)
147{
148	struct ems_pci_card *card = pci_get_drvdata(pdev);
149	struct net_device *dev;
150	int i = 0;
151
152	for (i = 0; i < card->channels; i++) {
153		dev = card->net_dev[i];
154
155		if (!dev)
156			continue;
157
158		dev_info(&pdev->dev, "Removing %s.\n", dev->name);
159		unregister_sja1000dev(dev);
160		free_sja1000dev(dev);
161	}
162
163	if (card->base_addr != NULL)
164		pci_iounmap(card->pci_dev, card->base_addr);
165
166	if (card->conf_addr != NULL)
167		pci_iounmap(card->pci_dev, card->conf_addr);
168
169	kfree(card);
170
171	pci_disable_device(pdev);
172	pci_set_drvdata(pdev, NULL);
173}
174
175static void ems_pci_card_reset(struct ems_pci_card *card)
176{
177	/* Request board reset */
178	writeb(0, card->base_addr);
179}
180
181/*
182 * Probe PCI device for EMS CAN signature and register each available
183 * CAN channel to SJA1000 Socket-CAN subsystem.
184 */
185static int __devinit ems_pci_add_card(struct pci_dev *pdev,
186					const struct pci_device_id *ent)
187{
188	struct sja1000_priv *priv;
189	struct net_device *dev;
190	struct ems_pci_card *card;
191	int err, i;
192
193	/* Enabling PCI device */
194	if (pci_enable_device(pdev) < 0) {
195		dev_err(&pdev->dev, "Enabling PCI device failed\n");
196		return -ENODEV;
197	}
198
199	/* Allocating card structures to hold addresses, ... */
200	card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL);
201	if (card == NULL) {
202		dev_err(&pdev->dev, "Unable to allocate memory\n");
203		pci_disable_device(pdev);
204		return -ENOMEM;
205	}
206
207	pci_set_drvdata(pdev, card);
208
209	card->pci_dev = pdev;
210
211	card->channels = 0;
212
213	/* Remap PITA configuration space, and controller memory area */
214	card->conf_addr = pci_iomap(pdev, 0, EMS_PCI_MEM_SIZE);
215	if (card->conf_addr == NULL) {
216		err = -ENOMEM;
217		goto failure_cleanup;
218	}
219
220	card->base_addr = pci_iomap(pdev, 1, EMS_PCI_MEM_SIZE);
221	if (card->base_addr == NULL) {
222		err = -ENOMEM;
223		goto failure_cleanup;
224	}
225
226	/* Configure PITA-2 parallel interface (enable MUX) */
227	writel(PITA2_MISC_CONFIG, card->conf_addr + PITA2_MISC);
228
229	/* Check for unique EMS CAN signature */
230	if (ems_pci_readb(card, 0) != 0x55 ||
231	    ems_pci_readb(card, 1) != 0xAA ||
232	    ems_pci_readb(card, 2) != 0x01 ||
233	    ems_pci_readb(card, 3) != 0xCB ||
234	    ems_pci_readb(card, 4) != 0x11) {
235		dev_err(&pdev->dev, "Not EMS Dr. Thomas Wuensche interface\n");
236		err = -ENODEV;
237		goto failure_cleanup;
238	}
239
240	ems_pci_card_reset(card);
241
242	/* Detect available channels */
243	for (i = 0; i < EMS_PCI_MAX_CHAN; i++) {
244		dev = alloc_sja1000dev(0);
245		if (dev == NULL) {
246			err = -ENOMEM;
247			goto failure_cleanup;
248		}
249
250		card->net_dev[i] = dev;
251		priv = netdev_priv(dev);
252		priv->priv = card;
253		priv->irq_flags = IRQF_SHARED;
254
255		dev->irq = pdev->irq;
256		priv->reg_base = card->base_addr + EMS_PCI_CAN_BASE_OFFSET
257					+ (i * EMS_PCI_CAN_CTRL_SIZE);
258
259		/* Check if channel is present */
260		if (ems_pci_check_chan(priv)) {
261			priv->read_reg  = ems_pci_read_reg;
262			priv->write_reg = ems_pci_write_reg;
263			priv->post_irq  = ems_pci_post_irq;
264			priv->can.clock.freq = EMS_PCI_CAN_CLOCK;
265			priv->ocr = EMS_PCI_OCR;
266			priv->cdr = EMS_PCI_CDR;
267
268			SET_NETDEV_DEV(dev, &pdev->dev);
269
270			/* Enable interrupts from card */
271			writel(PITA2_ICR_INT0_EN, card->conf_addr + PITA2_ICR);
272
273			/* Register SJA1000 device */
274			err = register_sja1000dev(dev);
275			if (err) {
276				dev_err(&pdev->dev, "Registering device failed "
277							"(err=%d)\n", err);
278				free_sja1000dev(dev);
279				goto failure_cleanup;
280			}
281
282			card->channels++;
283
284			dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d\n",
285					i + 1, priv->reg_base, dev->irq);
286		} else {
287			free_sja1000dev(dev);
288		}
289	}
290
291	return 0;
292
293failure_cleanup:
294	dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
295
296	ems_pci_del_card(pdev);
297
298	return err;
299}
300
301static struct pci_driver ems_pci_driver = {
302	.name = DRV_NAME,
303	.id_table = ems_pci_tbl,
304	.probe = ems_pci_add_card,
305	.remove = ems_pci_del_card,
306};
307
308static int __init ems_pci_init(void)
309{
310	return pci_register_driver(&ems_pci_driver);
311}
312
313static void __exit ems_pci_exit(void)
314{
315	pci_unregister_driver(&ems_pci_driver);
316}
317
318module_init(ems_pci_init);
319module_exit(ems_pci_exit);
320
321