]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/sja1000/ems_pci.c
merged branches/netlink in rev. 1037 back to trunk.
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / sja1000 / ems_pci.c
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/version.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 <socketcan/can.h>
28 #include <socketcan/can/dev.h>
29 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
30 #include <linux/io.h>
31 #else
32 #include <asm/io.h>
33 #endif
34
35 #include "sja1000.h"
36
37 #define DRV_NAME  "ems_pci"
38
39 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
40 #error This driver does not support Kernel versions < 2.6.23
41 #endif
42
43 MODULE_AUTHOR("Sebastian Haas <haas@ems-wuenche.com>");
44 MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-PCI/PCIe CAN cards");
45 MODULE_SUPPORTED_DEVICE("EMS CPC-PCI/PCIe CAN card");
46 MODULE_LICENSE("GPL v2");
47
48 #define EMS_PCI_V1_MAX_CHAN 2
49 #define EMS_PCI_V2_MAX_CHAN 4
50 #define EMS_PCI_MAX_CHAN    EMS_PCI_V2_MAX_CHAN
51
52 struct ems_pci_card {
53         int version;
54         int channels;
55
56         struct pci_dev *pci_dev;
57         struct net_device *net_dev[EMS_PCI_MAX_CHAN];
58
59         void __iomem *conf_addr;
60         void __iomem *base_addr;
61 };
62
63 #define EMS_PCI_CAN_CLOCK (16000000 / 2)
64
65 /*
66  * Register definitions and descriptions are from LinCAN 0.3.3.
67  *
68  * PSB4610 PITA-2 bridge control registers
69  */
70 #define PITA2_ICR           0x00        /* Interrupt Control Register */
71 #define PITA2_ICR_INT0      0x00000002  /* [RC] INT0 Active/Clear */
72 #define PITA2_ICR_INT0_EN   0x00020000  /* [RW] Enable INT0 */
73
74 #define PITA2_MISC          0x1c        /* Miscellaneous Register */
75 #define PITA2_MISC_CONFIG   0x04000000  /* Multiplexed parallel interface */
76
77 /*
78  * Register definitions for the PLX 9030
79  */
80 #define PLX_ICSR            0x4c   /* Interrupt Control/Status register */
81 #define PLX_ICSR_LINTI1_ENA 0x0001 /* LINTi1 Enable */
82 #define PLX_ICSR_PCIINT_ENA 0x0040 /* PCI Interrupt Enable */
83 #define PLX_ICSR_LINTI1_CLR 0x0400 /* Local Edge Triggerable Interrupt Clear */
84 #define PLX_ICSR_ENA_CLR    (PLX_ICSR_LINTI1_ENA | PLX_ICSR_PCIINT_ENA | \
85                              PLX_ICSR_LINTI1_CLR)
86
87 /*
88  * The board configuration is probably following:
89  * RX1 is connected to ground.
90  * TX1 is not connected.
91  * CLKO is not connected.
92  * Setting the OCR register to 0xDA is a good idea.
93  * This means normal output mode, push-pull and the correct polarity.
94  */
95 #define EMS_PCI_OCR         (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
96
97 /*
98  * In the CDR register, you should set CBP to 1.
99  * You will probably also want to set the clock divider value to 7
100  * (meaning direct oscillator output) because the second SJA1000 chip
101  * is driven by the first one CLKOUT output.
102  */
103 #define EMS_PCI_CDR             (CDR_CBP | CDR_CLKOUT_MASK)
104
105 #define EMS_PCI_V1_BASE_BAR     1
106 #define EMS_PCI_V1_MEM_SIZE     4096
107 #define EMS_PCI_V2_BASE_BAR     2
108 #define EMS_PCI_V2_MEM_SIZE     128
109 #define EMS_PCI_CAN_BASE_OFFSET 0x400 /* offset where the controllers starts */
110 #define EMS_PCI_CAN_CTRL_SIZE   0x200 /* memory size for each controller */
111
112 static struct pci_device_id ems_pci_tbl[] = {
113         /* CPC-PCI v1 */
114         {PCI_VENDOR_ID_SIEMENS, 0x2104, PCI_ANY_ID, PCI_ANY_ID,},
115         /* CPC-PCI v2 */
116         {PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4000},
117         /* CPC-104P v2 */
118         {PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4002},
119         {0,}
120 };
121 MODULE_DEVICE_TABLE(pci, ems_pci_tbl);
122
123 /*
124  * Helper to read internal registers from card logic (not CAN)
125  */
126 static u8 ems_pci_v1_readb(struct ems_pci_card *card, unsigned int port)
127 {
128         return readb(card->base_addr + (port * 4));
129 }
130
131 static u8 ems_pci_v1_read_reg(const struct sja1000_priv *priv, int port)
132 {
133         return readb(priv->reg_base + (port * 4));
134 }
135
136 static void ems_pci_v1_write_reg(const struct sja1000_priv *priv,
137                                  int port, u8 val)
138 {
139         writeb(val, priv->reg_base + (port * 4));
140 }
141
142 static void ems_pci_v1_post_irq(const struct sja1000_priv *priv)
143 {
144         struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
145
146         /* reset int flag of pita */
147         writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
148                card->conf_addr + PITA2_ICR);
149 }
150
151 static u8 ems_pci_v2_read_reg(const struct sja1000_priv *priv, int port)
152 {
153         return readb(priv->reg_base + port);
154 }
155
156 static void ems_pci_v2_write_reg(const struct sja1000_priv *priv,
157                                  int port, u8 val)
158 {
159         writeb(val, priv->reg_base + port);
160 }
161
162 static void ems_pci_v2_post_irq(const struct sja1000_priv *priv)
163 {
164         struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
165
166         writel(PLX_ICSR_ENA_CLR, card->conf_addr + PLX_ICSR);
167 }
168
169 /*
170  * Check if a CAN controller is present at the specified location
171  * by trying to set 'em into the PeliCAN mode
172  */
173 static inline int ems_pci_check_chan(const struct sja1000_priv *priv)
174 {
175         unsigned char res;
176
177         /* Make sure SJA1000 is in reset mode */
178         priv->write_reg(priv, REG_MOD, 1);
179
180         priv->write_reg(priv, REG_CDR, CDR_PELICAN);
181
182         /* read reset-values */
183         res = priv->read_reg(priv, REG_CDR);
184
185         if (res == CDR_PELICAN)
186                 return 1;
187
188         return 0;
189 }
190
191 static void ems_pci_del_card(struct pci_dev *pdev)
192 {
193         struct ems_pci_card *card = pci_get_drvdata(pdev);
194         struct net_device *dev;
195         int i = 0;
196
197         for (i = 0; i < card->channels; i++) {
198                 dev = card->net_dev[i];
199
200                 if (!dev)
201                         continue;
202
203                 dev_info(&pdev->dev, "Removing %s.\n", dev->name);
204                 unregister_sja1000dev(dev);
205                 free_sja1000dev(dev);
206         }
207
208         if (card->base_addr != NULL)
209                 pci_iounmap(card->pci_dev, card->base_addr);
210
211         if (card->conf_addr != NULL)
212                 pci_iounmap(card->pci_dev, card->conf_addr);
213
214         kfree(card);
215
216         pci_disable_device(pdev);
217         pci_set_drvdata(pdev, NULL);
218 }
219
220 static void ems_pci_card_reset(struct ems_pci_card *card)
221 {
222         /* Request board reset */
223         writeb(0, card->base_addr);
224 }
225
226 /*
227  * Probe PCI device for EMS CAN signature and register each available
228  * CAN channel to SJA1000 Socket-CAN subsystem.
229  */
230 static int __devinit ems_pci_add_card(struct pci_dev *pdev,
231                                         const struct pci_device_id *ent)
232 {
233         struct sja1000_priv *priv;
234         struct net_device *dev;
235         struct ems_pci_card *card;
236         int max_chan, mem_size, base_bar;
237         int err, i;
238
239         /* Enabling PCI device */
240         if (pci_enable_device(pdev) < 0) {
241                 dev_err(&pdev->dev, "Enabling PCI device failed\n");
242                 return -ENODEV;
243         }
244
245         /* Allocating card structures to hold addresses, ... */
246         card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL);
247         if (card == NULL) {
248                 dev_err(&pdev->dev, "Unable to allocate memory\n");
249                 pci_disable_device(pdev);
250                 return -ENOMEM;
251         }
252
253         pci_set_drvdata(pdev, card);
254
255         card->pci_dev = pdev;
256
257         card->channels = 0;
258
259         if (pdev->vendor == PCI_VENDOR_ID_PLX) {
260                 card->version = 2; /* CPC-PCI v2 */
261                 max_chan = EMS_PCI_V2_MAX_CHAN;
262                 base_bar = EMS_PCI_V2_BASE_BAR;
263                 mem_size = EMS_PCI_V2_MEM_SIZE;
264         } else {
265                 card->version = 1; /* CPC-PCI v1 */
266                 max_chan = EMS_PCI_V1_MAX_CHAN;
267                 base_bar = EMS_PCI_V1_BASE_BAR;
268                 mem_size = EMS_PCI_V1_MEM_SIZE;
269         }
270
271         /* Remap configuration space and controller memory area */
272         card->conf_addr = pci_iomap(pdev, 0, mem_size);
273         if (card->conf_addr == NULL) {
274                 err = -ENOMEM;
275                 goto failure_cleanup;
276         }
277
278         card->base_addr = pci_iomap(pdev, base_bar, mem_size);
279         if (card->base_addr == NULL) {
280                 err = -ENOMEM;
281                 goto failure_cleanup;
282         }
283
284         if (card->version == 1) {
285                 /* Configure PITA-2 parallel interface (enable MUX) */
286                 writel(PITA2_MISC_CONFIG, card->conf_addr + PITA2_MISC);
287
288                 /* Check for unique EMS CAN signature */
289                 if (ems_pci_v1_readb(card, 0) != 0x55 ||
290                     ems_pci_v1_readb(card, 1) != 0xAA ||
291                     ems_pci_v1_readb(card, 2) != 0x01 ||
292                     ems_pci_v1_readb(card, 3) != 0xCB ||
293                     ems_pci_v1_readb(card, 4) != 0x11) {
294                         dev_err(&pdev->dev,
295                                 "Not EMS Dr. Thomas Wuensche interface\n");
296                         err = -ENODEV;
297                         goto failure_cleanup;
298                 }
299         }
300
301         ems_pci_card_reset(card);
302
303         /* Detect available channels */
304         for (i = 0; i < max_chan; i++) {
305                 dev = alloc_sja1000dev(0);
306                 if (dev == NULL) {
307                         err = -ENOMEM;
308                         goto failure_cleanup;
309                 }
310
311                 card->net_dev[i] = dev;
312                 priv = netdev_priv(dev);
313                 priv->priv = card;
314 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
315                 priv->irq_flags = SA_SHIRQ;
316 #else
317                 priv->irq_flags = IRQF_SHARED;
318 #endif
319
320                 dev->irq = pdev->irq;
321                 priv->reg_base = card->base_addr + EMS_PCI_CAN_BASE_OFFSET
322                                         + (i * EMS_PCI_CAN_CTRL_SIZE);
323                 if (card->version == 1) {
324                         priv->read_reg  = ems_pci_v1_read_reg;
325                         priv->write_reg = ems_pci_v1_write_reg;
326                         priv->post_irq  = ems_pci_v1_post_irq;
327                 } else {
328                         priv->read_reg  = ems_pci_v2_read_reg;
329                         priv->write_reg = ems_pci_v2_write_reg;
330                         priv->post_irq  = ems_pci_v2_post_irq;
331                 }
332
333                 /* Check if channel is present */
334                 if (ems_pci_check_chan(priv)) {
335                         priv->can.clock.freq = EMS_PCI_CAN_CLOCK;
336                         priv->ocr = EMS_PCI_OCR;
337                         priv->cdr = EMS_PCI_CDR;
338
339                         SET_NETDEV_DEV(dev, &pdev->dev);
340
341                         if (card->version == 1)
342                                 /* reset int flag of pita */
343                                 writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
344                                        card->conf_addr + PITA2_ICR);
345                         else
346                                 /* enable IRQ in PLX 9030 */
347                                 writel(PLX_ICSR_ENA_CLR,
348                                        card->conf_addr + PLX_ICSR);
349
350                         /* Register SJA1000 device */
351                         err = register_sja1000dev(dev);
352                         if (err) {
353                                 dev_err(&pdev->dev, "Registering device failed "
354                                                         "(err=%d)\n", err);
355                                 free_sja1000dev(dev);
356                                 goto failure_cleanup;
357                         }
358
359                         card->channels++;
360
361                         dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d\n",
362                                         i + 1, priv->reg_base, dev->irq);
363                 } else {
364                         free_sja1000dev(dev);
365                 }
366         }
367
368         return 0;
369
370 failure_cleanup:
371         dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
372
373         ems_pci_del_card(pdev);
374
375         return err;
376 }
377
378 static struct pci_driver ems_pci_driver = {
379         .name = DRV_NAME,
380         .id_table = ems_pci_tbl,
381         .probe = ems_pci_add_card,
382         .remove = ems_pci_del_card,
383 };
384
385 static int __init ems_pci_init(void)
386 {
387         return pci_register_driver(&ems_pci_driver);
388 }
389
390 static void __exit ems_pci_exit(void)
391 {
392         pci_unregister_driver(&ems_pci_driver);
393 }
394
395 module_init(ems_pci_init);
396 module_exit(ems_pci_exit);
397