sja1000_isa.c 7.9 KB

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