cc770_isa.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Driver for CC770 and AN82527 CAN controllers on the legacy ISA bus
  3. *
  4. * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the version 2 of the GNU General Public License
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. /*
  16. * Bosch CC770 and Intel AN82527 CAN controllers on the ISA or PC-104 bus.
  17. * The I/O port or memory address and the IRQ number must be specified via
  18. * module parameters:
  19. *
  20. * insmod cc770_isa.ko port=0x310,0x380 irq=7,11
  21. *
  22. * for ISA devices using I/O ports or:
  23. *
  24. * insmod cc770_isa.ko mem=0xd1000,0xd1000 irq=7,11
  25. *
  26. * for memory mapped ISA devices.
  27. *
  28. * Indirect access via address and data port is supported as well:
  29. *
  30. * insmod cc770_isa.ko port=0x310,0x380 indirect=1 irq=7,11
  31. *
  32. * Furthermore, the following mode parameter can be defined:
  33. *
  34. * clk: External oscillator clock frequency (default=16000000 [16 MHz])
  35. * cir: CPU interface register (default=0x40 [DSC])
  36. * bcr: Bus configuration register (default=0x40 [CBY])
  37. * cor: Clockout register (default=0x00)
  38. *
  39. * Note: for clk, cir, bcr and cor, the first argument re-defines the
  40. * default for all other devices, e.g.:
  41. *
  42. * insmod cc770_isa.ko mem=0xd1000,0xd1000 irq=7,11 clk=24000000
  43. *
  44. * is equivalent to
  45. *
  46. * insmod cc770_isa.ko mem=0xd1000,0xd1000 irq=7,11 clk=24000000,24000000
  47. */
  48. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  49. #include <linux/kernel.h>
  50. #include <linux/module.h>
  51. #include <linux/platform_device.h>
  52. #include <linux/interrupt.h>
  53. #include <linux/netdevice.h>
  54. #include <linux/delay.h>
  55. #include <linux/irq.h>
  56. #include <linux/io.h>
  57. #include <linux/can.h>
  58. #include <linux/can/dev.h>
  59. #include <linux/can/platform/cc770.h>
  60. #include "cc770.h"
  61. #define MAXDEV 8
  62. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  63. MODULE_DESCRIPTION("Socket-CAN driver for CC770 on the ISA bus");
  64. MODULE_LICENSE("GPL v2");
  65. #define CLK_DEFAULT 16000000 /* 16 MHz */
  66. #define COR_DEFAULT 0x00
  67. #define BCR_DEFAULT BUSCFG_CBY
  68. static unsigned long port[MAXDEV];
  69. static unsigned long mem[MAXDEV];
  70. static int __devinitdata irq[MAXDEV];
  71. static int __devinitdata clk[MAXDEV];
  72. static u8 __devinitdata cir[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
  73. static u8 __devinitdata cor[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
  74. static u8 __devinitdata bcr[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
  75. static int __devinitdata indirect[MAXDEV] = {[0 ... (MAXDEV - 1)] = -1};
  76. module_param_array(port, ulong, NULL, S_IRUGO);
  77. MODULE_PARM_DESC(port, "I/O port number");
  78. module_param_array(mem, ulong, NULL, S_IRUGO);
  79. MODULE_PARM_DESC(mem, "I/O memory address");
  80. module_param_array(indirect, int, NULL, S_IRUGO);
  81. MODULE_PARM_DESC(indirect, "Indirect access via address and data port");
  82. module_param_array(irq, int, NULL, S_IRUGO);
  83. MODULE_PARM_DESC(irq, "IRQ number");
  84. module_param_array(clk, int, NULL, S_IRUGO);
  85. MODULE_PARM_DESC(clk, "External oscillator clock frequency "
  86. "(default=16000000 [16 MHz])");
  87. module_param_array(cir, byte, NULL, S_IRUGO);
  88. MODULE_PARM_DESC(cir, "CPU interface register (default=0x40 [DSC])");
  89. module_param_array(cor, byte, NULL, S_IRUGO);
  90. MODULE_PARM_DESC(cor, "Clockout register (default=0x00)");
  91. module_param_array(bcr, byte, NULL, S_IRUGO);
  92. MODULE_PARM_DESC(bcr, "Bus configuration register (default=0x40 [CBY])");
  93. #define CC770_IOSIZE 0x20
  94. #define CC770_IOSIZE_INDIRECT 0x02
  95. static struct platform_device *cc770_isa_devs[MAXDEV];
  96. static u8 cc770_isa_mem_read_reg(const struct cc770_priv *priv, int reg)
  97. {
  98. return readb(priv->reg_base + reg);
  99. }
  100. static void cc770_isa_mem_write_reg(const struct cc770_priv *priv,
  101. int reg, u8 val)
  102. {
  103. writeb(val, priv->reg_base + reg);
  104. }
  105. static u8 cc770_isa_port_read_reg(const struct cc770_priv *priv, int reg)
  106. {
  107. return inb((unsigned long)priv->reg_base + reg);
  108. }
  109. static void cc770_isa_port_write_reg(const struct cc770_priv *priv,
  110. int reg, u8 val)
  111. {
  112. outb(val, (unsigned long)priv->reg_base + reg);
  113. }
  114. static u8 cc770_isa_port_read_reg_indirect(const struct cc770_priv *priv,
  115. int reg)
  116. {
  117. unsigned long base = (unsigned long)priv->reg_base;
  118. outb(reg, base);
  119. return inb(base + 1);
  120. }
  121. static void cc770_isa_port_write_reg_indirect(const struct cc770_priv *priv,
  122. int reg, u8 val)
  123. {
  124. unsigned long base = (unsigned long)priv->reg_base;
  125. outb(reg, base);
  126. outb(val, base + 1);
  127. }
  128. static int __devinit cc770_isa_probe(struct platform_device *pdev)
  129. {
  130. struct net_device *dev;
  131. struct cc770_priv *priv;
  132. void __iomem *base = NULL;
  133. int iosize = CC770_IOSIZE;
  134. int idx = pdev->id;
  135. int err;
  136. u32 clktmp;
  137. dev_dbg(&pdev->dev, "probing idx=%d: port=%#lx, mem=%#lx, irq=%d\n",
  138. idx, port[idx], mem[idx], irq[idx]);
  139. if (mem[idx]) {
  140. if (!request_mem_region(mem[idx], iosize, KBUILD_MODNAME)) {
  141. err = -EBUSY;
  142. goto exit;
  143. }
  144. base = ioremap_nocache(mem[idx], iosize);
  145. if (!base) {
  146. err = -ENOMEM;
  147. goto exit_release;
  148. }
  149. } else {
  150. if (indirect[idx] > 0 ||
  151. (indirect[idx] == -1 && indirect[0] > 0))
  152. iosize = CC770_IOSIZE_INDIRECT;
  153. if (!request_region(port[idx], iosize, KBUILD_MODNAME)) {
  154. err = -EBUSY;
  155. goto exit;
  156. }
  157. }
  158. dev = alloc_cc770dev(0);
  159. if (!dev) {
  160. err = -ENOMEM;
  161. goto exit_unmap;
  162. }
  163. priv = netdev_priv(dev);
  164. dev->irq = irq[idx];
  165. priv->irq_flags = IRQF_SHARED;
  166. if (mem[idx]) {
  167. priv->reg_base = base;
  168. dev->base_addr = mem[idx];
  169. priv->read_reg = cc770_isa_mem_read_reg;
  170. priv->write_reg = cc770_isa_mem_write_reg;
  171. } else {
  172. priv->reg_base = (void __iomem *)port[idx];
  173. dev->base_addr = port[idx];
  174. if (iosize == CC770_IOSIZE_INDIRECT) {
  175. priv->read_reg = cc770_isa_port_read_reg_indirect;
  176. priv->write_reg = cc770_isa_port_write_reg_indirect;
  177. } else {
  178. priv->read_reg = cc770_isa_port_read_reg;
  179. priv->write_reg = cc770_isa_port_write_reg;
  180. }
  181. }
  182. if (clk[idx])
  183. clktmp = clk[idx];
  184. else if (clk[0])
  185. clktmp = clk[0];
  186. else
  187. clktmp = CLK_DEFAULT;
  188. priv->can.clock.freq = clktmp;
  189. if (cir[idx] != 0xff) {
  190. priv->cpu_interface = cir[idx];
  191. } else if (cir[0] != 0xff) {
  192. priv->cpu_interface = cir[0];
  193. } else {
  194. /* The system clock may not exceed 10 MHz */
  195. if (clktmp > 10000000) {
  196. priv->cpu_interface |= CPUIF_DSC;
  197. clktmp /= 2;
  198. }
  199. /* The memory clock may not exceed 8 MHz */
  200. if (clktmp > 8000000)
  201. priv->cpu_interface |= CPUIF_DMC;
  202. }
  203. if (priv->cpu_interface & CPUIF_DSC)
  204. priv->can.clock.freq /= 2;
  205. if (bcr[idx] != 0xff)
  206. priv->bus_config = bcr[idx];
  207. else if (bcr[0] != 0xff)
  208. priv->bus_config = bcr[0];
  209. else
  210. priv->bus_config = BCR_DEFAULT;
  211. if (cor[idx] != 0xff)
  212. priv->clkout = cor[idx];
  213. else if (cor[0] != 0xff)
  214. priv->clkout = cor[0];
  215. else
  216. priv->clkout = COR_DEFAULT;
  217. dev_set_drvdata(&pdev->dev, dev);
  218. SET_NETDEV_DEV(dev, &pdev->dev);
  219. err = register_cc770dev(dev);
  220. if (err) {
  221. dev_err(&pdev->dev,
  222. "couldn't register device (err=%d)\n", err);
  223. goto exit_unmap;
  224. }
  225. dev_info(&pdev->dev, "device registered (reg_base=0x%p, irq=%d)\n",
  226. priv->reg_base, dev->irq);
  227. return 0;
  228. exit_unmap:
  229. if (mem[idx])
  230. iounmap(base);
  231. exit_release:
  232. if (mem[idx])
  233. release_mem_region(mem[idx], iosize);
  234. else
  235. release_region(port[idx], iosize);
  236. exit:
  237. return err;
  238. }
  239. static int __devexit cc770_isa_remove(struct platform_device *pdev)
  240. {
  241. struct net_device *dev = dev_get_drvdata(&pdev->dev);
  242. struct cc770_priv *priv = netdev_priv(dev);
  243. int idx = pdev->id;
  244. unregister_cc770dev(dev);
  245. dev_set_drvdata(&pdev->dev, NULL);
  246. if (mem[idx]) {
  247. iounmap(priv->reg_base);
  248. release_mem_region(mem[idx], CC770_IOSIZE);
  249. } else {
  250. if (priv->read_reg == cc770_isa_port_read_reg_indirect)
  251. release_region(port[idx], CC770_IOSIZE_INDIRECT);
  252. else
  253. release_region(port[idx], CC770_IOSIZE);
  254. }
  255. free_cc770dev(dev);
  256. return 0;
  257. }
  258. static struct platform_driver cc770_isa_driver = {
  259. .probe = cc770_isa_probe,
  260. .remove = __devexit_p(cc770_isa_remove),
  261. .driver = {
  262. .name = KBUILD_MODNAME,
  263. .owner = THIS_MODULE,
  264. },
  265. };
  266. static int __init cc770_isa_init(void)
  267. {
  268. int idx, err;
  269. for (idx = 0; idx < ARRAY_SIZE(cc770_isa_devs); idx++) {
  270. if ((port[idx] || mem[idx]) && irq[idx]) {
  271. cc770_isa_devs[idx] =
  272. platform_device_alloc(KBUILD_MODNAME, idx);
  273. if (!cc770_isa_devs[idx]) {
  274. err = -ENOMEM;
  275. goto exit_free_devices;
  276. }
  277. err = platform_device_add(cc770_isa_devs[idx]);
  278. if (err) {
  279. platform_device_put(cc770_isa_devs[idx]);
  280. goto exit_free_devices;
  281. }
  282. pr_debug("platform device %d: port=%#lx, mem=%#lx, "
  283. "irq=%d\n",
  284. idx, port[idx], mem[idx], irq[idx]);
  285. } else if (idx == 0 || port[idx] || mem[idx]) {
  286. pr_err("insufficient parameters supplied\n");
  287. err = -EINVAL;
  288. goto exit_free_devices;
  289. }
  290. }
  291. err = platform_driver_register(&cc770_isa_driver);
  292. if (err)
  293. goto exit_free_devices;
  294. pr_info("driver for max. %d devices registered\n", MAXDEV);
  295. return 0;
  296. exit_free_devices:
  297. while (--idx >= 0) {
  298. if (cc770_isa_devs[idx])
  299. platform_device_unregister(cc770_isa_devs[idx]);
  300. }
  301. return err;
  302. }
  303. module_init(cc770_isa_init);
  304. static void __exit cc770_isa_exit(void)
  305. {
  306. int idx;
  307. platform_driver_unregister(&cc770_isa_driver);
  308. for (idx = 0; idx < ARRAY_SIZE(cc770_isa_devs); idx++) {
  309. if (cc770_isa_devs[idx])
  310. platform_device_unregister(cc770_isa_devs[idx]);
  311. }
  312. }
  313. module_exit(cc770_isa_exit);