c_can_platform.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Platform CAN bus driver for Bosch C_CAN controller
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Bhupesh Sharma <bhupesh.sharma@st.com>
  6. *
  7. * Borrowed heavily from the C_CAN driver originally written by:
  8. * Copyright (C) 2007
  9. * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
  10. * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
  11. *
  12. * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
  13. * Bosch C_CAN user manual can be obtained from:
  14. * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
  15. * users_manual_c_can.pdf
  16. *
  17. * This file is licensed under the terms of the GNU General Public
  18. * License version 2. This program is licensed "as is" without any
  19. * warranty of any kind, whether express or implied.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/version.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/delay.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/list.h>
  30. #include <linux/delay.h>
  31. #include <linux/io.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/clk.h>
  34. #include <linux/can/dev.h>
  35. #include "c_can.h"
  36. /*
  37. * 16-bit c_can registers can be arranged differently in the memory
  38. * architecture of different implementations. For example: 16-bit
  39. * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
  40. * Handle the same by providing a common read/write interface.
  41. */
  42. static u16 c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv *priv,
  43. void *reg)
  44. {
  45. return readw(reg);
  46. }
  47. static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv *priv,
  48. void *reg, u16 val)
  49. {
  50. writew(val, reg);
  51. }
  52. static u16 c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv *priv,
  53. void *reg)
  54. {
  55. return readw(reg + (long)reg - (long)priv->regs);
  56. }
  57. static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv *priv,
  58. void *reg, u16 val)
  59. {
  60. writew(val, reg + (long)reg - (long)priv->regs);
  61. }
  62. static int __devinit c_can_plat_probe(struct platform_device *pdev)
  63. {
  64. int ret;
  65. void __iomem *addr;
  66. struct net_device *dev;
  67. struct c_can_priv *priv;
  68. struct resource *mem, *irq;
  69. #ifdef CONFIG_HAVE_CLK
  70. struct clk *clk;
  71. /* get the appropriate clk */
  72. clk = clk_get(&pdev->dev, NULL);
  73. if (IS_ERR(clk)) {
  74. dev_err(&pdev->dev, "no clock defined\n");
  75. ret = -ENODEV;
  76. goto exit;
  77. }
  78. #endif
  79. /* get the platform data */
  80. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  81. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  82. if (!mem || (irq <= 0)) {
  83. ret = -ENODEV;
  84. goto exit_free_clk;
  85. }
  86. if (!request_mem_region(mem->start, resource_size(mem),
  87. KBUILD_MODNAME)) {
  88. dev_err(&pdev->dev, "resource unavailable\n");
  89. ret = -ENODEV;
  90. goto exit_free_clk;
  91. }
  92. addr = ioremap(mem->start, resource_size(mem));
  93. if (!addr) {
  94. dev_err(&pdev->dev, "failed to map can port\n");
  95. ret = -ENOMEM;
  96. goto exit_release_mem;
  97. }
  98. /* allocate the c_can device */
  99. dev = alloc_c_can_dev();
  100. if (!dev) {
  101. ret = -ENOMEM;
  102. goto exit_iounmap;
  103. }
  104. priv = netdev_priv(dev);
  105. dev->irq = irq->start;
  106. priv->regs = addr;
  107. #ifdef CONFIG_HAVE_CLK
  108. priv->can.clock.freq = clk_get_rate(clk);
  109. priv->priv = clk;
  110. #endif
  111. switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
  112. case IORESOURCE_MEM_32BIT:
  113. priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
  114. priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
  115. break;
  116. case IORESOURCE_MEM_16BIT:
  117. default:
  118. priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
  119. priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
  120. break;
  121. }
  122. platform_set_drvdata(pdev, dev);
  123. SET_NETDEV_DEV(dev, &pdev->dev);
  124. ret = register_c_can_dev(dev);
  125. if (ret) {
  126. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  127. KBUILD_MODNAME, ret);
  128. goto exit_free_device;
  129. }
  130. dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
  131. KBUILD_MODNAME, priv->regs, dev->irq);
  132. return 0;
  133. exit_free_device:
  134. platform_set_drvdata(pdev, NULL);
  135. free_c_can_dev(dev);
  136. exit_iounmap:
  137. iounmap(addr);
  138. exit_release_mem:
  139. release_mem_region(mem->start, resource_size(mem));
  140. exit_free_clk:
  141. #ifdef CONFIG_HAVE_CLK
  142. clk_put(clk);
  143. exit:
  144. #endif
  145. dev_err(&pdev->dev, "probe failed\n");
  146. return ret;
  147. }
  148. static int __devexit c_can_plat_remove(struct platform_device *pdev)
  149. {
  150. struct net_device *dev = platform_get_drvdata(pdev);
  151. struct c_can_priv *priv = netdev_priv(dev);
  152. struct resource *mem;
  153. unregister_c_can_dev(dev);
  154. platform_set_drvdata(pdev, NULL);
  155. free_c_can_dev(dev);
  156. iounmap(priv->regs);
  157. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. release_mem_region(mem->start, resource_size(mem));
  159. #ifdef CONFIG_HAVE_CLK
  160. clk_put(priv->priv);
  161. #endif
  162. return 0;
  163. }
  164. static struct platform_driver c_can_plat_driver = {
  165. .driver = {
  166. .name = KBUILD_MODNAME,
  167. .owner = THIS_MODULE,
  168. },
  169. .probe = c_can_plat_probe,
  170. .remove = __devexit_p(c_can_plat_remove),
  171. };
  172. static int __init c_can_plat_init(void)
  173. {
  174. return platform_driver_register(&c_can_plat_driver);
  175. }
  176. module_init(c_can_plat_init);
  177. static void __exit c_can_plat_exit(void)
  178. {
  179. platform_driver_unregister(&c_can_plat_driver);
  180. }
  181. module_exit(c_can_plat_exit);
  182. MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
  183. MODULE_LICENSE("GPL v2");
  184. MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");