c_can_pci.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. pci_set_master(pdev);
  75. pci_enable_msi(pdev);
  76. addr = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
  77. if (!addr) {
  78. dev_err(&pdev->dev,
  79. "device has no PCI memory resources, "
  80. "failing adapter\n");
  81. ret = -ENOMEM;
  82. goto out_release_regions;
  83. }
  84. /* allocate the c_can device */
  85. dev = alloc_c_can_dev();
  86. if (!dev) {
  87. ret = -ENOMEM;
  88. goto out_iounmap;
  89. }
  90. priv = netdev_priv(dev);
  91. pci_set_drvdata(pdev, dev);
  92. SET_NETDEV_DEV(dev, &pdev->dev);
  93. dev->irq = pdev->irq;
  94. priv->base = addr;
  95. if (!c_can_pci_data->freq) {
  96. dev_err(&pdev->dev, "no clock frequency defined\n");
  97. ret = -ENODEV;
  98. goto out_free_c_can;
  99. } else {
  100. priv->can.clock.freq = c_can_pci_data->freq;
  101. }
  102. /* Configure CAN type */
  103. switch (c_can_pci_data->type) {
  104. case BOSCH_C_CAN:
  105. priv->regs = reg_map_c_can;
  106. break;
  107. case BOSCH_D_CAN:
  108. priv->regs = reg_map_d_can;
  109. priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  110. break;
  111. default:
  112. ret = -EINVAL;
  113. goto out_free_c_can;
  114. }
  115. /* Configure access to registers */
  116. switch (c_can_pci_data->reg_align) {
  117. case C_CAN_REG_ALIGN_32:
  118. priv->read_reg = c_can_pci_read_reg_aligned_to_32bit;
  119. priv->write_reg = c_can_pci_write_reg_aligned_to_32bit;
  120. break;
  121. case C_CAN_REG_ALIGN_16:
  122. priv->read_reg = c_can_pci_read_reg_aligned_to_16bit;
  123. priv->write_reg = c_can_pci_write_reg_aligned_to_16bit;
  124. break;
  125. default:
  126. ret = -EINVAL;
  127. goto out_free_c_can;
  128. }
  129. ret = register_c_can_dev(dev);
  130. if (ret) {
  131. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  132. KBUILD_MODNAME, ret);
  133. goto out_free_c_can;
  134. }
  135. dev_dbg(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
  136. KBUILD_MODNAME, priv->regs, dev->irq);
  137. return 0;
  138. out_free_c_can:
  139. pci_set_drvdata(pdev, NULL);
  140. free_c_can_dev(dev);
  141. out_iounmap:
  142. pci_iounmap(pdev, addr);
  143. out_release_regions:
  144. pci_disable_msi(pdev);
  145. pci_clear_master(pdev);
  146. pci_release_regions(pdev);
  147. out_disable_device:
  148. pci_disable_device(pdev);
  149. out:
  150. return ret;
  151. }
  152. static void c_can_pci_remove(struct pci_dev *pdev)
  153. {
  154. struct net_device *dev = pci_get_drvdata(pdev);
  155. struct c_can_priv *priv = netdev_priv(dev);
  156. unregister_c_can_dev(dev);
  157. pci_set_drvdata(pdev, NULL);
  158. free_c_can_dev(dev);
  159. pci_iounmap(pdev, priv->base);
  160. pci_disable_msi(pdev);
  161. pci_clear_master(pdev);
  162. pci_release_regions(pdev);
  163. pci_disable_device(pdev);
  164. }
  165. static struct c_can_pci_data c_can_sta2x11= {
  166. .type = BOSCH_C_CAN,
  167. .reg_align = C_CAN_REG_ALIGN_32,
  168. .freq = 52000000, /* 52 Mhz */
  169. };
  170. #define C_CAN_ID(_vend, _dev, _driverdata) { \
  171. PCI_DEVICE(_vend, _dev), \
  172. .driver_data = (unsigned long)&_driverdata, \
  173. }
  174. static DEFINE_PCI_DEVICE_TABLE(c_can_pci_tbl) = {
  175. C_CAN_ID(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_CAN,
  176. c_can_sta2x11),
  177. {},
  178. };
  179. static struct pci_driver c_can_pci_driver = {
  180. .name = KBUILD_MODNAME,
  181. .id_table = c_can_pci_tbl,
  182. .probe = c_can_pci_probe,
  183. .remove = c_can_pci_remove,
  184. };
  185. module_pci_driver(c_can_pci_driver);
  186. MODULE_AUTHOR("Federico Vaga <federico.vaga@gmail.com>");
  187. MODULE_LICENSE("GPL v2");
  188. MODULE_DESCRIPTION("PCI CAN bus driver for Bosch C_CAN/D_CAN controller");
  189. MODULE_DEVICE_TABLE(pci, c_can_pci_tbl);