2 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the version 2 of the GNU General Public License
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/interrupt.h>
21 #include <linux/netdevice.h>
22 #include <linux/delay.h>
23 #include <linux/irq.h>
25 #include <linux/can/dev.h>
26 #include <linux/can/platform/sja1000.h>
30 #define DRV_NAME "sja1000_isa"
34 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
35 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the ISA bus");
36 MODULE_LICENSE("GPL v2");
38 #define CLK_DEFAULT 16000000 /* 16 MHz */
39 #define CDR_DEFAULT (CDR_CBP | CDR_CLK_OFF)
40 #define OCR_DEFAULT OCR_TX0_PUSHPULL
42 static unsigned long port[MAXDEV];
43 static unsigned long mem[MAXDEV];
44 static int irq[MAXDEV];
45 static int clk[MAXDEV];
46 static unsigned char cdr[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
47 static unsigned char ocr[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
48 static int indirect[MAXDEV] = {[0 ... (MAXDEV - 1)] = -1};
50 module_param_array(port, ulong, NULL, S_IRUGO);
51 MODULE_PARM_DESC(port, "I/O port number");
53 module_param_array(mem, ulong, NULL, S_IRUGO);
54 MODULE_PARM_DESC(mem, "I/O memory address");
56 module_param_array(indirect, int, NULL, S_IRUGO);
57 MODULE_PARM_DESC(indirect, "Indirect access via address and data port");
59 module_param_array(irq, int, NULL, S_IRUGO);
60 MODULE_PARM_DESC(irq, "IRQ number");
62 module_param_array(clk, int, NULL, S_IRUGO);
63 MODULE_PARM_DESC(clk, "External oscillator clock frequency "
64 "(default=16000000 [16 MHz])");
66 module_param_array(cdr, byte, NULL, S_IRUGO);
67 MODULE_PARM_DESC(cdr, "Clock divider register "
68 "(default=0x48 [CDR_CBP | CDR_CLK_OFF])");
70 module_param_array(ocr, byte, NULL, S_IRUGO);
71 MODULE_PARM_DESC(ocr, "Output control register "
72 "(default=0x18 [OCR_TX0_PUSHPULL])");
74 #define SJA1000_IOSIZE 0x20
75 #define SJA1000_IOSIZE_INDIRECT 0x02
77 static struct platform_device *sja1000_isa_devs[MAXDEV];
79 static u8 sja1000_isa_mem_read_reg(const struct sja1000_priv *priv, int reg)
81 return readb(priv->reg_base + reg);
84 static void sja1000_isa_mem_write_reg(const struct sja1000_priv *priv,
87 writeb(val, priv->reg_base + reg);
90 static u8 sja1000_isa_port_read_reg(const struct sja1000_priv *priv, int reg)
92 return inb((unsigned long)priv->reg_base + reg);
95 static void sja1000_isa_port_write_reg(const struct sja1000_priv *priv,
98 outb(val, (unsigned long)priv->reg_base + reg);
101 static u8 sja1000_isa_port_read_reg_indirect(const struct sja1000_priv *priv,
104 unsigned long base = (unsigned long)priv->reg_base;
107 return inb(base + 1);
110 static void sja1000_isa_port_write_reg_indirect(const struct sja1000_priv *priv,
113 unsigned long base = (unsigned long)priv->reg_base;
119 static int sja1000_isa_probe(struct platform_device *pdev)
121 struct net_device *dev;
122 struct sja1000_priv *priv;
123 void __iomem *base = NULL;
124 int iosize = SJA1000_IOSIZE;
128 dev_dbg(&pdev->dev, "probing idx=%d: port=%#lx, mem=%#lx, irq=%d\n",
129 idx, port[idx], mem[idx], irq[idx]);
132 if (!request_mem_region(mem[idx], iosize, DRV_NAME)) {
136 base = ioremap_nocache(mem[idx], iosize);
142 if (indirect[idx] > 0 ||
143 (indirect[idx] == -1 && indirect[0] > 0))
144 iosize = SJA1000_IOSIZE_INDIRECT;
145 if (!request_region(port[idx], iosize, DRV_NAME)) {
151 dev = alloc_sja1000dev(0);
156 priv = netdev_priv(dev);
159 priv->irq_flags = IRQF_SHARED;
161 priv->reg_base = base;
162 dev->base_addr = mem[idx];
163 priv->read_reg = sja1000_isa_mem_read_reg;
164 priv->write_reg = sja1000_isa_mem_write_reg;
166 priv->reg_base = (void __iomem *)port[idx];
167 dev->base_addr = port[idx];
169 if (iosize == SJA1000_IOSIZE_INDIRECT) {
170 priv->read_reg = sja1000_isa_port_read_reg_indirect;
171 priv->write_reg = sja1000_isa_port_write_reg_indirect;
173 priv->read_reg = sja1000_isa_port_read_reg;
174 priv->write_reg = sja1000_isa_port_write_reg;
179 priv->can.clock.freq = clk[idx] / 2;
181 priv->can.clock.freq = clk[0] / 2;
183 priv->can.clock.freq = CLK_DEFAULT / 2;
185 if (ocr[idx] != 0xff)
186 priv->ocr = ocr[idx];
187 else if (ocr[0] != 0xff)
190 priv->ocr = OCR_DEFAULT;
192 if (cdr[idx] != 0xff)
193 priv->cdr = cdr[idx];
194 else if (cdr[0] != 0xff)
197 priv->cdr = CDR_DEFAULT;
199 platform_set_drvdata(pdev, dev);
200 SET_NETDEV_DEV(dev, &pdev->dev);
202 err = register_sja1000dev(dev);
204 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
209 dev_info(&pdev->dev, "%s device registered (reg_base=0x%p, irq=%d)\n",
210 DRV_NAME, priv->reg_base, dev->irq);
218 release_mem_region(mem[idx], iosize);
220 release_region(port[idx], iosize);
225 static int sja1000_isa_remove(struct platform_device *pdev)
227 struct net_device *dev = platform_get_drvdata(pdev);
228 struct sja1000_priv *priv = netdev_priv(dev);
231 unregister_sja1000dev(dev);
234 iounmap(priv->reg_base);
235 release_mem_region(mem[idx], SJA1000_IOSIZE);
237 if (priv->read_reg == sja1000_isa_port_read_reg_indirect)
238 release_region(port[idx], SJA1000_IOSIZE_INDIRECT);
240 release_region(port[idx], SJA1000_IOSIZE);
242 free_sja1000dev(dev);
247 static struct platform_driver sja1000_isa_driver = {
248 .probe = sja1000_isa_probe,
249 .remove = sja1000_isa_remove,
252 .owner = THIS_MODULE,
256 static int __init sja1000_isa_init(void)
260 for (idx = 0; idx < MAXDEV; idx++) {
261 if ((port[idx] || mem[idx]) && irq[idx]) {
262 sja1000_isa_devs[idx] =
263 platform_device_alloc(DRV_NAME, idx);
264 if (!sja1000_isa_devs[idx]) {
266 goto exit_free_devices;
268 err = platform_device_add(sja1000_isa_devs[idx]);
270 platform_device_put(sja1000_isa_devs[idx]);
271 goto exit_free_devices;
273 pr_debug("%s: platform device %d: port=%#lx, mem=%#lx, "
275 DRV_NAME, idx, port[idx], mem[idx], irq[idx]);
276 } else if (idx == 0 || port[idx] || mem[idx]) {
277 pr_err("%s: insufficient parameters supplied\n",
280 goto exit_free_devices;
284 err = platform_driver_register(&sja1000_isa_driver);
286 goto exit_free_devices;
288 pr_info("Legacy %s driver for max. %d devices registered\n",
295 if (sja1000_isa_devs[idx])
296 platform_device_unregister(sja1000_isa_devs[idx]);
302 static void __exit sja1000_isa_exit(void)
306 platform_driver_unregister(&sja1000_isa_driver);
307 for (idx = 0; idx < MAXDEV; idx++) {
308 if (sja1000_isa_devs[idx])
309 platform_device_unregister(sja1000_isa_devs[idx]);
313 module_init(sja1000_isa_init);
314 module_exit(sja1000_isa_exit);