]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/sja1000/peak_pci.c
9decb90ba86881e41e437ae6af4a73e9b041b46c
[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 cards");
47 MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI 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
81
82 static struct pci_device_id peak_pci_tbl[] = {
83         {PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
84         {0,}
85 };
86
87 MODULE_DEVICE_TABLE(pci, peak_pci_tbl);
88
89 static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port)
90 {
91         return readb(priv->reg_base + (port << 2));
92 }
93
94 static void peak_pci_write_reg(const struct sja1000_priv *priv,
95                                int port, u8 val)
96 {
97         writeb(val, priv->reg_base + (port << 2));
98 }
99
100 static void peak_pci_post_irq(const struct sja1000_priv *priv)
101 {
102         struct peak_pci *board = priv->priv;
103         u16 icr_low;
104
105         /* Select and clear in Pita stored interrupt */
106         icr_low = readw(board->conf_addr + PITA_ICR);
107         if (board->channel == PEAK_PCI_SLAVE) {
108                 if (icr_low & 0x0001)
109                         writew(0x0001, board->conf_addr + PITA_ICR);
110         } else {
111                 if (icr_low & 0x0002)
112                         writew(0x0002, board->conf_addr + PITA_ICR);
113         }
114 }
115
116 static void peak_pci_del_chan(struct net_device *dev, int init_step)
117 {
118         struct sja1000_priv *priv = netdev_priv(dev);
119         struct peak_pci *board;
120         u16 icr_high;
121
122         if (!dev)
123                 return;
124         priv = netdev_priv(dev);
125         if (!priv)
126                 return;
127         board = priv->priv;
128         if (!board)
129                 return;
130
131         switch (init_step) {
132         case 0:         /* Full cleanup */
133                 printk(KERN_INFO "Removing %s device %s\n",
134                        DRV_NAME, dev->name);
135                 unregister_sja1000dev(dev);
136         case 4:
137                 icr_high = readw(board->conf_addr + PITA_ICR + 2);
138                 if (board->channel == PEAK_PCI_SLAVE)
139                         icr_high &= ~0x0001;
140                 else
141                         icr_high &= ~0x0002;
142                 writew(icr_high, board->conf_addr + PITA_ICR + 2);
143         case 3:
144                 iounmap(priv->reg_base);
145         case 2:
146                 if (board->channel != PEAK_PCI_SLAVE)
147                         iounmap((void *)board->conf_addr);
148         case 1:
149                 free_sja1000dev(dev);
150                 break;
151         }
152
153 }
154
155 static int peak_pci_add_chan(struct pci_dev *pdev, int channel,
156                              struct net_device **master_dev)
157 {
158         struct net_device *dev;
159         struct sja1000_priv *priv;
160         struct peak_pci *board;
161         u16 icr_high;
162         unsigned long addr;
163         int err, init_step;
164
165         dev = alloc_sja1000dev(sizeof(struct peak_pci));
166         if (dev == NULL)
167                 return -ENOMEM;
168         init_step = 1;
169
170         priv = netdev_priv(dev);
171         board = priv->priv;
172
173         board->pci_dev = pdev;
174         board->channel = channel;
175
176         if (channel != PEAK_PCI_SLAVE) {
177
178                 addr = pci_resource_start(pdev, 0);
179                 board->conf_addr = ioremap(addr, PCI_CONFIG_PORT_SIZE);
180                 if (board->conf_addr == 0) {
181                         err = -ENODEV;
182                         goto failure;
183                 }
184                 init_step = 2;
185
186                 /* Set GPIO control register */
187                 writew(0x0005, board->conf_addr + PITA_GPIOICR + 2);
188
189                 /* Enable single or dual channel */
190                 if (channel == PEAK_PCI_MASTER)
191                         writeb(0x00, board->conf_addr + PITA_GPIOICR);
192                 else
193                         writeb(0x04, board->conf_addr + PITA_GPIOICR);
194                 /* Toggle reset */
195                 writeb(0x05, board->conf_addr + PITA_MISC + 3);
196                 mdelay(5);
197                 /* Leave parport mux mode */
198                 writeb(0x04, board->conf_addr + PITA_MISC + 3);
199         } else {
200                 struct sja1000_priv *master_priv = netdev_priv(*master_dev);
201                 struct peak_pci *master_board = master_priv->priv;
202                 master_board->slave_dev = dev;
203                 board->conf_addr = master_board->conf_addr;
204         }
205
206         addr = pci_resource_start(pdev, 1);
207         if (channel == PEAK_PCI_SLAVE)
208                 addr += PCI_PORT_SIZE;
209
210         priv->reg_base = ioremap(addr, PCI_PORT_SIZE);
211         if (priv->reg_base == 0) {
212                 err = -ENOMEM;
213                 goto failure;
214         }
215         init_step = 3;
216
217         priv->read_reg = peak_pci_read_reg;
218         priv->write_reg = peak_pci_write_reg;
219         priv->post_irq = peak_pci_post_irq;
220
221         priv->can.clock.freq = PEAK_PCI_CAN_CLOCK;
222
223         priv->ocr = PEAK_PCI_OCR;
224
225         if (channel == PEAK_PCI_MASTER)
226                 priv->cdr = PEAK_PCI_CDR_MASTER;
227         else
228                 priv->cdr = PEAK_PCI_CDR_SINGLE;
229
230         /* Setup interrupt handling */
231 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
232         priv->irq_flags = SA_SHIRQ;
233 #else
234         priv->irq_flags = IRQF_SHARED;
235 #endif
236         dev->irq = pdev->irq;
237         icr_high = readw(board->conf_addr + PITA_ICR + 2);
238         if (channel == PEAK_PCI_SLAVE)
239                 icr_high |= 0x0001;
240         else
241                 icr_high |= 0x0002;
242         writew(icr_high, board->conf_addr + PITA_ICR + 2);
243         init_step = 4;
244
245         SET_NETDEV_DEV(dev, &pdev->dev);
246
247         /* Register SJA1000 device */
248         err = register_sja1000dev(dev);
249         if (err) {
250                 printk(KERN_ERR "Registering %s device failed (err=%d)\n",
251                        DRV_NAME, err);
252                 goto failure;
253         }
254
255         if (channel != PEAK_PCI_SLAVE)
256                 *master_dev = dev;
257
258         printk(KERN_INFO "%s: %s at reg_base=0x%p conf_addr=%p irq=%d\n",
259                DRV_NAME, dev->name, priv->reg_base, board->conf_addr, dev->irq);
260
261         return 0;
262
263 failure:
264         peak_pci_del_chan(dev, init_step);
265         return err;
266 }
267
268 static int __devinit peak_pci_init_one(struct pci_dev *pdev,
269                                        const struct pci_device_id *ent)
270 {
271         int err;
272         u16 sub_sys_id;
273         struct net_device *master_dev = NULL;
274
275         printk(KERN_INFO "%s: initializing device %04x:%04x\n",
276                DRV_NAME, pdev->vendor, pdev->device);
277
278         err = pci_enable_device(pdev);
279         if (err)
280                 goto failure;
281
282         err = pci_request_regions(pdev, DRV_NAME);
283         if (err)
284                 goto failure;
285
286         err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
287         if (err)
288                 goto failure_cleanup;
289
290         err = pci_write_config_word(pdev, 0x44, 0);
291         if (err)
292                 goto failure_cleanup;
293
294         if (sub_sys_id > 3) {
295                 err = peak_pci_add_chan(pdev,
296                                         PEAK_PCI_MASTER, &master_dev);
297                 if (err)
298                         goto failure_cleanup;
299
300                 err = peak_pci_add_chan(pdev,
301                                         PEAK_PCI_SLAVE, &master_dev);
302                 if (err)
303                         goto failure_cleanup;
304         } else {
305                 err = peak_pci_add_chan(pdev, PEAK_PCI_SINGLE,
306                                              &master_dev);
307                 if (err)
308                         goto failure_cleanup;
309         }
310
311         pci_set_drvdata(pdev, master_dev);
312         return 0;
313
314 failure_cleanup:
315         if (master_dev)
316                 peak_pci_del_chan(master_dev, 0);
317
318         pci_release_regions(pdev);
319
320 failure:
321         return err;
322
323 }
324
325 static void __devexit peak_pci_remove_one(struct pci_dev *pdev)
326 {
327         struct net_device *dev = pci_get_drvdata(pdev);
328         struct sja1000_priv *priv = netdev_priv(dev);
329         struct peak_pci *board = priv->priv;
330
331         if (board->slave_dev)
332                 peak_pci_del_chan(board->slave_dev, 0);
333         peak_pci_del_chan(dev, 0);
334
335         pci_release_regions(pdev);
336         pci_disable_device(pdev);
337         pci_set_drvdata(pdev, NULL);
338 }
339
340 static struct pci_driver peak_pci_driver = {
341         .name = DRV_NAME,
342         .id_table = peak_pci_tbl,
343         .probe = peak_pci_init_one,
344         .remove = __devexit_p(peak_pci_remove_one),
345 };
346
347 static int __init peak_pci_init(void)
348 {
349         return pci_register_driver(&peak_pci_driver);
350 }
351
352 static void __exit peak_pci_exit(void)
353 {
354         pci_unregister_driver(&peak_pci_driver);
355 }
356
357 module_init(peak_pci_init);
358 module_exit(peak_pci_exit);