]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/sja1000/trajet-gw2.c
fix version code.
[socketcan-devel.git] / kernel / 2.6 / drivers / sja1000 / trajet-gw2.c
1 /*
2  * $Id: trajet-gw2.c,v 2.0 2006/04/13 10:37:22 ethuerm Exp $
3  *
4  * trajet-gw2.c - Philips SJA1000 network device driver for TRAJET.GW2
5  *
6  * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
7  * 38106 Braunschweig, GERMANY
8  *
9  * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
24  *
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.
29  *
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.
32  *
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
44  * DAMAGE.
45  *
46  * Send feedback to <llcf@volkswagen.de>
47  *
48  */
49
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>
59 #include <linux/in.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>
65 #include <asm/io.h>
66
67 #include "can.h"
68 #include "can_ioctl.h" /* for struct can_device_stats */
69 #include "sja1000.h"
70
71 #define MAX_CAN         8
72 #define CAN_DEV_NAME    "can%d"
73 #define DRV_NAME        "sja1000-gw2"
74
75 #define DEFAULT_KBIT_PER_SEC 500
76 #define SJA1000_HW_CLOCK 20000000
77 #define ADDR_GAP        1
78 #define RSIZE           (SJA1000_IO_SIZE_PELICAN * (ADDR_GAP + 1))
79
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;
85
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 "'");
89
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,
96         0
97 };
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,
102         0
103 };
104 static int btr[MAX_CAN] = { 0 };
105 static int rx_probe[MAX_CAN] = { 0 };
106
107 static int clk = SJA1000_HW_CLOCK;
108 static int debug = 0;
109 static int restart_ms = 100;
110
111 /* array of all can chips */
112 static struct net_device        *can_dev[MAX_CAN];
113
114
115 /* special functions to access the chips registers */
116 static uint8_t reg_read(struct net_device *dev, int reg)
117 {
118         static uint8_t val;
119
120         val = (uint8_t)readw(dev->base_addr + reg * (ADDR_GAP + 1) + ADDR_GAP);
121         rmb();
122
123         return val;
124 }
125
126 static void reg_write(struct net_device *dev, int reg, uint8_t val)
127 {
128         writew(val, dev->base_addr + reg * 2 + 1);
129         wmb();
130 }
131
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");
140
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)
144 {
145         struct net_device       *dev;
146         struct can_priv         *priv;
147
148         if (!(dev = alloc_netdev(sizeof(struct can_priv), CAN_DEV_NAME,
149                                  sja1000_setup))) {
150                 printk(KERN_ERR "%s: out of memory\n", chip_name);
151                 return NULL;
152         }
153
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);
156
157         /* fill net_device structure */
158
159         priv             = netdev_priv(dev);
160
161         dev->irq         = irq;
162         dev->base_addr   = base;
163
164         priv->reg_read   = reg_read;
165         priv->reg_write  = reg_write;
166
167         priv->speed      = speed;
168         priv->btr        = btr;
169         priv->rx_probe   = rx_probe;
170         priv->clock      = clk;
171         priv->restart_ms = restart_ms;
172         priv->debug      = debug;
173
174         if (REG_READ(0) == 0xFF)
175                 goto free_dev;
176
177         /* set chip into reset mode */
178         set_reset_mode(dev);
179
180         /* go into Pelican mode, disable clkout, disable comparator */
181         REG_WRITE(REG_CDR, 0xCF);
182
183         /* output control */
184         /* connected to external transceiver */
185         REG_WRITE(REG_OCR, 0x1A);
186
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);
189
190         if (register_netdev(dev) == 0)
191                 return dev;
192
193         printk(KERN_INFO "%s: probing failed\n", chip_name);
194  free_dev:
195         free_netdev(dev);
196         return NULL;
197 }
198
199 static __exit void sja1000_gw2_cleanup_module(void)
200 {
201         int i;
202
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]);
211                 }
212         }
213         sja1000_proc_delete(drv_name);
214 }
215
216 static __init int sja1000_gw2_init_module(void)
217 {
218         int i;
219         struct net_device *dev;
220         void *base;
221
222         if (clk < 1000 ) /* MHz command line value */
223                 clk *= 1000000;
224
225         if (clk < 1000000 ) /* kHz command line value */
226                 clk *= 1000;
227
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);
232
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();
239                         return -EBUSY;
240                 }
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);
243                 if (dev != NULL) {
244                         can_dev[i] = dev;
245                         sja1000_proc_init(drv_name, can_dev, MAX_CAN);
246                 } else {
247                         can_dev[i] = NULL;
248                         iounmap(base);
249                         release_mem_region(base_addr[i], RSIZE);
250                 }
251         }
252         return 0;
253 }
254
255 module_init(sja1000_gw2_init_module);
256 module_exit(sja1000_gw2_cleanup_module);