4 * mem.c - Philips SJA1000 network device driver for IOMEM
6 * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, the following disclaimer and
14 * the referenced file 'COPYING'.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Volkswagen nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * Alternatively, provided that this notice is retained in full, this
23 * software may be distributed under the terms of the GNU General
24 * Public License ("GPL") version 2 as distributed in the 'COPYING'
25 * file from the main directory of the linux kernel source.
27 * The provided data structures and external interfaces from this code
28 * are not restricted to be used by modules with a GPL compatible license.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43 * Send feedback to <socketcan-users@lists.berlios.de>
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/types.h>
52 #include <linux/fcntl.h>
53 #include <linux/interrupt.h>
54 #include <linux/ptrace.h>
55 #include <linux/ioport.h>
57 #include <linux/slab.h>
58 #include <linux/string.h>
59 #include <linux/errno.h>
60 #include <linux/netdevice.h>
61 #include <linux/skbuff.h>
64 #include <linux/can.h>
65 #include <linux/can/ioctl.h> /* for struct can_device_stats */
69 #define CAN_DEV_NAME "can%d"
70 #define DRV_NAME "sja1000-mem"
72 #define DEFAULT_KBIT_PER_SEC 500
73 #define SJA1000_HW_CLOCK 16000000
75 /* driver and version information */
76 static const char *drv_name = DRV_NAME;
77 static const char *drv_version = "0.0.1";
78 static const char *drv_reldate = "2006-08-22";
79 static const char *chip_name = SJA1000_CHIP_NAME;
81 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>, Pavel Pisa <pisa@cmp.felk.cvut.cz>");
82 MODULE_LICENSE("Dual BSD/GPL");
83 MODULE_DESCRIPTION("LLCF SJA1000 network device driver '" DRV_NAME "'");
85 /* module parameters */
86 static uint32_t base_addr[MAX_CAN] = { (uint32_t)0xda000L, 0};
88 static int irq[MAX_CAN] = { 9, 0 };
90 static int speed[MAX_CAN] = { DEFAULT_KBIT_PER_SEC, DEFAULT_KBIT_PER_SEC, 0};
92 static int btr[MAX_CAN] = { 0 };
93 static int rx_probe[MAX_CAN] = { 0 };
95 static int clk = SJA1000_HW_CLOCK;
97 static int restart_ms = 100;
99 /* array of all can chips */
100 static struct net_device *can_dev[MAX_CAN];
102 static int base_addr_n;
106 static int rx_probe_n;
108 module_param_array(base_addr, int, &base_addr_n, 0);
109 module_param_array(irq, int, &irq_n, 0);
110 module_param_array(speed, int, &speed_n, 0);
111 module_param_array(btr, int, &btr_n, 0);
112 module_param_array(rx_probe, int, &rx_probe_n, 0);
114 module_param(clk, int, 0);
115 module_param(debug, int, 0);
116 module_param(restart_ms, int, 0);
118 /* special functions to access the chips registers */
119 static uint8_t reg_read(struct net_device *dev, int reg)
122 void __iomem *addr = (void __iomem *)dev->base_addr + reg;
124 val = (uint8_t)readw(addr);
130 static void reg_write(struct net_device *dev, int reg, uint8_t val)
132 void __iomem *addr = (void __iomem *)dev->base_addr + reg;
138 static struct net_device* sja1000_mem_probe(uint32_t base, int irq, int speed,
139 int btr, int rx_probe, int clk,
140 int debug, int restart_ms)
142 struct net_device *dev;
143 struct can_priv *priv;
145 if (!(dev = alloc_netdev(sizeof(struct can_priv), CAN_DEV_NAME,
147 printk(KERN_ERR "%s: out of memory\n", chip_name);
151 printk(KERN_INFO "%s: base 0x%X / irq %d / speed %d / btr 0x%X / rx_probe %d\n",
152 chip_name, base, irq, speed, btr, rx_probe);
154 /* fill net_device structure */
156 priv = netdev_priv(dev);
159 dev->base_addr = base;
161 priv->reg_read = reg_read;
162 priv->reg_write = reg_write;
166 priv->rx_probe = rx_probe;
168 priv->restart_ms = restart_ms;
171 if (REG_READ(0) == 0xFF)
174 /* set chip into reset mode */
177 /* go into Pelican mode, disable clkout, disable comparator */
178 REG_WRITE(REG_CDR, 0xCF);
181 /* connected to external transceiver */
182 REG_WRITE(REG_OCR, 0x1A);
184 printk(KERN_INFO "%s: %s found at 0x%X, irq is %d\n",
185 dev->name, chip_name, (uint32_t)dev->base_addr, dev->irq);
187 if (register_netdev(dev) == 0)
190 printk(KERN_INFO "%s: probing failed\n", chip_name);
196 static __exit void sja1000_mem_cleanup_module(void)
200 for (i = 0; i < MAX_CAN; i++) {
201 if (can_dev[i] != NULL) {
202 struct can_priv *priv = netdev_priv(can_dev[i]);
203 unregister_netdev(can_dev[i]);
204 del_timer(&priv->timer);
205 iounmap((void __iomem *)can_dev[i]->base_addr);
206 release_mem_region(base_addr[i], SJA1000_IO_SIZE_BASIC);
207 free_netdev(can_dev[i]);
210 sja1000_proc_delete(drv_name);
213 static __init int sja1000_mem_init_module(void)
217 if (clk < 1000 ) /* MHz command line value */
220 if (clk < 1000000 ) /* kHz command line value */
223 printk(KERN_INFO "%s - %s driver v%s (%s)\n",
224 chip_name, drv_name, drv_version, drv_reldate);
225 printk(KERN_INFO "%s - options [clk %d.%06d MHz] [restart_ms %dms] [debug %d]\n",
226 chip_name, clk/1000000, clk%1000000, restart_ms, debug);
228 for (i = 0; base_addr[i]; i++) {
230 struct net_device *dev = NULL;
233 printk(KERN_DEBUG "%s: checking for %s on address 0x%X ...\n",
234 chip_name, chip_name, base_addr[i]);
235 if (!request_mem_region(base_addr[i], SJA1000_IO_SIZE_BASIC, chip_name)) {
236 printk(KERN_ERR "%s: memory already in use\n", chip_name);
237 sja1000_mem_cleanup_module();
241 base = ioremap(base_addr[i], SJA1000_IO_SIZE_BASIC);
243 dev = sja1000_mem_probe((uint32_t)base, irq[i], speed[i], btr[i], rx_probe[i], clk, debug, restart_ms);
246 sja1000_proc_init(drv_name, can_dev, MAX_CAN);
250 release_mem_region(base_addr[i], SJA1000_IO_SIZE_BASIC);
256 module_init(sja1000_mem_init_module);
257 module_exit(sja1000_mem_cleanup_module);