2 * $Id: trajet-gw2.c,v 2.0 2006/04/13 10:37:22 ethuerm Exp $
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 <llcf@volkswagen.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>
68 #include "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.11";
83 static const char *drv_reldate = "2005-10-11";
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];
115 /* special functions to access the chips registers */
116 static uint8_t reg_read(struct net_device *dev, int reg)
120 val = (uint8_t)readw(dev->base_addr + reg * (ADDR_GAP + 1) + ADDR_GAP);
126 static void reg_write(struct net_device *dev, int reg, uint8_t val)
128 writew(val, dev->base_addr + reg * 2 + 1);
132 MODULE_PARM(base_addr, "1-" __MODULE_STRING(MAX_CAN)"i");
133 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CAN)"i");
134 MODULE_PARM(speed, "1-" __MODULE_STRING(MAX_CAN)"i");
135 MODULE_PARM(btr, "1-" __MODULE_STRING(MAX_CAN)"i");
136 MODULE_PARM(rx_probe, "1-" __MODULE_STRING(MAX_CAN)"i");
137 MODULE_PARM(clk, "i");
138 MODULE_PARM(debug, "i");
139 MODULE_PARM(restart_ms, "i");
141 static struct net_device* sja1000_gw2_probe(uint32_t base, int irq, int speed,
142 int btr, int rx_probe, int clk,
143 int debug, int restart_ms)
145 struct net_device *dev;
146 struct can_priv *priv;
148 if (!(dev = alloc_netdev(sizeof(struct can_priv), CAN_DEV_NAME,
150 printk(KERN_ERR "%s: out of memory\n", chip_name);
154 printk(KERN_INFO "%s: base 0x%X / irq %d / speed %d / btr 0x%X / rx_probe %d\n",
155 chip_name, base, irq, speed, btr, rx_probe);
157 /* fill net_device structure */
159 priv = netdev_priv(dev);
162 dev->base_addr = base;
164 priv->reg_read = reg_read;
165 priv->reg_write = reg_write;
169 priv->rx_probe = rx_probe;
171 priv->restart_ms = restart_ms;
174 if (REG_READ(0) == 0xFF)
177 /* set chip into reset mode */
180 /* go into Pelican mode, disable clkout, disable comparator */
181 REG_WRITE(REG_CDR, 0xCF);
184 /* connected to external transceiver */
185 REG_WRITE(REG_OCR, 0x1A);
187 printk(KERN_INFO "%s: %s found at 0x%X, irq is %d\n",
188 dev->name, chip_name, (uint32_t)dev->base_addr, dev->irq);
190 if (register_netdev(dev) == 0)
193 printk(KERN_INFO "%s: probing failed\n", chip_name);
199 static __exit void sja1000_gw2_cleanup_module(void)
203 for (i = 0; i < MAX_CAN; i++) {
204 if (can_dev[i] != NULL) {
205 struct can_priv *priv = netdev_priv(can_dev[i]);
206 unregister_netdev(can_dev[i]);
207 del_timer(&priv->timer);
208 iounmap((void*)can_dev[i]->base_addr);
209 release_mem_region(base_addr[i], RSIZE);
210 free_netdev(can_dev[i]);
213 sja1000_proc_delete(drv_name);
216 static __init int sja1000_gw2_init_module(void)
219 struct net_device *dev;
222 if (clk < 1000 ) /* MHz command line value */
225 if (clk < 1000000 ) /* kHz command line value */
228 printk(KERN_INFO "%s - %s driver v%s (%s)\n",
229 chip_name, drv_name, drv_version, drv_reldate);
230 printk(KERN_INFO "%s - options [clk %d.%06d MHz] [restart_ms %dms] [debug %d]\n",
231 chip_name, clk/1000000, clk%1000000, restart_ms, debug);
233 for (i = 0; base_addr[i]; i++) {
234 printk(KERN_DEBUG "%s: checking for %s on address 0x%X ...\n",
235 chip_name, chip_name, base_addr[i]);
236 if (!request_mem_region(base_addr[i], RSIZE, chip_name)) {
237 printk(KERN_ERR "%s: memory already in use\n", chip_name);
238 sja1000_gw2_cleanup_module();
241 base = ioremap(base_addr[i], RSIZE);
242 dev = sja1000_gw2_probe((uint32_t)base, irq[i], speed[i], btr[i], rx_probe[i], clk, debug, restart_ms);
245 sja1000_proc_init(drv_name, can_dev, MAX_CAN);
249 release_mem_region(base_addr[i], RSIZE);
255 module_init(sja1000_gw2_init_module);
256 module_exit(sja1000_gw2_cleanup_module);