1 /* 2 * Copyright (C) 2009 Wolfgang Grandegger <wg@grandegger.com> 3 * 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 7 * 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. 12 * 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/>. 15 */ 16 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> 24 #include <linux/io.h> 25 #include <linux/can/dev.h> 26 #include <linux/can/platform/sja1000.h> 27 28 #include "sja1000.h" 29 30 #define DRV_NAME "sja1000_isa" 31 32 #define MAXDEV 8 33 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"); 37 38 #define CLK_DEFAULT 16000000 /* 16 MHz */ 39 #define CDR_DEFAULT (CDR_CBP | CDR_CLK_OFF) 40 #define OCR_DEFAULT OCR_TX0_PUSHPULL 41 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}; 49 50 module_param_array(port, ulong, NULL, S_IRUGO); 51 MODULE_PARM_DESC(port, "I/O port number"); 52 53 module_param_array(mem, ulong, NULL, S_IRUGO); 54 MODULE_PARM_DESC(mem, "I/O memory address"); 55 56 module_param_array(indirect, int, NULL, S_IRUGO); 57 MODULE_PARM_DESC(indirect, "Indirect access via address and data port"); 58 59 module_param_array(irq, int, NULL, S_IRUGO); 60 MODULE_PARM_DESC(irq, "IRQ number"); 61 62 module_param_array(clk, int, NULL, S_IRUGO); 63 MODULE_PARM_DESC(clk, "External oscillator clock frequency " 64 "(default=16000000 [16 MHz])"); 65 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])"); 69 70 module_param_array(ocr, byte, NULL, S_IRUGO); 71 MODULE_PARM_DESC(ocr, "Output control register " 72 "(default=0x18 [OCR_TX0_PUSHPULL])"); 73 74 #define SJA1000_IOSIZE 0x20 75 #define SJA1000_IOSIZE_INDIRECT 0x02 76 77 static struct platform_device *sja1000_isa_devs[MAXDEV]; 78 79 static u8 sja1000_isa_mem_read_reg(const struct sja1000_priv *priv, int reg) 80 { 81 return readb(priv->reg_base + reg); 82 } 83 84 static void sja1000_isa_mem_write_reg(const struct sja1000_priv *priv, 85 int reg, u8 val) 86 { 87 writeb(val, priv->reg_base + reg); 88 } 89 90 static u8 sja1000_isa_port_read_reg(const struct sja1000_priv *priv, int reg) 91 { 92 return inb((unsigned long)priv->reg_base + reg); 93 } 94 95 static void sja1000_isa_port_write_reg(const struct sja1000_priv *priv, 96 int reg, u8 val) 97 { 98 outb(val, (unsigned long)priv->reg_base + reg); 99 } 100 101 static u8 sja1000_isa_port_read_reg_indirect(const struct sja1000_priv *priv, 102 int reg) 103 { 104 unsigned long base = (unsigned long)priv->reg_base; 105 106 outb(reg, base); 107 return inb(base + 1); 108 } 109 110 static void sja1000_isa_port_write_reg_indirect(const struct sja1000_priv *priv, 111 int reg, u8 val) 112 { 113 unsigned long base = (unsigned long)priv->reg_base; 114 115 outb(reg, base); 116 outb(val, base + 1); 117 } 118 119 static int sja1000_isa_probe(struct platform_device *pdev) 120 { 121 struct net_device *dev; 122 struct sja1000_priv *priv; 123 void __iomem *base = NULL; 124 int iosize = SJA1000_IOSIZE; 125 int idx = pdev->id; 126 int err; 127 128 dev_dbg(&pdev->dev, "probing idx=%d: port=%#lx, mem=%#lx, irq=%d\n", 129 idx, port[idx], mem[idx], irq[idx]); 130 131 if (mem[idx]) { 132 if (!request_mem_region(mem[idx], iosize, DRV_NAME)) { 133 err = -EBUSY; 134 goto exit; 135 } 136 base = ioremap_nocache(mem[idx], iosize); 137 if (!base) { 138 err = -ENOMEM; 139 goto exit_release; 140 } 141 } else { 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)) { 146 err = -EBUSY; 147 goto exit; 148 } 149 } 150 151 dev = alloc_sja1000dev(0); 152 if (!dev) { 153 err = -ENOMEM; 154 goto exit_unmap; 155 } 156 priv = netdev_priv(dev); 157 158 dev->irq = irq[idx]; 159 priv->irq_flags = IRQF_SHARED; 160 if (mem[idx]) { 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; 165 } else { 166 priv->reg_base = (void __iomem *)port[idx]; 167 dev->base_addr = port[idx]; 168 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; 172 } else { 173 priv->read_reg = sja1000_isa_port_read_reg; 174 priv->write_reg = sja1000_isa_port_write_reg; 175 } 176 } 177 178 if (clk[idx]) 179 priv->can.clock.freq = clk[idx] / 2; 180 else if (clk[0]) 181 priv->can.clock.freq = clk[0] / 2; 182 else 183 priv->can.clock.freq = CLK_DEFAULT / 2; 184 185 if (ocr[idx] != 0xff) 186 priv->ocr = ocr[idx]; 187 else if (ocr[0] != 0xff) 188 priv->ocr = ocr[0]; 189 else 190 priv->ocr = OCR_DEFAULT; 191 192 if (cdr[idx] != 0xff) 193 priv->cdr = cdr[idx]; 194 else if (cdr[0] != 0xff) 195 priv->cdr = cdr[0]; 196 else 197 priv->cdr = CDR_DEFAULT; 198 199 platform_set_drvdata(pdev, dev); 200 SET_NETDEV_DEV(dev, &pdev->dev); 201 202 err = register_sja1000dev(dev); 203 if (err) { 204 dev_err(&pdev->dev, "registering %s failed (err=%d)\n", 205 DRV_NAME, err); 206 goto exit_unmap; 207 } 208 209 dev_info(&pdev->dev, "%s device registered (reg_base=0x%p, irq=%d)\n", 210 DRV_NAME, priv->reg_base, dev->irq); 211 return 0; 212 213 exit_unmap: 214 if (mem[idx]) 215 iounmap(base); 216 exit_release: 217 if (mem[idx]) 218 release_mem_region(mem[idx], iosize); 219 else 220 release_region(port[idx], iosize); 221 exit: 222 return err; 223 } 224 225 static int sja1000_isa_remove(struct platform_device *pdev) 226 { 227 struct net_device *dev = platform_get_drvdata(pdev); 228 struct sja1000_priv *priv = netdev_priv(dev); 229 int idx = pdev->id; 230 231 unregister_sja1000dev(dev); 232 233 if (mem[idx]) { 234 iounmap(priv->reg_base); 235 release_mem_region(mem[idx], SJA1000_IOSIZE); 236 } else { 237 if (priv->read_reg == sja1000_isa_port_read_reg_indirect) 238 release_region(port[idx], SJA1000_IOSIZE_INDIRECT); 239 else 240 release_region(port[idx], SJA1000_IOSIZE); 241 } 242 free_sja1000dev(dev); 243 244 return 0; 245 } 246 247 static struct platform_driver sja1000_isa_driver = { 248 .probe = sja1000_isa_probe, 249 .remove = sja1000_isa_remove, 250 .driver = { 251 .name = DRV_NAME, 252 .owner = THIS_MODULE, 253 }, 254 }; 255 256 static int __init sja1000_isa_init(void) 257 { 258 int idx, err; 259 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]) { 265 err = -ENOMEM; 266 goto exit_free_devices; 267 } 268 err = platform_device_add(sja1000_isa_devs[idx]); 269 if (err) { 270 platform_device_put(sja1000_isa_devs[idx]); 271 goto exit_free_devices; 272 } 273 pr_debug("%s: platform device %d: port=%#lx, mem=%#lx, " 274 "irq=%d\n", 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", 278 DRV_NAME); 279 err = -EINVAL; 280 goto exit_free_devices; 281 } 282 } 283 284 err = platform_driver_register(&sja1000_isa_driver); 285 if (err) 286 goto exit_free_devices; 287 288 pr_info("Legacy %s driver for max. %d devices registered\n", 289 DRV_NAME, MAXDEV); 290 291 return 0; 292 293 exit_free_devices: 294 while (--idx >= 0) { 295 if (sja1000_isa_devs[idx]) 296 platform_device_unregister(sja1000_isa_devs[idx]); 297 } 298 299 return err; 300 } 301 302 static void __exit sja1000_isa_exit(void) 303 { 304 int idx; 305 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]); 310 } 311 } 312 313 module_init(sja1000_isa_init); 314 module_exit(sja1000_isa_exit); 315