2 * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
4 * Derived from the PCAN project file driver/src/pcan_pci.c:
6 * Copyright (C) 2001-2006 PEAK System-Technik GmbH
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
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.
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.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/netdevice.h>
26 #include <linux/delay.h>
27 #include <linux/pci.h>
29 #include <linux/can.h>
30 #include <linux/can/dev.h>
34 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
35 MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI/PCIe cards");
36 MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe CAN card");
37 MODULE_LICENSE("GPL v2");
39 #define DRV_NAME "peak_pci"
41 struct peak_pci_chan
{
42 void __iomem
*cfg_base
; /* Common for all channels */
43 struct net_device
*prev_dev
; /* Chain of network devices */
44 u16 icr_mask
; /* Interrupt mask for fast ack */
47 #define PEAK_PCI_CAN_CLOCK (16000000 / 2)
49 #define PEAK_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
50 #define PEAK_PCI_OCR OCR_TX0_PUSHPULL
53 * Important PITA registers
55 #define PITA_ICR 0x00 /* Interrupt control register */
56 #define PITA_GPIOICR 0x18 /* GPIO interface control register */
57 #define PITA_MISC 0x1C /* Miscellaneous register */
59 #define PEAK_PCI_CFG_SIZE 0x1000 /* Size of the config PCI bar */
60 #define PEAK_PCI_CHAN_SIZE 0x0400 /* Size used by the channel */
62 #define PEAK_PCI_VENDOR_ID 0x001C /* The PCI device and vendor IDs */
63 #define PEAK_PCI_DEVICE_ID 0x0001 /* for PCI/PCIe slot cards */
65 static const u16 peak_pci_icr_masks
[] = {0x02, 0x01, 0x40, 0x80};
67 static DEFINE_PCI_DEVICE_TABLE(peak_pci_tbl
) = {
68 {PEAK_PCI_VENDOR_ID
, PEAK_PCI_DEVICE_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
72 MODULE_DEVICE_TABLE(pci
, peak_pci_tbl
);
74 static u8
peak_pci_read_reg(const struct sja1000_priv
*priv
, int port
)
76 return readb(priv
->reg_base
+ (port
<< 2));
79 static void peak_pci_write_reg(const struct sja1000_priv
*priv
,
82 writeb(val
, priv
->reg_base
+ (port
<< 2));
85 static void peak_pci_post_irq(const struct sja1000_priv
*priv
)
87 struct peak_pci_chan
*chan
= priv
->priv
;
90 /* Select and clear in PITA stored interrupt */
91 icr
= readw(chan
->cfg_base
+ PITA_ICR
);
92 if (icr
& chan
->icr_mask
)
93 writew(chan
->icr_mask
, chan
->cfg_base
+ PITA_ICR
);
96 static int __devinit
peak_pci_probe(struct pci_dev
*pdev
,
97 const struct pci_device_id
*ent
)
99 struct sja1000_priv
*priv
;
100 struct peak_pci_chan
*chan
;
101 struct net_device
*dev
;
102 void __iomem
*cfg_base
, *reg_base
;
104 int i
, err
, channels
;
106 err
= pci_enable_device(pdev
);
110 err
= pci_request_regions(pdev
, DRV_NAME
);
112 goto failure_disable_pci
;
114 err
= pci_read_config_word(pdev
, 0x2e, &sub_sys_id
);
116 goto failure_release_regions
;
118 dev_dbg(&pdev
->dev
, "probing device %04x:%04x:%04x\n",
119 pdev
->vendor
, pdev
->device
, sub_sys_id
);
121 err
= pci_write_config_word(pdev
, 0x44, 0);
123 goto failure_release_regions
;
125 if (sub_sys_id
>= 12)
127 else if (sub_sys_id
>= 10)
129 else if (sub_sys_id
>= 4)
134 cfg_base
= pci_iomap(pdev
, 0, PEAK_PCI_CFG_SIZE
);
136 dev_err(&pdev
->dev
, "failed to map PCI resource #0\n");
137 goto failure_release_regions
;
140 reg_base
= pci_iomap(pdev
, 1, PEAK_PCI_CHAN_SIZE
* channels
);
142 dev_err(&pdev
->dev
, "failed to map PCI resource #1\n");
143 goto failure_unmap_cfg_base
;
146 /* Set GPIO control register */
147 writew(0x0005, cfg_base
+ PITA_GPIOICR
+ 2);
148 /* Enable all channels of this card */
149 writeb(0x00, cfg_base
+ PITA_GPIOICR
);
151 writeb(0x05, cfg_base
+ PITA_MISC
+ 3);
153 /* Leave parport mux mode */
154 writeb(0x04, cfg_base
+ PITA_MISC
+ 3);
156 icr
= readw(cfg_base
+ PITA_ICR
+ 2);
158 for (i
= 0; i
< channels
; i
++) {
159 dev
= alloc_sja1000dev(sizeof(struct peak_pci_chan
));
162 goto failure_remove_channels
;
165 priv
= netdev_priv(dev
);
168 chan
->cfg_base
= cfg_base
;
169 priv
->reg_base
= reg_base
+ i
* PEAK_PCI_CHAN_SIZE
;
171 priv
->read_reg
= peak_pci_read_reg
;
172 priv
->write_reg
= peak_pci_write_reg
;
173 priv
->post_irq
= peak_pci_post_irq
;
175 priv
->can
.clock
.freq
= PEAK_PCI_CAN_CLOCK
;
176 priv
->ocr
= PEAK_PCI_OCR
;
177 priv
->cdr
= PEAK_PCI_CDR
;
178 /* Neither a slave nor a single device distributes the clock */
179 if (channels
== 1 || i
> 0)
180 priv
->cdr
|= CDR_CLK_OFF
;
182 /* Setup interrupt handling */
183 priv
->irq_flags
= IRQF_SHARED
;
184 dev
->irq
= pdev
->irq
;
186 chan
->icr_mask
= peak_pci_icr_masks
[i
];
187 icr
|= chan
->icr_mask
;
189 SET_NETDEV_DEV(dev
, &pdev
->dev
);
191 err
= register_sja1000dev(dev
);
193 dev_err(&pdev
->dev
, "failed to register device\n");
194 free_sja1000dev(dev
);
195 goto failure_remove_channels
;
198 /* Create chain of SJA1000 devices */
199 chan
->prev_dev
= pci_get_drvdata(pdev
);
200 pci_set_drvdata(pdev
, dev
);
203 "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n",
204 dev
->name
, priv
->reg_base
, chan
->cfg_base
, dev
->irq
);
207 /* Enable interrupts */
208 writew(icr
, cfg_base
+ PITA_ICR
+ 2);
212 failure_remove_channels
:
213 /* Disable interrupts */
214 writew(0x0, cfg_base
+ PITA_ICR
+ 2);
216 for (dev
= pci_get_drvdata(pdev
); dev
; dev
= chan
->prev_dev
) {
217 unregister_sja1000dev(dev
);
218 free_sja1000dev(dev
);
219 priv
= netdev_priv(dev
);
223 pci_iounmap(pdev
, reg_base
);
225 failure_unmap_cfg_base
:
226 pci_iounmap(pdev
, cfg_base
);
228 failure_release_regions
:
229 pci_release_regions(pdev
);
232 pci_disable_device(pdev
);
237 static void __devexit
peak_pci_remove(struct pci_dev
*pdev
)
239 struct net_device
*dev
= pci_get_drvdata(pdev
); /* Last device */
240 struct sja1000_priv
*priv
= netdev_priv(dev
);
241 struct peak_pci_chan
*chan
= priv
->priv
;
242 void __iomem
*cfg_base
= chan
->cfg_base
;
243 void __iomem
*reg_base
= priv
->reg_base
;
245 /* Disable interrupts */
246 writew(0x0, cfg_base
+ PITA_ICR
+ 2);
248 /* Loop over all registered devices */
250 dev_info(&pdev
->dev
, "removing device %s\n", dev
->name
);
251 unregister_sja1000dev(dev
);
252 free_sja1000dev(dev
);
253 dev
= chan
->prev_dev
;
256 priv
= netdev_priv(dev
);
260 pci_iounmap(pdev
, reg_base
);
261 pci_iounmap(pdev
, cfg_base
);
262 pci_release_regions(pdev
);
263 pci_disable_device(pdev
);
265 pci_set_drvdata(pdev
, NULL
);
268 static struct pci_driver peak_pci_driver
= {
270 .id_table
= peak_pci_tbl
,
271 .probe
= peak_pci_probe
,
272 .remove
= __devexit_p(peak_pci_remove
),
275 static int __init
peak_pci_init(void)
277 return pci_register_driver(&peak_pci_driver
);
279 module_init(peak_pci_init
);
281 static void __exit
peak_pci_exit(void)
283 pci_unregister_driver(&peak_pci_driver
);
285 module_exit(peak_pci_exit
);