c_can_pci.c 5.2 KB

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