]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/sja1000/peak_pci.c
Added PCI device ID for the PCAN Express Card hardware.
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / sja1000 / peak_pci.c
1 /*
2  * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
3  *
4  * Derived from the PCAN project file driver/src/pcan_pci.c:
5  *
6  * Copyright (C) 2001-2006  PEAK System-Technik GmbH
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the version 2 of the GNU General Public License
10  * as published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/version.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/netdevice.h>
27 #include <linux/delay.h>
28 #include <linux/pci.h>
29 #include <socketcan/can.h>
30 #include <socketcan/can/dev.h>
31 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
32 #include <linux/io.h>
33 #else
34 #include <asm/io.h>
35 #endif
36
37 #include "sja1000.h"
38
39 #define DRV_NAME  "peak_pci"
40
41 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
42 #error This driver does not support Kernel versions < 2.6.23
43 #endif
44
45 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
46 MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI/PCIe cards");
47 MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe CAN card");
48 MODULE_LICENSE("GPL v2");
49
50 struct peak_pci {
51         int channel;
52         struct pci_dev *pci_dev;
53         struct net_device *slave_dev;
54         volatile void __iomem *conf_addr;
55 };
56
57 #define PEAK_PCI_SINGLE      0  /* single channel device */
58 #define PEAK_PCI_MASTER      1  /* multi channel master device */
59 #define PEAK_PCI_SLAVE       2  /* multi channel slave device */
60
61 #define PEAK_PCI_CAN_CLOCK   (16000000 / 2)
62
63 #define PEAK_PCI_CDR_SINGLE  (CDR_CBP | CDR_CLKOUT_MASK | CDR_CLK_OFF)
64 #define PEAK_PCI_CDR_MASTER  (CDR_CBP | CDR_CLKOUT_MASK)
65
66 #define PEAK_PCI_OCR         OCR_TX0_PUSHPULL
67
68 /*
69  * Important PITA registers
70  */
71 #define PITA_ICR             0x00       /* interrupt control register */
72 #define PITA_GPIOICR         0x18       /* general purpose I/O interface
73                                            control register */
74 #define PITA_MISC            0x1C       /* miscellanoes register */
75
76 #define PCI_CONFIG_PORT_SIZE 0x1000     /* size of the config io-memory */
77 #define PCI_PORT_SIZE        0x0400     /* size of a channel io-memory */
78
79 #define PEAK_PCI_VENDOR_ID   0x001C     /* the PCI device and vendor IDs */
80 #define PEAK_PCI_DEVICE_ID   0x0001     /* PCAN PCI and PCIe slot cards */
81 #define PEAK_PCIE_CARD_ID    0x0002     /* PCAN ExpressCard */
82
83 /* TODO: Add LED Status support for PCAN ExpressCard */
84
85 static struct pci_device_id peak_pci_tbl[] = {
86         {PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
87         {PEAK_PCI_VENDOR_ID, PEAK_PCIE_CARD_ID, PCI_ANY_ID, PCI_ANY_ID,},
88         {0,}
89 };
90
91 MODULE_DEVICE_TABLE(pci, peak_pci_tbl);
92
93 static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port)
94 {
95         return readb(priv->reg_base + (port << 2));
96 }
97
98 static void peak_pci_write_reg(const struct sja1000_priv *priv,
99                                int port, u8 val)
100 {
101         writeb(val, priv->reg_base + (port << 2));
102 }
103
104 static void peak_pci_post_irq(const struct sja1000_priv *priv)
105 {
106         struct peak_pci *board = priv->priv;
107         u16 icr_low;
108
109         /* Select and clear in Pita stored interrupt */
110         icr_low = readw(board->conf_addr + PITA_ICR);
111         if (board->channel == PEAK_PCI_SLAVE) {
112                 if (icr_low & 0x0001)
113                         writew(0x0001, board->conf_addr + PITA_ICR);
114         } else {
115                 if (icr_low & 0x0002)
116                         writew(0x0002, board->conf_addr + PITA_ICR);
117         }
118 }
119
120 static void peak_pci_del_chan(struct net_device *dev, int init_step)
121 {
122         struct sja1000_priv *priv = netdev_priv(dev);
123         struct peak_pci *board;
124         u16 icr_high;
125
126         if (!dev)
127                 return;
128         priv = netdev_priv(dev);
129         if (!priv)
130                 return;
131         board = priv->priv;
132         if (!board)
133                 return;
134
135         switch (init_step) {
136         case 0:         /* Full cleanup */
137                 printk(KERN_INFO "Removing %s device %s\n",
138                        DRV_NAME, dev->name);
139                 unregister_sja1000dev(dev);
140         case 4:
141                 icr_high = readw(board->conf_addr + PITA_ICR + 2);
142                 if (board->channel == PEAK_PCI_SLAVE)
143                         icr_high &= ~0x0001;
144                 else
145                         icr_high &= ~0x0002;
146                 writew(icr_high, board->conf_addr + PITA_ICR + 2);
147         case 3:
148                 iounmap(priv->reg_base);
149         case 2:
150                 if (board->channel != PEAK_PCI_SLAVE)
151                         iounmap((void *)board->conf_addr);
152         case 1:
153                 free_sja1000dev(dev);
154                 break;
155         }
156
157 }
158
159 static int peak_pci_add_chan(struct pci_dev *pdev, int channel,
160                              struct net_device **master_dev)
161 {
162         struct net_device *dev;
163         struct sja1000_priv *priv;
164         struct peak_pci *board;
165         u16 icr_high;
166         unsigned long addr;
167         int err, init_step;
168
169         dev = alloc_sja1000dev(sizeof(struct peak_pci));
170         if (dev == NULL)
171                 return -ENOMEM;
172         init_step = 1;
173
174         priv = netdev_priv(dev);
175         board = priv->priv;
176
177         board->pci_dev = pdev;
178         board->channel = channel;
179
180         if (channel != PEAK_PCI_SLAVE) {
181
182                 addr = pci_resource_start(pdev, 0);
183                 board->conf_addr = ioremap(addr, PCI_CONFIG_PORT_SIZE);
184                 if (board->conf_addr == 0) {
185                         err = -ENODEV;
186                         goto failure;
187                 }
188                 init_step = 2;
189
190                 /* Set GPIO control register */
191                 writew(0x0005, board->conf_addr + PITA_GPIOICR + 2);
192
193                 /* Enable single or dual channel */
194                 if (channel == PEAK_PCI_MASTER)
195                         writeb(0x00, board->conf_addr + PITA_GPIOICR);
196                 else
197                         writeb(0x04, board->conf_addr + PITA_GPIOICR);
198                 /* Toggle reset */
199                 writeb(0x05, board->conf_addr + PITA_MISC + 3);
200                 mdelay(5);
201                 /* Leave parport mux mode */
202                 writeb(0x04, board->conf_addr + PITA_MISC + 3);
203         } else {
204                 struct sja1000_priv *master_priv = netdev_priv(*master_dev);
205                 struct peak_pci *master_board = master_priv->priv;
206                 master_board->slave_dev = dev;
207                 board->conf_addr = master_board->conf_addr;
208         }
209
210         addr = pci_resource_start(pdev, 1);
211         if (channel == PEAK_PCI_SLAVE)
212                 addr += PCI_PORT_SIZE;
213
214         priv->reg_base = ioremap(addr, PCI_PORT_SIZE);
215         if (priv->reg_base == 0) {
216                 err = -ENOMEM;
217                 goto failure;
218         }
219         init_step = 3;
220
221         priv->read_reg = peak_pci_read_reg;
222         priv->write_reg = peak_pci_write_reg;
223         priv->post_irq = peak_pci_post_irq;
224
225         priv->can.clock.freq = PEAK_PCI_CAN_CLOCK;
226
227         priv->ocr = PEAK_PCI_OCR;
228
229         if (channel == PEAK_PCI_MASTER)
230                 priv->cdr = PEAK_PCI_CDR_MASTER;
231         else
232                 priv->cdr = PEAK_PCI_CDR_SINGLE;
233
234         /* Setup interrupt handling */
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
236         priv->irq_flags = SA_SHIRQ;
237 #else
238         priv->irq_flags = IRQF_SHARED;
239 #endif
240         dev->irq = pdev->irq;
241         icr_high = readw(board->conf_addr + PITA_ICR + 2);
242         if (channel == PEAK_PCI_SLAVE)
243                 icr_high |= 0x0001;
244         else
245                 icr_high |= 0x0002;
246         writew(icr_high, board->conf_addr + PITA_ICR + 2);
247         init_step = 4;
248
249         SET_NETDEV_DEV(dev, &pdev->dev);
250
251         /* Register SJA1000 device */
252         err = register_sja1000dev(dev);
253         if (err) {
254                 printk(KERN_ERR "Registering %s device failed (err=%d)\n",
255                        DRV_NAME, err);
256                 goto failure;
257         }
258
259         if (channel != PEAK_PCI_SLAVE)
260                 *master_dev = dev;
261
262         printk(KERN_INFO "%s: %s at reg_base=0x%p conf_addr=%p irq=%d\n",
263                DRV_NAME, dev->name, priv->reg_base, board->conf_addr, dev->irq);
264
265         return 0;
266
267 failure:
268         peak_pci_del_chan(dev, init_step);
269         return err;
270 }
271
272 static int __devinit peak_pci_init_one(struct pci_dev *pdev,
273                                        const struct pci_device_id *ent)
274 {
275         int err;
276         u16 sub_sys_id;
277         struct net_device *master_dev = NULL;
278
279         printk(KERN_INFO "%s: initializing device %04x:%04x\n",
280                DRV_NAME, pdev->vendor, pdev->device);
281
282         err = pci_enable_device(pdev);
283         if (err)
284                 goto failure;
285
286         err = pci_request_regions(pdev, DRV_NAME);
287         if (err)
288                 goto failure;
289
290         err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
291         if (err)
292                 goto failure_cleanup;
293
294         err = pci_write_config_word(pdev, 0x44, 0);
295         if (err)
296                 goto failure_cleanup;
297
298         if (sub_sys_id > 3) {
299                 err = peak_pci_add_chan(pdev,
300                                         PEAK_PCI_MASTER, &master_dev);
301                 if (err)
302                         goto failure_cleanup;
303
304                 err = peak_pci_add_chan(pdev,
305                                         PEAK_PCI_SLAVE, &master_dev);
306                 if (err)
307                         goto failure_cleanup;
308         } else {
309                 err = peak_pci_add_chan(pdev, PEAK_PCI_SINGLE,
310                                              &master_dev);
311                 if (err)
312                         goto failure_cleanup;
313         }
314
315         pci_set_drvdata(pdev, master_dev);
316         return 0;
317
318 failure_cleanup:
319         if (master_dev)
320                 peak_pci_del_chan(master_dev, 0);
321
322         pci_release_regions(pdev);
323
324 failure:
325         return err;
326
327 }
328
329 static void __devexit peak_pci_remove_one(struct pci_dev *pdev)
330 {
331         struct net_device *dev = pci_get_drvdata(pdev);
332         struct sja1000_priv *priv = netdev_priv(dev);
333         struct peak_pci *board = priv->priv;
334
335         if (board->slave_dev)
336                 peak_pci_del_chan(board->slave_dev, 0);
337         peak_pci_del_chan(dev, 0);
338
339         pci_release_regions(pdev);
340         pci_disable_device(pdev);
341         pci_set_drvdata(pdev, NULL);
342 }
343
344 static struct pci_driver peak_pci_driver = {
345         .name = DRV_NAME,
346         .id_table = peak_pci_tbl,
347         .probe = peak_pci_init_one,
348         .remove = __devexit_p(peak_pci_remove_one),
349 };
350
351 static int __init peak_pci_init(void)
352 {
353         return pci_register_driver(&peak_pci_driver);
354 }
355
356 static void __exit peak_pci_exit(void)
357 {
358         pci_unregister_driver(&peak_pci_driver);
359 }
360
361 module_init(peak_pci_init);
362 module_exit(peak_pci_exit);