c_can_pci.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * PCI bus driver for Bosch C_CAN/D_CAN controller
  3. *
  4. * Copyright (C) 2012 Federico Vaga <federico.vaga@gmail.com>
  5. *
  6. * Borrowed from c_can_platform.c
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/clk.h>
  16. #include <linux/pci.h>
  17. #include <linux/can/dev.h>
  18. #include "c_can.h"
  19. enum c_can_pci_reg_align {
  20. C_CAN_REG_ALIGN_16,
  21. C_CAN_REG_ALIGN_32,
  22. };
  23. struct c_can_pci_data {
  24. /* Specify if is C_CAN or D_CAN */
  25. enum c_can_dev_id type;
  26. /* Set the register alignment in the memory */
  27. enum c_can_pci_reg_align reg_align;
  28. /* Set the frequency if clk is not usable */
  29. unsigned int freq;
  30. };
  31. /*
  32. * 16-bit c_can registers can be arranged differently in the memory
  33. * architecture of different implementations. For example: 16-bit
  34. * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
  35. * Handle the same by providing a common read/write interface.
  36. */
  37. static u16 c_can_pci_read_reg_aligned_to_16bit(struct c_can_priv *priv,
  38. enum reg index)
  39. {
  40. return readw(priv->base + priv->regs[index]);
  41. }
  42. static void c_can_pci_write_reg_aligned_to_16bit(struct c_can_priv *priv,
  43. enum reg index, u16 val)
  44. {
  45. writew(val, priv->base + priv->regs[index]);
  46. }
  47. static u16 c_can_pci_read_reg_aligned_to_32bit(struct c_can_priv *priv,
  48. enum reg index)
  49. {
  50. return readw(priv->base + 2 * priv->regs[index]);
  51. }
  52. static void c_can_pci_write_reg_aligned_to_32bit(struct c_can_priv *priv,
  53. enum reg index, u16 val)
  54. {
  55. writew(val, priv->base + 2 * priv->regs[index]);
  56. }
  57. static int __devinit c_can_pci_probe(struct pci_dev *pdev,
  58. const struct pci_device_id *ent)
  59. {
  60. struct c_can_pci_data *c_can_pci_data = (void *)ent->driver_data;
  61. struct c_can_priv *priv;
  62. struct net_device *dev;
  63. void __iomem *addr;
  64. struct clk *clk;
  65. int ret;
  66. ret = pci_enable_device(pdev);
  67. if (ret) {
  68. dev_err(&pdev->dev, "pci_enable_device FAILED\n");
  69. goto out;
  70. }
  71. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  72. if (ret) {
  73. dev_err(&pdev->dev, "pci_request_regions FAILED\n");
  74. goto out_disable_device;
  75. }
  76. pci_set_master(pdev);
  77. pci_enable_msi(pdev);
  78. addr = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
  79. if (!addr) {
  80. dev_err(&pdev->dev,
  81. "device has no PCI memory resources, "
  82. "failing adapter\n");
  83. ret = -ENOMEM;
  84. goto out_release_regions;
  85. }
  86. /* allocate the c_can device */
  87. dev = alloc_c_can_dev();
  88. if (!dev) {
  89. ret = -ENOMEM;
  90. goto out_iounmap;
  91. }
  92. priv = netdev_priv(dev);
  93. pci_set_drvdata(pdev, dev);
  94. SET_NETDEV_DEV(dev, &pdev->dev);
  95. dev->irq = pdev->irq;
  96. priv->base = addr;
  97. if (!c_can_pci_data->freq) {
  98. /* get the appropriate clk */
  99. clk = clk_get(&pdev->dev, NULL);
  100. if (IS_ERR(clk)) {
  101. dev_err(&pdev->dev, "no clock defined\n");
  102. ret = -ENODEV;
  103. goto out_free_c_can;
  104. }
  105. priv->can.clock.freq = clk_get_rate(clk);
  106. priv->priv = clk;
  107. } else {
  108. priv->can.clock.freq = c_can_pci_data->freq;
  109. priv->priv = NULL;
  110. }
  111. /* Configure CAN type */
  112. switch (c_can_pci_data->type) {
  113. case C_CAN_DEVTYPE:
  114. priv->regs = reg_map_c_can;
  115. break;
  116. case D_CAN_DEVTYPE:
  117. priv->regs = reg_map_d_can;
  118. priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  119. break;
  120. default:
  121. ret = -EINVAL;
  122. goto out_free_clock;
  123. }
  124. /* Configure access to registers */
  125. switch (c_can_pci_data->reg_align) {
  126. case C_CAN_REG_ALIGN_32:
  127. priv->read_reg = c_can_pci_read_reg_aligned_to_32bit;
  128. priv->write_reg = c_can_pci_write_reg_aligned_to_32bit;
  129. break;
  130. case C_CAN_REG_ALIGN_16:
  131. priv->read_reg = c_can_pci_read_reg_aligned_to_16bit;
  132. priv->write_reg = c_can_pci_write_reg_aligned_to_16bit;
  133. break;
  134. default:
  135. ret = -EINVAL;
  136. goto out_free_clock;
  137. }
  138. ret = register_c_can_dev(dev);
  139. if (ret) {
  140. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  141. KBUILD_MODNAME, ret);
  142. goto out_free_clock;
  143. }
  144. dev_dbg(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
  145. KBUILD_MODNAME, priv->regs, dev->irq);
  146. return 0;
  147. out_free_clock:
  148. if (priv->priv)
  149. clk_put(priv->priv);
  150. out_free_c_can:
  151. pci_set_drvdata(pdev, NULL);
  152. free_c_can_dev(dev);
  153. out_iounmap:
  154. pci_iounmap(pdev, addr);
  155. out_release_regions:
  156. pci_disable_msi(pdev);
  157. pci_clear_master(pdev);
  158. pci_release_regions(pdev);
  159. out_disable_device:
  160. pci_disable_device(pdev);
  161. out:
  162. return ret;
  163. }
  164. static void __devexit c_can_pci_remove(struct pci_dev *pdev)
  165. {
  166. struct net_device *dev = pci_get_drvdata(pdev);
  167. struct c_can_priv *priv = netdev_priv(dev);
  168. unregister_c_can_dev(dev);
  169. if (priv->priv)
  170. clk_put(priv->priv);
  171. pci_set_drvdata(pdev, NULL);
  172. free_c_can_dev(dev);
  173. pci_iounmap(pdev, priv->base);
  174. pci_disable_msi(pdev);
  175. pci_clear_master(pdev);
  176. pci_release_regions(pdev);
  177. pci_disable_device(pdev);
  178. }
  179. static struct c_can_pci_data c_can_sta2x11= {
  180. .type = C_CAN_DEVTYPE,
  181. .reg_align = C_CAN_REG_ALIGN_32,
  182. .freq = 52000000, /* 52 Mhz */
  183. };
  184. #define C_CAN_ID(_vend, _dev, _driverdata) { \
  185. PCI_DEVICE(_vend, _dev), \
  186. .driver_data = (unsigned long)&_driverdata, \
  187. }
  188. static DEFINE_PCI_DEVICE_TABLE(c_can_pci_tbl) = {
  189. C_CAN_ID(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_CAN,
  190. c_can_sta2x11),
  191. {},
  192. };
  193. static struct pci_driver c_can_pci_driver = {
  194. .name = KBUILD_MODNAME,
  195. .id_table = c_can_pci_tbl,
  196. .probe = c_can_pci_probe,
  197. .remove = __devexit_p(c_can_pci_remove),
  198. };
  199. module_pci_driver(c_can_pci_driver);
  200. MODULE_AUTHOR("Federico Vaga <federico.vaga@gmail.com>");
  201. MODULE_LICENSE("GPL v2");
  202. MODULE_DESCRIPTION("PCI CAN bus driver for Bosch C_CAN/D_CAN controller");
  203. MODULE_DEVICE_TABLE(pci, c_can_pci_tbl);