2 * Copyright (C) 2007 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>
29 #include <socketcan/can.h>
30 #include <socketcan/can/dev.h>
31 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
39 #define DRV_NAME "peak_pci"
41 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
42 #error This driver does not support Kernel versions < 2.6.23
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");
52 struct pci_dev *pci_dev;
53 struct net_device *slave_dev;
54 volatile void __iomem *conf_addr;
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 */
61 #define PEAK_PCI_CAN_CLOCK (16000000 / 2)
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)
66 #define PEAK_PCI_OCR OCR_TX0_PUSHPULL
69 * Important PITA registers
71 #define PITA_ICR 0x00 /* interrupt control register */
72 #define PITA_GPIOICR 0x18 /* general purpose I/O interface
74 #define PITA_MISC 0x1C /* miscellanoes register */
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 */
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 */
83 /* TODO: Add LED Status support for PCAN ExpressCard */
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,},
91 MODULE_DEVICE_TABLE(pci, peak_pci_tbl);
93 static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port)
95 return readb(priv->reg_base + (port << 2));
98 static void peak_pci_write_reg(const struct sja1000_priv *priv,
101 writeb(val, priv->reg_base + (port << 2));
104 static void peak_pci_post_irq(const struct sja1000_priv *priv)
106 struct peak_pci *board = priv->priv;
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);
115 if (icr_low & 0x0002)
116 writew(0x0002, board->conf_addr + PITA_ICR);
120 static void peak_pci_del_chan(struct net_device *dev, int init_step)
122 struct sja1000_priv *priv = netdev_priv(dev);
123 struct peak_pci *board;
128 priv = netdev_priv(dev);
136 case 0: /* Full cleanup */
137 printk(KERN_INFO "Removing %s device %s\n",
138 DRV_NAME, dev->name);
139 unregister_sja1000dev(dev);
141 icr_high = readw(board->conf_addr + PITA_ICR + 2);
142 if (board->channel == PEAK_PCI_SLAVE)
146 writew(icr_high, board->conf_addr + PITA_ICR + 2);
148 iounmap(priv->reg_base);
150 if (board->channel != PEAK_PCI_SLAVE)
151 iounmap((void *)board->conf_addr);
153 free_sja1000dev(dev);
159 static int peak_pci_add_chan(struct pci_dev *pdev, int channel,
160 struct net_device **master_dev)
162 struct net_device *dev;
163 struct sja1000_priv *priv;
164 struct peak_pci *board;
169 dev = alloc_sja1000dev(sizeof(struct peak_pci));
174 priv = netdev_priv(dev);
177 board->pci_dev = pdev;
178 board->channel = channel;
180 if (channel != PEAK_PCI_SLAVE) {
182 addr = pci_resource_start(pdev, 0);
183 board->conf_addr = ioremap(addr, PCI_CONFIG_PORT_SIZE);
184 if (board->conf_addr == 0) {
190 /* Set GPIO control register */
191 writew(0x0005, board->conf_addr + PITA_GPIOICR + 2);
193 /* Enable single or dual channel */
194 if (channel == PEAK_PCI_MASTER)
195 writeb(0x00, board->conf_addr + PITA_GPIOICR);
197 writeb(0x04, board->conf_addr + PITA_GPIOICR);
199 writeb(0x05, board->conf_addr + PITA_MISC + 3);
201 /* Leave parport mux mode */
202 writeb(0x04, board->conf_addr + PITA_MISC + 3);
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;
210 addr = pci_resource_start(pdev, 1);
211 if (channel == PEAK_PCI_SLAVE)
212 addr += PCI_PORT_SIZE;
214 priv->reg_base = ioremap(addr, PCI_PORT_SIZE);
215 if (priv->reg_base == 0) {
221 priv->read_reg = peak_pci_read_reg;
222 priv->write_reg = peak_pci_write_reg;
223 priv->post_irq = peak_pci_post_irq;
225 priv->can.clock.freq = PEAK_PCI_CAN_CLOCK;
227 priv->ocr = PEAK_PCI_OCR;
229 if (channel == PEAK_PCI_MASTER)
230 priv->cdr = PEAK_PCI_CDR_MASTER;
232 priv->cdr = PEAK_PCI_CDR_SINGLE;
234 /* Setup interrupt handling */
235 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
236 priv->irq_flags = SA_SHIRQ;
238 priv->irq_flags = IRQF_SHARED;
240 dev->irq = pdev->irq;
241 icr_high = readw(board->conf_addr + PITA_ICR + 2);
242 if (channel == PEAK_PCI_SLAVE)
246 writew(icr_high, board->conf_addr + PITA_ICR + 2);
249 SET_NETDEV_DEV(dev, &pdev->dev);
251 /* Register SJA1000 device */
252 err = register_sja1000dev(dev);
254 printk(KERN_ERR "Registering %s device failed (err=%d)\n",
259 if (channel != PEAK_PCI_SLAVE)
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);
268 peak_pci_del_chan(dev, init_step);
272 static int __devinit peak_pci_init_one(struct pci_dev *pdev,
273 const struct pci_device_id *ent)
277 struct net_device *master_dev = NULL;
279 printk(KERN_INFO "%s: initializing device %04x:%04x\n",
280 DRV_NAME, pdev->vendor, pdev->device);
282 err = pci_enable_device(pdev);
286 err = pci_request_regions(pdev, DRV_NAME);
290 err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
292 goto failure_cleanup;
294 err = pci_write_config_word(pdev, 0x44, 0);
296 goto failure_cleanup;
298 if (sub_sys_id > 3) {
299 err = peak_pci_add_chan(pdev,
300 PEAK_PCI_MASTER, &master_dev);
302 goto failure_cleanup;
304 err = peak_pci_add_chan(pdev,
305 PEAK_PCI_SLAVE, &master_dev);
307 goto failure_cleanup;
309 err = peak_pci_add_chan(pdev, PEAK_PCI_SINGLE,
312 goto failure_cleanup;
315 pci_set_drvdata(pdev, master_dev);
320 peak_pci_del_chan(master_dev, 0);
322 pci_release_regions(pdev);
329 static void __devexit peak_pci_remove_one(struct pci_dev *pdev)
331 struct net_device *dev = pci_get_drvdata(pdev);
332 struct sja1000_priv *priv = netdev_priv(dev);
333 struct peak_pci *board = priv->priv;
335 if (board->slave_dev)
336 peak_pci_del_chan(board->slave_dev, 0);
337 peak_pci_del_chan(dev, 0);
339 pci_release_regions(pdev);
340 pci_disable_device(pdev);
341 pci_set_drvdata(pdev, NULL);
344 static struct pci_driver peak_pci_driver = {
346 .id_table = peak_pci_tbl,
347 .probe = peak_pci_init_one,
348 .remove = __devexit_p(peak_pci_remove_one),
351 static int __init peak_pci_init(void)
353 return pci_register_driver(&peak_pci_driver);
356 static void __exit peak_pci_exit(void)
358 pci_unregister_driver(&peak_pci_driver);
361 module_init(peak_pci_init);
362 module_exit(peak_pci_exit);