]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/sja1000/mem.c
reorganized kernel include files:
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / sja1000 / mem.c
1 /*
2  * $Id$
3  *
4  * mem.c - Philips SJA1000 network device driver for IOMEM
5  *
6  * Copyright (c) 2002-2005 Volkswagen Group Electronic Research
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
21  *
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.
26  *
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.
29  *
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
41  * DAMAGE.
42  *
43  * Send feedback to <socketcan-users@lists.berlios.de>
44  *
45  */
46
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>
56 #include <linux/in.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>
62 #include <asm/io.h>
63
64 #include <linux/can.h>
65 #include <linux/can/ioctl.h> /* for struct can_device_stats */
66 #include "sja1000.h"
67
68 #define MAX_CAN         8
69 #define CAN_DEV_NAME    "can%d"
70 #define DRV_NAME        "sja1000-mem"
71
72 #define DEFAULT_KBIT_PER_SEC 500
73 #define SJA1000_HW_CLOCK 16000000
74
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;
80
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 "'");
84
85 /* module parameters */
86 static uint32_t base_addr[MAX_CAN] = { (uint32_t)0xda000L, 0};
87
88 static int irq[MAX_CAN] = { 9, 0 };
89
90 static int speed[MAX_CAN] = { DEFAULT_KBIT_PER_SEC, DEFAULT_KBIT_PER_SEC, 0};
91
92 static int btr[MAX_CAN] = { 0 };
93 static int rx_probe[MAX_CAN] = { 0 };
94
95 static int clk = SJA1000_HW_CLOCK;
96 static int debug = 0;
97 static int restart_ms = 100;
98
99 /* array of all can chips */
100 static struct net_device        *can_dev[MAX_CAN];
101
102 static int base_addr_n;
103 static int irq_n;
104 static int speed_n;
105 static int btr_n;
106 static int rx_probe_n;
107
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);
113
114 module_param(clk, int, 0);
115 module_param(debug, int, 0);
116 module_param(restart_ms, int, 0);
117
118 /* special functions to access the chips registers */
119 static uint8_t reg_read(struct net_device *dev, int reg)
120 {
121         static uint8_t val;
122         void __iomem *addr = (void __iomem *)dev->base_addr + reg;
123
124         val = (uint8_t)readw(addr);
125         rmb();
126
127         return val;
128 }
129
130 static void reg_write(struct net_device *dev, int reg, uint8_t val)
131 {
132         void __iomem *addr = (void __iomem *)dev->base_addr + reg;
133
134         writew(val, addr);
135         wmb();
136 }
137
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)
141 {
142         struct net_device       *dev;
143         struct can_priv         *priv;
144
145         if (!(dev = alloc_netdev(sizeof(struct can_priv), CAN_DEV_NAME,
146                                  sja1000_setup))) {
147                 printk(KERN_ERR "%s: out of memory\n", chip_name);
148                 return NULL;
149         }
150
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);
153
154         /* fill net_device structure */
155
156         priv             = netdev_priv(dev);
157
158         dev->irq         = irq;
159         dev->base_addr   = base;
160
161         priv->reg_read   = reg_read;
162         priv->reg_write  = reg_write;
163
164         priv->speed      = speed;
165         priv->btr        = btr;
166         priv->rx_probe   = rx_probe;
167         priv->clock      = clk;
168         priv->restart_ms = restart_ms;
169         priv->debug      = debug;
170
171         if (REG_READ(0) == 0xFF)
172                 goto free_dev;
173
174         /* set chip into reset mode */
175         set_reset_mode(dev);
176
177         /* go into Pelican mode, disable clkout, disable comparator */
178         REG_WRITE(REG_CDR, 0xCF);
179
180         /* output control */
181         /* connected to external transceiver */
182         REG_WRITE(REG_OCR, 0x1A);
183
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);
186
187         if (register_netdev(dev) == 0)
188                 return dev;
189
190         printk(KERN_INFO "%s: probing failed\n", chip_name);
191  free_dev:
192         free_netdev(dev);
193         return NULL;
194 }
195
196 static __exit void sja1000_mem_cleanup_module(void)
197 {
198         int i;
199
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]);
208                 }
209         }
210         sja1000_proc_delete(drv_name);
211 }
212
213 static __init int sja1000_mem_init_module(void)
214 {
215         int i;
216
217         if (clk < 1000 ) /* MHz command line value */
218                 clk *= 1000000;
219
220         if (clk < 1000000 ) /* kHz command line value */
221                 clk *= 1000;
222
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);
227
228         for (i = 0; base_addr[i]; i++) {
229
230                 struct net_device *dev = NULL;
231                 void *base;
232
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();
238                         return -EBUSY;
239                 }
240
241                 base = ioremap(base_addr[i], SJA1000_IO_SIZE_BASIC);
242                 if (base)
243                         dev = sja1000_mem_probe((uint32_t)base, irq[i], speed[i], btr[i], rx_probe[i], clk, debug, restart_ms);
244                 if (dev != NULL) {
245                         can_dev[i] = dev;
246                         sja1000_proc_init(drv_name, can_dev, MAX_CAN);
247                 } else {
248                         can_dev[i] = NULL;
249                         iounmap(base);
250                         release_mem_region(base_addr[i], SJA1000_IO_SIZE_BASIC);
251                 }
252         }
253         return 0;
254 }
255
256 module_init(sja1000_mem_init_module);
257 module_exit(sja1000_mem_cleanup_module);