4 * trajet-gw2.c - Philips SJA1000 network device driver for TRAJET.GW2
6 * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
7 * 38106 Braunschweig, GERMANY
9 * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, the following disclaimer and
17 * the referenced file 'COPYING'.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of Volkswagen nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * Alternatively, provided that this notice is retained in full, this
26 * software may be distributed under the terms of the GNU General
27 * Public License ("GPL") version 2 as distributed in the 'COPYING'
28 * file from the main directory of the linux kernel source.
30 * The provided data structures and external interfaces from this code
31 * are not restricted to be used by modules with a GPL compatible license.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
46 * Send feedback to <socketcan-users@lists.berlios.de>
50 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/kernel.h>
53 #include <linux/sched.h>
54 #include <linux/types.h>
55 #include <linux/fcntl.h>
56 #include <linux/interrupt.h>
57 #include <linux/ptrace.h>
58 #include <linux/ioport.h>
60 #include <linux/slab.h>
61 #include <linux/string.h>
62 #include <linux/errno.h>
63 #include <linux/netdevice.h>
64 #include <linux/skbuff.h>
67 #include <linux/can.h>
68 #include <linux/can/ioctl.h> /* for struct can_device_stats */
72 #define CAN_DEV_NAME "can%d"
73 #define DRV_NAME "sja1000-gw2"
75 #define DEFAULT_KBIT_PER_SEC 500
76 #define SJA1000_HW_CLOCK 20000000
78 #define RSIZE (SJA1000_IO_SIZE_PELICAN * (ADDR_GAP + 1))
80 /* driver and version information */
81 static const char *drv_name = DRV_NAME;
82 static const char *drv_version = "0.0.12";
83 static const char *drv_reldate = "2005-08-22";
84 static const char *chip_name = SJA1000_CHIP_NAME;
86 MODULE_AUTHOR("Matthias Brukner <M.Brukner@trajet.de>");
87 MODULE_LICENSE("Dual BSD/GPL");
88 MODULE_DESCRIPTION("LLCF SJA1000 network device driver '" DRV_NAME "'");
90 /* module parameters */
91 static uint32_t base_addr[MAX_CAN] = {
92 (uint32_t)0xf0100200L,
93 (uint32_t)0xf0100300L,
94 (uint32_t)0xf0100400L,
95 (uint32_t)0xf0100500L,
98 static int irq[MAX_CAN] = { 26, 26, 26, 26, 0 };
99 static int speed[MAX_CAN] = {
100 DEFAULT_KBIT_PER_SEC, DEFAULT_KBIT_PER_SEC,
101 DEFAULT_KBIT_PER_SEC, DEFAULT_KBIT_PER_SEC,
104 static int btr[MAX_CAN] = { 0 };
105 static int rx_probe[MAX_CAN] = { 0 };
107 static int clk = SJA1000_HW_CLOCK;
108 static int debug = 0;
109 static int restart_ms = 100;
111 /* array of all can chips */
112 static struct net_device *can_dev[MAX_CAN];
114 static int base_addr_n;
118 static int rx_probe_n;
120 module_param_array(base_addr, int, &base_addr_n, 0);
121 module_param_array(irq, int, &irq_n, 0);
122 module_param_array(speed, int, &speed_n, 0);
123 module_param_array(btr, int, &btr_n, 0);
124 module_param_array(rx_probe, int, &rx_probe_n, 0);
126 module_param(clk, int, 0);
127 module_param(debug, int, 0);
128 module_param(restart_ms, int, 0);
130 /* special functions to access the chips registers */
131 static uint8_t reg_read(struct net_device *dev, int reg)
134 void __iomem *addr = (void __iomem *)dev->base_addr + reg * (ADDR_GAP + 1) + ADDR_GAP;
136 val = (uint8_t)readw(addr);
142 static void reg_write(struct net_device *dev, int reg, uint8_t val)
144 void __iomem *addr = (void __iomem *)dev->base_addr + reg * (ADDR_GAP + 1) + ADDR_GAP;
150 static struct net_device* sja1000_gw2_probe(uint32_t base, int irq, int speed,
151 int btr, int rx_probe, int clk,
152 int debug, int restart_ms)
154 struct net_device *dev;
155 struct can_priv *priv;
157 if (!(dev = alloc_netdev(sizeof(struct can_priv), CAN_DEV_NAME,
159 printk(KERN_ERR "%s: out of memory\n", chip_name);
163 printk(KERN_INFO "%s: base 0x%X / irq %d / speed %d / btr 0x%X / rx_probe %d\n",
164 chip_name, base, irq, speed, btr, rx_probe);
166 /* fill net_device structure */
168 priv = netdev_priv(dev);
171 dev->base_addr = base;
173 priv->reg_read = reg_read;
174 priv->reg_write = reg_write;
178 priv->rx_probe = rx_probe;
180 priv->restart_ms = restart_ms;
183 if (REG_READ(0) == 0xFF) {
184 printk(KERN_INFO "%s: probing failed\n", chip_name);
188 if (register_netdev(dev)) {
189 printk(KERN_INFO "%s: register netdev failed\n", chip_name);
193 /* set chip into reset mode */
196 /* go into Pelican mode, disable clkout, disable comparator */
197 REG_WRITE(REG_CDR, 0xCF);
200 /* connected to external transceiver */
201 REG_WRITE(REG_OCR, 0x1A);
203 printk(KERN_INFO "%s: %s found at 0x%X, irq is %d\n",
204 dev->name, chip_name, (uint32_t)dev->base_addr, dev->irq);
213 static __exit void sja1000_gw2_cleanup_module(void)
217 for (i = 0; i < MAX_CAN; i++) {
218 if (can_dev[i] != NULL) {
219 struct can_priv *priv = netdev_priv(can_dev[i]);
220 unregister_netdev(can_dev[i]);
221 del_timer(&priv->timer);
222 iounmap((void __iomem *)can_dev[i]->base_addr);
223 release_mem_region(base_addr[i], RSIZE);
224 free_netdev(can_dev[i]);
227 sja1000_proc_delete(drv_name);
230 static __init int sja1000_gw2_init_module(void)
234 if (clk < 1000 ) /* MHz command line value */
237 if (clk < 1000000 ) /* kHz command line value */
240 printk(KERN_INFO "%s - %s driver v%s (%s)\n",
241 chip_name, drv_name, drv_version, drv_reldate);
242 printk(KERN_INFO "%s - options [clk %d.%06d MHz] [restart_ms %dms] [debug %d]\n",
243 chip_name, clk/1000000, clk%1000000, restart_ms, debug);
245 for (i = 0; base_addr[i]; i++) {
247 struct net_device *dev = NULL;
250 printk(KERN_DEBUG "%s: checking for %s on address 0x%X ...\n",
251 chip_name, chip_name, base_addr[i]);
252 if (!request_mem_region(base_addr[i], RSIZE, chip_name)) {
253 printk(KERN_ERR "%s: memory already in use\n", chip_name);
254 sja1000_gw2_cleanup_module();
258 base = ioremap(base_addr[i], RSIZE);
260 dev = sja1000_gw2_probe((uint32_t)base, irq[i], speed[i], btr[i], rx_probe[i], clk, debug, restart_ms);
264 sja1000_proc_init(drv_name, can_dev, MAX_CAN);
268 release_mem_region(base_addr[i], RSIZE);
274 module_init(sja1000_gw2_init_module);
275 module_exit(sja1000_gw2_cleanup_module);