sja1000_isa.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/delay.h>
  22. #include <linux/irq.h>
  23. #include <linux/io.h>
  24. #include <linux/can/dev.h>
  25. #include <linux/can/platform/sja1000.h>
  26. #include "sja1000.h"
  27. #define DRV_NAME "sja1000_isa"
  28. #define MAXDEV 8
  29. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  30. MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the ISA bus");
  31. MODULE_LICENSE("GPL v2");
  32. #define CLK_DEFAULT 16000000 /* 16 MHz */
  33. #define CDR_DEFAULT (CDR_CBP | CDR_CLK_OFF)
  34. #define OCR_DEFAULT OCR_TX0_PUSHPULL
  35. static unsigned long port[MAXDEV];
  36. static unsigned long mem[MAXDEV];
  37. static int irq[MAXDEV];
  38. static int clk[MAXDEV];
  39. static unsigned char cdr[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
  40. static unsigned char ocr[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
  41. static int indirect[MAXDEV] = {[0 ... (MAXDEV - 1)] = -1};
  42. module_param_array(port, ulong, NULL, S_IRUGO);
  43. MODULE_PARM_DESC(port, "I/O port number");
  44. module_param_array(mem, ulong, NULL, S_IRUGO);
  45. MODULE_PARM_DESC(mem, "I/O memory address");
  46. module_param_array(indirect, int, NULL, S_IRUGO);
  47. MODULE_PARM_DESC(indirect, "Indirect access via address and data port");
  48. module_param_array(irq, int, NULL, S_IRUGO);
  49. MODULE_PARM_DESC(irq, "IRQ number");
  50. module_param_array(clk, int, NULL, S_IRUGO);
  51. MODULE_PARM_DESC(clk, "External oscillator clock frequency "
  52. "(default=16000000 [16 MHz])");
  53. module_param_array(cdr, byte, NULL, S_IRUGO);
  54. MODULE_PARM_DESC(cdr, "Clock divider register "
  55. "(default=0x48 [CDR_CBP | CDR_CLK_OFF])");
  56. module_param_array(ocr, byte, NULL, S_IRUGO);
  57. MODULE_PARM_DESC(ocr, "Output control register "
  58. "(default=0x18 [OCR_TX0_PUSHPULL])");
  59. #define SJA1000_IOSIZE 0x20
  60. #define SJA1000_IOSIZE_INDIRECT 0x02
  61. static struct platform_device *sja1000_isa_devs[MAXDEV];
  62. static u8 sja1000_isa_mem_read_reg(const struct sja1000_priv *priv, int reg)
  63. {
  64. return readb(priv->reg_base + reg);
  65. }
  66. static void sja1000_isa_mem_write_reg(const struct sja1000_priv *priv,
  67. int reg, u8 val)
  68. {
  69. writeb(val, priv->reg_base + reg);
  70. }
  71. static u8 sja1000_isa_port_read_reg(const struct sja1000_priv *priv, int reg)
  72. {
  73. return inb((unsigned long)priv->reg_base + reg);
  74. }
  75. static void sja1000_isa_port_write_reg(const struct sja1000_priv *priv,
  76. int reg, u8 val)
  77. {
  78. outb(val, (unsigned long)priv->reg_base + reg);
  79. }
  80. static u8 sja1000_isa_port_read_reg_indirect(const struct sja1000_priv *priv,
  81. int reg)
  82. {
  83. unsigned long base = (unsigned long)priv->reg_base;
  84. outb(reg, base);
  85. return inb(base + 1);
  86. }
  87. static void sja1000_isa_port_write_reg_indirect(const struct sja1000_priv *priv,
  88. int reg, u8 val)
  89. {
  90. unsigned long base = (unsigned long)priv->reg_base;
  91. outb(reg, base);
  92. outb(val, base + 1);
  93. }
  94. static int sja1000_isa_probe(struct platform_device *pdev)
  95. {
  96. struct net_device *dev;
  97. struct sja1000_priv *priv;
  98. void __iomem *base = NULL;
  99. int iosize = SJA1000_IOSIZE;
  100. int idx = pdev->id;
  101. int err;
  102. dev_dbg(&pdev->dev, "probing idx=%d: port=%#lx, mem=%#lx, irq=%d\n",
  103. idx, port[idx], mem[idx], irq[idx]);
  104. if (mem[idx]) {
  105. if (!request_mem_region(mem[idx], iosize, DRV_NAME)) {
  106. err = -EBUSY;
  107. goto exit;
  108. }
  109. base = ioremap_nocache(mem[idx], iosize);
  110. if (!base) {
  111. err = -ENOMEM;
  112. goto exit_release;
  113. }
  114. } else {
  115. if (indirect[idx] > 0 ||
  116. (indirect[idx] == -1 && indirect[0] > 0))
  117. iosize = SJA1000_IOSIZE_INDIRECT;
  118. if (!request_region(port[idx], iosize, DRV_NAME)) {
  119. err = -EBUSY;
  120. goto exit;
  121. }
  122. }
  123. dev = alloc_sja1000dev(0);
  124. if (!dev) {
  125. err = -ENOMEM;
  126. goto exit_unmap;
  127. }
  128. priv = netdev_priv(dev);
  129. dev->irq = irq[idx];
  130. priv->irq_flags = IRQF_SHARED;
  131. if (mem[idx]) {
  132. priv->reg_base = base;
  133. dev->base_addr = mem[idx];
  134. priv->read_reg = sja1000_isa_mem_read_reg;
  135. priv->write_reg = sja1000_isa_mem_write_reg;
  136. } else {
  137. priv->reg_base = (void __iomem *)port[idx];
  138. dev->base_addr = port[idx];
  139. if (iosize == SJA1000_IOSIZE_INDIRECT) {
  140. priv->read_reg = sja1000_isa_port_read_reg_indirect;
  141. priv->write_reg = sja1000_isa_port_write_reg_indirect;
  142. } else {
  143. priv->read_reg = sja1000_isa_port_read_reg;
  144. priv->write_reg = sja1000_isa_port_write_reg;
  145. }
  146. }
  147. if (clk[idx])
  148. priv->can.clock.freq = clk[idx] / 2;
  149. else if (clk[0])
  150. priv->can.clock.freq = clk[0] / 2;
  151. else
  152. priv->can.clock.freq = CLK_DEFAULT / 2;
  153. if (ocr[idx] != 0xff)
  154. priv->ocr = ocr[idx];
  155. else if (ocr[0] != 0xff)
  156. priv->ocr = ocr[0];
  157. else
  158. priv->ocr = OCR_DEFAULT;
  159. if (cdr[idx] != 0xff)
  160. priv->cdr = cdr[idx];
  161. else if (cdr[0] != 0xff)
  162. priv->cdr = cdr[0];
  163. else
  164. priv->cdr = CDR_DEFAULT;
  165. platform_set_drvdata(pdev, dev);
  166. SET_NETDEV_DEV(dev, &pdev->dev);
  167. err = register_sja1000dev(dev);
  168. if (err) {
  169. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  170. DRV_NAME, err);
  171. goto exit_unmap;
  172. }
  173. dev_info(&pdev->dev, "%s device registered (reg_base=0x%p, irq=%d)\n",
  174. DRV_NAME, priv->reg_base, dev->irq);
  175. return 0;
  176. exit_unmap:
  177. if (mem[idx])
  178. iounmap(base);
  179. exit_release:
  180. if (mem[idx])
  181. release_mem_region(mem[idx], iosize);
  182. else
  183. release_region(port[idx], iosize);
  184. exit:
  185. return err;
  186. }
  187. static int sja1000_isa_remove(struct platform_device *pdev)
  188. {
  189. struct net_device *dev = platform_get_drvdata(pdev);
  190. struct sja1000_priv *priv = netdev_priv(dev);
  191. int idx = pdev->id;
  192. unregister_sja1000dev(dev);
  193. if (mem[idx]) {
  194. iounmap(priv->reg_base);
  195. release_mem_region(mem[idx], SJA1000_IOSIZE);
  196. } else {
  197. if (priv->read_reg == sja1000_isa_port_read_reg_indirect)
  198. release_region(port[idx], SJA1000_IOSIZE_INDIRECT);
  199. else
  200. release_region(port[idx], SJA1000_IOSIZE);
  201. }
  202. free_sja1000dev(dev);
  203. return 0;
  204. }
  205. static struct platform_driver sja1000_isa_driver = {
  206. .probe = sja1000_isa_probe,
  207. .remove = sja1000_isa_remove,
  208. .driver = {
  209. .name = DRV_NAME,
  210. .owner = THIS_MODULE,
  211. },
  212. };
  213. static int __init sja1000_isa_init(void)
  214. {
  215. int idx, err;
  216. for (idx = 0; idx < MAXDEV; idx++) {
  217. if ((port[idx] || mem[idx]) && irq[idx]) {
  218. sja1000_isa_devs[idx] =
  219. platform_device_alloc(DRV_NAME, idx);
  220. if (!sja1000_isa_devs[idx]) {
  221. err = -ENOMEM;
  222. goto exit_free_devices;
  223. }
  224. err = platform_device_add(sja1000_isa_devs[idx]);
  225. if (err) {
  226. platform_device_put(sja1000_isa_devs[idx]);
  227. goto exit_free_devices;
  228. }
  229. pr_debug("%s: platform device %d: port=%#lx, mem=%#lx, "
  230. "irq=%d\n",
  231. DRV_NAME, idx, port[idx], mem[idx], irq[idx]);
  232. } else if (idx == 0 || port[idx] || mem[idx]) {
  233. pr_err("%s: insufficient parameters supplied\n",
  234. DRV_NAME);
  235. err = -EINVAL;
  236. goto exit_free_devices;
  237. }
  238. }
  239. err = platform_driver_register(&sja1000_isa_driver);
  240. if (err)
  241. goto exit_free_devices;
  242. pr_info("Legacy %s driver for max. %d devices registered\n",
  243. DRV_NAME, MAXDEV);
  244. return 0;
  245. exit_free_devices:
  246. while (--idx >= 0) {
  247. if (sja1000_isa_devs[idx])
  248. platform_device_unregister(sja1000_isa_devs[idx]);
  249. }
  250. return err;
  251. }
  252. static void __exit sja1000_isa_exit(void)
  253. {
  254. int idx;
  255. platform_driver_unregister(&sja1000_isa_driver);
  256. for (idx = 0; idx < MAXDEV; idx++) {
  257. if (sja1000_isa_devs[idx])
  258. platform_device_unregister(sja1000_isa_devs[idx]);
  259. }
  260. }
  261. module_init(sja1000_isa_init);
  262. module_exit(sja1000_isa_exit);