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/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>
30 #include <linux/can.h>
31 #include <linux/can/dev.h>
35 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
36 MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI/PCIe cards");
37 MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe CAN card");
38 MODULE_LICENSE("GPL v2");
40 #define DRV_NAME "peak_pci"
42 struct peak_pci_chan
{
43 void __iomem
*cfg_base
; /* Common for all channels */
44 struct net_device
*next_dev
; /* Chain of network devices */
45 u16 icr_mask
; /* Interrupt mask for fast ack */
48 #define PEAK_PCI_CAN_CLOCK (16000000 / 2)
50 #define PEAK_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
51 #define PEAK_PCI_OCR OCR_TX0_PUSHPULL
54 * Important PITA registers
56 #define PITA_ICR 0x00 /* Interrupt control register */
57 #define PITA_GPIOICR 0x18 /* GPIO interface control register */
58 #define PITA_MISC 0x1C /* Miscellaneous register */
60 #define PEAK_PCI_CFG_SIZE 0x1000 /* Size of the config PCI bar */
61 #define PEAK_PCI_CHAN_SIZE 0x0400 /* Size used by the channel */
63 #define PEAK_PCI_VENDOR_ID 0x001C /* The PCI device and vendor IDs */
64 #define PEAK_PCI_DEVICE_ID 0x0001 /* for PCI/PCIe slot cards */
66 static const u16 peak_pci_icr_masks
[] = {0x02, 0x01, 0x40, 0x80};
68 static DEFINE_PCI_DEVICE_TABLE(peak_pci_tbl
) = {
69 {PEAK_PCI_VENDOR_ID
, PEAK_PCI_DEVICE_ID
, PCI_ANY_ID
, PCI_ANY_ID
,},
73 MODULE_DEVICE_TABLE(pci
, peak_pci_tbl
);
75 static u8
peak_pci_read_reg(const struct sja1000_priv
*priv
, int port
)
77 return readb(priv
->reg_base
+ (port
<< 2));
80 static void peak_pci_write_reg(const struct sja1000_priv
*priv
,
83 writeb(val
, priv
->reg_base
+ (port
<< 2));
86 static void peak_pci_post_irq(const struct sja1000_priv
*priv
)
88 struct peak_pci_chan
*chan
= priv
->priv
;
91 /* Select and clear in PITA stored interrupt */
92 icr
= readw(chan
->cfg_base
+ PITA_ICR
);
93 if (icr
& chan
->icr_mask
)
94 writew(chan
->icr_mask
, chan
->cfg_base
+ PITA_ICR
);
97 static int __devinit
peak_pci_probe(struct pci_dev
*pdev
,
98 const struct pci_device_id
*ent
)
100 struct sja1000_priv
*priv
;
101 struct peak_pci_chan
*chan
;
102 struct net_device
*dev
, *dev0
= NULL
;
103 void __iomem
*cfg_base
, *reg_base
;
105 int i
, err
, channels
;
107 err
= pci_enable_device(pdev
);
111 err
= pci_request_regions(pdev
, DRV_NAME
);
113 goto failure_disable_pci
;
115 err
= pci_read_config_word(pdev
, 0x2e, &sub_sys_id
);
117 goto failure_release_regions
;
119 dev_dbg(&pdev
->dev
, "probing device %04x:%04x:%04x\n",
120 pdev
->vendor
, pdev
->device
, sub_sys_id
);
122 err
= pci_write_config_word(pdev
, 0x44, 0);
124 goto failure_release_regions
;
126 if (sub_sys_id
>= 12)
128 else if (sub_sys_id
>= 10)
130 else if (sub_sys_id
>= 4)
135 cfg_base
= pci_iomap(pdev
, 0, PEAK_PCI_CFG_SIZE
);
137 dev_err(&pdev
->dev
, "failed to map PCI resource #0\n");
138 goto failure_release_regions
;
141 reg_base
= pci_iomap(pdev
, 1, PEAK_PCI_CHAN_SIZE
* channels
);
143 dev_err(&pdev
->dev
, "failed to map PCI resource #1\n");
144 goto failure_unmap_cfg_base
;
147 /* Set GPIO control register */
148 writew(0x0005, cfg_base
+ PITA_GPIOICR
+ 2);
149 /* Enable all channels of this card */
150 writeb(0x00, cfg_base
+ PITA_GPIOICR
);
152 writeb(0x05, cfg_base
+ PITA_MISC
+ 3);
154 /* Leave parport mux mode */
155 writeb(0x04, cfg_base
+ PITA_MISC
+ 3);
157 icr
= readw(cfg_base
+ PITA_ICR
+ 2);
159 for (i
= 0; i
< channels
; i
++) {
160 dev
= alloc_sja1000dev(sizeof(struct peak_pci_chan
));
163 goto failure_remove_channels
;
166 priv
= netdev_priv(dev
);
169 chan
->cfg_base
= cfg_base
;
170 priv
->reg_base
= reg_base
+ i
* PEAK_PCI_CHAN_SIZE
;
172 priv
->read_reg
= peak_pci_read_reg
;
173 priv
->write_reg
= peak_pci_write_reg
;
174 priv
->post_irq
= peak_pci_post_irq
;
176 priv
->can
.clock
.freq
= PEAK_PCI_CAN_CLOCK
;
177 priv
->ocr
= PEAK_PCI_OCR
;
178 priv
->cdr
= PEAK_PCI_CDR
;
179 /* Neither a slave nor a single device distributes the clock */
180 if (channels
== 1 || i
> 0)
181 priv
->cdr
|= CDR_CLK_OFF
;
183 /* Setup interrupt handling */
184 priv
->irq_flags
= IRQF_SHARED
;
185 dev
->irq
= pdev
->irq
;
187 chan
->icr_mask
= peak_pci_icr_masks
[i
];
188 icr
|= chan
->icr_mask
;
190 SET_NETDEV_DEV(dev
, &pdev
->dev
);
192 err
= register_sja1000dev(dev
);
194 dev_err(&pdev
->dev
, "failed to register device\n");
195 free_sja1000dev(dev
);
196 goto failure_remove_channels
;
199 /* Create chain of SJA1000 devices */
203 chan
->next_dev
= dev
;
206 "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n",
207 dev
->name
, priv
->reg_base
, chan
->cfg_base
, dev
->irq
);
210 pci_set_drvdata(pdev
, dev0
);
212 /* Enable interrupts */
213 writew(icr
, cfg_base
+ PITA_ICR
+ 2);
217 failure_remove_channels
:
218 /* Disable interrupts */
219 writew(0x0, cfg_base
+ PITA_ICR
+ 2);
221 for (dev
= dev0
; dev
; dev
= chan
->next_dev
) {
222 unregister_sja1000dev(dev
);
223 free_sja1000dev(dev
);
224 priv
= netdev_priv(dev
);
226 dev
= chan
->next_dev
;
229 pci_iounmap(pdev
, reg_base
);
231 failure_unmap_cfg_base
:
232 pci_iounmap(pdev
, cfg_base
);
234 failure_release_regions
:
235 pci_release_regions(pdev
);
238 pci_disable_device(pdev
);
243 static void __devexit
peak_pci_remove(struct pci_dev
*pdev
)
245 struct net_device
*dev
= pci_get_drvdata(pdev
); /* First device */
246 struct sja1000_priv
*priv
= netdev_priv(dev
);
247 struct peak_pci_chan
*chan
= priv
->priv
;
248 void __iomem
*cfg_base
= chan
->cfg_base
;
249 void __iomem
*reg_base
= priv
->reg_base
;
251 /* Disable interrupts */
252 writew(0x0, cfg_base
+ PITA_ICR
+ 2);
254 /* Loop over all registered devices */
256 dev_info(&pdev
->dev
, "removing device %s\n", dev
->name
);
257 unregister_sja1000dev(dev
);
258 free_sja1000dev(dev
);
259 dev
= chan
->next_dev
;
262 priv
= netdev_priv(dev
);
266 pci_iounmap(pdev
, reg_base
);
267 pci_iounmap(pdev
, cfg_base
);
268 pci_release_regions(pdev
);
269 pci_disable_device(pdev
);
271 pci_set_drvdata(pdev
, NULL
);
274 static struct pci_driver peak_pci_driver
= {
276 .id_table
= peak_pci_tbl
,
277 .probe
= peak_pci_probe
,
278 .remove
= __devexit_p(peak_pci_remove
),
281 static int __init
peak_pci_init(void)
283 return pci_register_driver(&peak_pci_driver
);
285 module_init(peak_pci_init
);
287 static void __exit
peak_pci_exit(void)
289 pci_unregister_driver(&peak_pci_driver
);
291 module_exit(peak_pci_exit
);