]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/net/can/sja1000/ixxat_pci.c
Added socketcan from SVN r903
[lisovros/linux_canprio.git] / drivers / net / can / sja1000 / ixxat_pci.c
1 /*
2  * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
3  * Copyright (C) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the version 2 of the GNU General Public License
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/delay.h>
24 #include <linux/pci.h>
25 #include <linux/can.h>
26 #include <linux/can/dev.h>
27 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
28 #include <linux/io.h>
29 #else
30 #include <asm/io.h>
31 #endif
32
33 #include "sja1000.h"
34
35 #define DRV_NAME  "ixxat_pci"
36
37 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de");
38 MODULE_DESCRIPTION("Socket-CAN driver for IXXAT PC-I 04/PCI PCI cards");
39 MODULE_SUPPORTED_DEVICE("IXXAT PC-I 04/PCI card");
40 MODULE_LICENSE("GPL v2");
41
42 /* Maximum number of interfaces supported on one card. Currently
43  * we only support a maximum of two interfaces, which is the maximum
44  * of what Ixxat sells anyway.
45  */
46 #define IXXAT_PCI_MAX_CAN 2
47
48 struct ixxat_pci {
49         struct pci_dev *pci_dev;
50         struct net_device *dev[IXXAT_PCI_MAX_CAN];
51         int conf_addr;
52         void __iomem *base_addr;
53 };
54
55 #define IXXAT_PCI_CAN_CLOCK  (16000000 / 2)
56
57 #define IXXAT_PCI_OCR        (OCR_TX0_PUSHPULL | OCR_TX0_INVERT | \
58                               OCR_TX1_PUSHPULL)
59 #define IXXAT_PCI_CDR        0
60
61 #define CHANNEL_RESET_OFFSET 0x110
62 #define CHANNEL_OFFSET      0x200
63
64 #define INTCSR_OFFSET        0x4c /* Offset in PLX9050 conf registers */
65 #define INTCSR_LINTI1        (1 << 0)
66 #define INTCSR_LINTI2        (1 << 3)
67 #define INTCSR_PCI           (1 << 6)
68
69 /* PCI vender, device and sub-device ID */
70 #define IXXAT_PCI_VENDOR_ID  0x10b5
71 #define IXXAT_PCI_DEVICE_ID  0x9050
72 #define IXXAT_PCI_SUB_SYS_ID 0x2540
73
74 #define IXXAT_PCI_BASE_SIZE  0x400
75
76 static struct pci_device_id ixxat_pci_tbl[] = {
77         {IXXAT_PCI_VENDOR_ID, IXXAT_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
78         {0,}
79 };
80
81 MODULE_DEVICE_TABLE(pci, ixxat_pci_tbl);
82
83 static u8 ixxat_pci_read_reg(struct net_device *ndev, int port)
84 {
85         u8 val;
86         val = readb((void __iomem *)(ndev->base_addr + port));
87         return val;
88 }
89
90 static void ixxat_pci_write_reg(struct net_device *ndev, int port, u8 val)
91 {
92         writeb(val, (void __iomem *)(ndev->base_addr + port));
93 }
94
95 static void ixxat_pci_del_chan(struct pci_dev *pdev, struct net_device *ndev)
96 {
97         dev_info(&pdev->dev, "Removing device %s\n", ndev->name);
98
99         unregister_sja1000dev(ndev);
100
101         free_sja1000dev(ndev);
102 }
103
104 static struct net_device *ixxat_pci_add_chan(struct pci_dev *pdev,
105                 void __iomem *base_addr)
106 {
107         struct net_device *ndev;
108         struct sja1000_priv *priv;
109         int err;
110
111         ndev = alloc_sja1000dev(0);
112         if (ndev == NULL)
113                 return ERR_PTR(-ENOMEM);
114
115         priv = netdev_priv(ndev);
116
117         ndev->base_addr = (unsigned long)base_addr;
118
119         priv->read_reg = ixxat_pci_read_reg;
120         priv->write_reg = ixxat_pci_write_reg;
121
122         priv->can.bittiming.clock = IXXAT_PCI_CAN_CLOCK;
123
124         priv->ocr = IXXAT_PCI_OCR;
125         priv->cdr = IXXAT_PCI_CDR;
126
127         /* Set and enable PCI interrupts */
128         ndev->irq = pdev->irq;
129
130         dev_dbg(&pdev->dev, "base_addr=%#lx irq=%d\n",
131                         ndev->base_addr, ndev->irq);
132
133         SET_NETDEV_DEV(ndev, &pdev->dev);
134
135         err = register_sja1000dev(ndev);
136         if (err) {
137                 dev_err(&pdev->dev, "Failed to register (err=%d)\n", err);
138                 goto failure;
139         }
140
141         return ndev;
142
143 failure:
144         free_sja1000dev(ndev);
145         return ERR_PTR(err);
146 }
147
148 static int __devinit ixxat_pci_init_one(struct pci_dev *pdev,
149                                        const struct pci_device_id *ent)
150 {
151         struct ixxat_pci *board;
152         int err, intcsr = INTCSR_LINTI1 | INTCSR_PCI;
153         u16 sub_sys_id;
154         void __iomem *base_addr;
155
156         dev_info(&pdev->dev, "Initializing device %04x:%04x\n",
157                pdev->vendor, pdev->device);
158
159         board = kzalloc(sizeof(*board), GFP_KERNEL);
160         if (!board)
161                 return -ENOMEM;
162
163         err = pci_enable_device(pdev);
164         if (err)
165                 goto failure;
166
167         err = pci_request_regions(pdev, DRV_NAME);
168         if (err)
169                 goto failure;
170
171         err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
172         if (err)
173                 goto failure_release_pci;
174
175         if (sub_sys_id != IXXAT_PCI_SUB_SYS_ID)
176                 return -ENODEV;
177
178         /* Enable memory and I/O space */
179         err = pci_write_config_word(pdev, 0x04, 0x3);
180         if (err)
181                 goto failure_release_pci;
182
183         board->conf_addr = pci_resource_start(pdev, 1);
184
185         base_addr = pci_iomap(pdev, 2, IXXAT_PCI_BASE_SIZE);
186         if (base_addr == 0) {
187                 err = -ENODEV;
188                 goto failure_release_pci;
189         }
190
191         board->base_addr = base_addr;
192
193         writeb(0x1, base_addr + CHANNEL_RESET_OFFSET);
194         writeb(0x1, base_addr + CHANNEL_OFFSET + CHANNEL_RESET_OFFSET);
195         udelay(100);
196
197         board->dev[0] = ixxat_pci_add_chan(pdev, base_addr);
198         if (IS_ERR(board->dev[0]))
199                 goto failure_iounmap;
200
201         /* Check if second channel is available */
202         if ((readb(base_addr + CHANNEL_OFFSET + REG_MOD) & 0xa1) == 0x21 &&
203             readb(base_addr + CHANNEL_OFFSET + REG_SR) == 0x0c &&
204             readb(base_addr + CHANNEL_OFFSET + REG_IR) == 0xe0) {
205                 board->dev[1] = ixxat_pci_add_chan(pdev,
206                                 base_addr + CHANNEL_OFFSET);
207                 if (IS_ERR(board->dev[1]))
208                         goto failure_unreg_dev0;
209
210                 intcsr |= INTCSR_LINTI2;
211         }
212
213         /* enable interrupt(s) in PLX9050 */
214         outb(intcsr, board->conf_addr + INTCSR_OFFSET);
215
216         pci_set_drvdata(pdev, board);
217
218         return 0;
219
220 failure_unreg_dev0:
221         ixxat_pci_del_chan(pdev, board->dev[0]);
222
223 failure_iounmap:
224         pci_iounmap(pdev, board->base_addr);
225
226 failure_release_pci:
227         pci_release_regions(pdev);
228
229 failure:
230         kfree(board);
231
232         return err;
233 }
234
235 static void __devexit ixxat_pci_remove_one(struct pci_dev *pdev)
236 {
237         struct ixxat_pci *board = pci_get_drvdata(pdev);
238         int i;
239
240         /* Disable interrupts in PLX9050*/
241         outb(0, board->conf_addr + INTCSR_OFFSET);
242
243         for (i = 0; i < IXXAT_PCI_MAX_CAN; i++) {
244                 if (!board->dev[i])
245                         break;
246                 ixxat_pci_del_chan(pdev, board->dev[i]);
247         }
248
249         pci_iounmap(pdev, board->base_addr);
250
251         pci_release_regions(pdev);
252         pci_disable_device(pdev);
253         pci_set_drvdata(pdev, NULL);
254
255         kfree(board);
256 }
257
258 static struct pci_driver ixxat_pci_driver = {
259         .name = DRV_NAME,
260         .id_table = ixxat_pci_tbl,
261         .probe = ixxat_pci_init_one,
262         .remove = __devexit_p(ixxat_pci_remove_one),
263 };
264
265 static int __init ixxat_pci_init(void)
266 {
267         return pci_register_driver(&ixxat_pci_driver);
268 }
269
270 static void __exit ixxat_pci_exit(void)
271 {
272         pci_unregister_driver(&ixxat_pci_driver);
273 }
274
275 module_init(ixxat_pci_init);
276 module_exit(ixxat_pci_exit);