c_can_pci.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. /* Number of message objects */
  29. unsigned int msg_obj_num;
  30. /* Set the register alignment in the memory */
  31. enum c_can_pci_reg_align reg_align;
  32. /* Set the frequency */
  33. unsigned int freq;
  34. /* PCI bar number */
  35. int bar;
  36. /* Callback for reset */
  37. void (*init)(const struct c_can_priv *priv, bool enable);
  38. };
  39. /* 16-bit c_can registers can be arranged differently in the memory
  40. * architecture of different implementations. For example: 16-bit
  41. * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
  42. * Handle the same by providing a common read/write interface.
  43. */
  44. static u16 c_can_pci_read_reg_aligned_to_16bit(const struct c_can_priv *priv,
  45. enum reg index)
  46. {
  47. return readw(priv->base + priv->regs[index]);
  48. }
  49. static void c_can_pci_write_reg_aligned_to_16bit(const struct c_can_priv *priv,
  50. enum reg index, u16 val)
  51. {
  52. writew(val, priv->base + priv->regs[index]);
  53. }
  54. static u16 c_can_pci_read_reg_aligned_to_32bit(const struct c_can_priv *priv,
  55. enum reg index)
  56. {
  57. return readw(priv->base + 2 * priv->regs[index]);
  58. }
  59. static void c_can_pci_write_reg_aligned_to_32bit(const struct c_can_priv *priv,
  60. enum reg index, u16 val)
  61. {
  62. writew(val, priv->base + 2 * priv->regs[index]);
  63. }
  64. static u16 c_can_pci_read_reg_32bit(const struct c_can_priv *priv,
  65. enum reg index)
  66. {
  67. return (u16)ioread32(priv->base + 2 * priv->regs[index]);
  68. }
  69. static void c_can_pci_write_reg_32bit(const struct c_can_priv *priv,
  70. enum reg index, u16 val)
  71. {
  72. iowrite32((u32)val, priv->base + 2 * priv->regs[index]);
  73. }
  74. static u32 c_can_pci_read_reg32(const struct c_can_priv *priv, enum reg index)
  75. {
  76. u32 val;
  77. val = priv->read_reg(priv, index);
  78. val |= ((u32)priv->read_reg(priv, index + 1)) << 16;
  79. return val;
  80. }
  81. static void c_can_pci_write_reg32(const struct c_can_priv *priv, enum reg index,
  82. u32 val)
  83. {
  84. priv->write_reg(priv, index + 1, val >> 16);
  85. priv->write_reg(priv, index, val);
  86. }
  87. static void c_can_pci_reset_pch(const struct c_can_priv *priv, bool enable)
  88. {
  89. if (enable) {
  90. u32 __iomem *addr = priv->base + PCH_PCI_SOFT_RESET;
  91. /* write to sw reset register */
  92. iowrite32(1, addr);
  93. iowrite32(0, addr);
  94. }
  95. }
  96. static int c_can_pci_probe(struct pci_dev *pdev,
  97. const struct pci_device_id *ent)
  98. {
  99. struct c_can_pci_data *c_can_pci_data = (void *)ent->driver_data;
  100. struct c_can_priv *priv;
  101. struct net_device *dev;
  102. void __iomem *addr;
  103. int ret;
  104. ret = pci_enable_device(pdev);
  105. if (ret) {
  106. dev_err(&pdev->dev, "pci_enable_device FAILED\n");
  107. goto out;
  108. }
  109. ret = pci_request_regions(pdev, KBUILD_MODNAME);
  110. if (ret) {
  111. dev_err(&pdev->dev, "pci_request_regions FAILED\n");
  112. goto out_disable_device;
  113. }
  114. ret = pci_enable_msi(pdev);
  115. if (!ret) {
  116. dev_info(&pdev->dev, "MSI enabled\n");
  117. pci_set_master(pdev);
  118. }
  119. addr = pci_iomap(pdev, c_can_pci_data->bar,
  120. pci_resource_len(pdev, c_can_pci_data->bar));
  121. if (!addr) {
  122. dev_err(&pdev->dev,
  123. "device has no PCI memory resources, failing adapter\n");
  124. ret = -ENOMEM;
  125. goto out_release_regions;
  126. }
  127. /* allocate the c_can device */
  128. dev = alloc_c_can_dev(c_can_pci_data->msg_obj_num);
  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_release_regions(pdev);
  196. out_disable_device:
  197. pci_disable_device(pdev);
  198. out:
  199. return ret;
  200. }
  201. static void c_can_pci_remove(struct pci_dev *pdev)
  202. {
  203. struct net_device *dev = pci_get_drvdata(pdev);
  204. struct c_can_priv *priv = netdev_priv(dev);
  205. void __iomem *addr = priv->base;
  206. unregister_c_can_dev(dev);
  207. free_c_can_dev(dev);
  208. pci_iounmap(pdev, addr);
  209. pci_disable_msi(pdev);
  210. pci_release_regions(pdev);
  211. pci_disable_device(pdev);
  212. }
  213. static const struct c_can_pci_data c_can_sta2x11 = {
  214. .type = BOSCH_C_CAN,
  215. .msg_obj_num = 32,
  216. .reg_align = C_CAN_REG_ALIGN_32,
  217. .freq = 52000000, /* 52 Mhz */
  218. .bar = 0,
  219. };
  220. static const struct c_can_pci_data c_can_pch = {
  221. .type = BOSCH_C_CAN,
  222. .msg_obj_num = 32,
  223. .reg_align = C_CAN_REG_32,
  224. .freq = 50000000, /* 50 MHz */
  225. .init = c_can_pci_reset_pch,
  226. .bar = 1,
  227. };
  228. #define C_CAN_ID(_vend, _dev, _driverdata) { \
  229. PCI_DEVICE(_vend, _dev), \
  230. .driver_data = (unsigned long)&(_driverdata), \
  231. }
  232. static const struct pci_device_id c_can_pci_tbl[] = {
  233. C_CAN_ID(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_CAN,
  234. c_can_sta2x11),
  235. C_CAN_ID(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PCH_CAN,
  236. c_can_pch),
  237. {},
  238. };
  239. static struct pci_driver c_can_pci_driver = {
  240. .name = KBUILD_MODNAME,
  241. .id_table = c_can_pci_tbl,
  242. .probe = c_can_pci_probe,
  243. .remove = c_can_pci_remove,
  244. };
  245. module_pci_driver(c_can_pci_driver);
  246. MODULE_AUTHOR("Federico Vaga <federico.vaga@gmail.com>");
  247. MODULE_LICENSE("GPL v2");
  248. MODULE_DESCRIPTION("PCI CAN bus driver for Bosch C_CAN/D_CAN controller");
  249. MODULE_DEVICE_TABLE(pci, c_can_pci_tbl);