]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/sja1000/trajet-gw2.c
Fix r189 to make sure it also works for people who didn't add the slcan
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / sja1000 / trajet-gw2.c
1 /*
2  * $Id$
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 <socketcan-users@lists.berlios.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 <linux/can.h>
68 #include <linux/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.12";
83 static const char *drv_reldate  = "2005-08-22";
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 static int base_addr_n;
115 static int irq_n;
116 static int speed_n;
117 static int btr_n;
118 static int rx_probe_n;
119
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);
125
126 module_param(clk, int, 0);
127 module_param(debug, int, 0);
128 module_param(restart_ms, int, 0);
129
130 /* special functions to access the chips registers */
131 static uint8_t reg_read(struct net_device *dev, int reg)
132 {
133         static uint8_t val;
134         void __iomem *addr = (void __iomem *)dev->base_addr + reg * (ADDR_GAP + 1) + ADDR_GAP;
135
136         val = (uint8_t)readw(addr);
137         rmb();
138
139         return val;
140 }
141
142 static void reg_write(struct net_device *dev, int reg, uint8_t val)
143 {
144         void __iomem *addr = (void __iomem *)dev->base_addr + reg * (ADDR_GAP + 1) + ADDR_GAP;
145
146         writew(val, addr);
147         wmb();
148 }
149
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)
153 {
154         struct net_device       *dev;
155         struct can_priv         *priv;
156
157         if (!(dev = alloc_netdev(sizeof(struct can_priv), CAN_DEV_NAME,
158                                  sja1000_setup))) {
159                 printk(KERN_ERR "%s: out of memory\n", chip_name);
160                 return NULL;
161         }
162
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);
165
166         /* fill net_device structure */
167
168         priv             = netdev_priv(dev);
169
170         dev->irq         = irq;
171         dev->base_addr   = base;
172
173         priv->reg_read   = reg_read;
174         priv->reg_write  = reg_write;
175
176         priv->speed      = speed;
177         priv->btr        = btr;
178         priv->rx_probe   = rx_probe;
179         priv->clock      = clk;
180         priv->restart_ms = restart_ms;
181         priv->debug      = debug;
182
183         if (REG_READ(0) == 0xFF) {
184                 printk(KERN_INFO "%s: probing failed\n", chip_name);
185                 goto free_dev;
186         }
187
188         if (register_netdev(dev)) {
189                 printk(KERN_INFO "%s: register netdev failed\n", chip_name);
190                 goto free_dev;
191         }
192
193         /* set chip into reset mode */
194         set_reset_mode(dev);
195
196         /* go into Pelican mode, disable clkout, disable comparator */
197         REG_WRITE(REG_CDR, 0xCF);
198
199         /* output control */
200         /* connected to external transceiver */
201         REG_WRITE(REG_OCR, 0x1A);
202
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);
205
206         return dev;
207
208  free_dev:
209         free_netdev(dev);
210         return NULL;
211 }
212
213 static __exit void sja1000_gw2_cleanup_module(void)
214 {
215         int i;
216
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]);
225                 }
226         }
227         sja1000_proc_delete(drv_name);
228 }
229
230 static __init int sja1000_gw2_init_module(void)
231 {
232         int i;
233
234         if (clk < 1000 ) /* MHz command line value */
235                 clk *= 1000000;
236
237         if (clk < 1000000 ) /* kHz command line value */
238                 clk *= 1000;
239
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);
244
245         for (i = 0; base_addr[i]; i++) {
246
247                 struct net_device *dev = NULL;
248                 void *base;
249
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();
255                         return -EBUSY;
256                 }
257
258                 base = ioremap(base_addr[i], RSIZE);
259                 if (base)
260                         dev = sja1000_gw2_probe((uint32_t)base, irq[i], speed[i], btr[i], rx_probe[i], clk, debug, restart_ms);
261
262                 if (dev != NULL) {
263                         can_dev[i] = dev;
264                         sja1000_proc_init(drv_name, can_dev, MAX_CAN);
265                 } else {
266                         can_dev[i] = NULL;
267                         iounmap(base);
268                         release_mem_region(base_addr[i], RSIZE);
269                 }
270         }
271         return 0;
272 }
273
274 module_init(sja1000_gw2_init_module);
275 module_exit(sja1000_gw2_cleanup_module);