c_can_pci.c 6.9 KB

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