kvaser_pci.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2008 Per Dalen <per.dalen@cnw.se>
  4. *
  5. * Parts of this software are based on (derived) the following:
  6. *
  7. * - Kvaser linux driver, version 4.72 BETA
  8. * Copyright (C) 2002-2007 KVASER AB
  9. *
  10. * - Lincan driver, version 0.3.3, OCERA project
  11. * Copyright (C) 2004 Pavel Pisa
  12. * Copyright (C) 2001 Arnaud Westenberg
  13. *
  14. * - Socketcan SJA1000 drivers
  15. * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  16. * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
  17. * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
  18. * 38106 Braunschweig, GERMANY
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/delay.h>
  25. #include <linux/pci.h>
  26. #include <linux/can/dev.h>
  27. #include <linux/io.h>
  28. #include "sja1000.h"
  29. #define DRV_NAME "kvaser_pci"
  30. MODULE_AUTHOR("Per Dalen <per.dalen@cnw.se>");
  31. MODULE_DESCRIPTION("Socket-CAN driver for KVASER PCAN PCI cards");
  32. MODULE_SUPPORTED_DEVICE("KVASER PCAN PCI CAN card");
  33. MODULE_LICENSE("GPL v2");
  34. #define MAX_NO_OF_CHANNELS 4 /* max no of channels on a single card */
  35. struct kvaser_pci {
  36. int channel;
  37. struct pci_dev *pci_dev;
  38. struct net_device *slave_dev[MAX_NO_OF_CHANNELS-1];
  39. void __iomem *conf_addr;
  40. void __iomem *res_addr;
  41. int no_channels;
  42. u8 xilinx_ver;
  43. };
  44. #define KVASER_PCI_CAN_CLOCK (16000000 / 2)
  45. /*
  46. * The board configuration is probably following:
  47. * RX1 is connected to ground.
  48. * TX1 is not connected.
  49. * CLKO is not connected.
  50. * Setting the OCR register to 0xDA is a good idea.
  51. * This means normal output mode , push-pull and the correct polarity.
  52. */
  53. #define KVASER_PCI_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
  54. /*
  55. * In the CDR register, you should set CBP to 1.
  56. * You will probably also want to set the clock divider value to 0
  57. * (meaning divide-by-2), the Pelican bit, and the clock-off bit
  58. * (you will have no need for CLKOUT anyway).
  59. */
  60. #define KVASER_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  61. /*
  62. * These register values are valid for revision 14 of the Xilinx logic.
  63. */
  64. #define XILINX_VERINT 7 /* Lower nibble simulate interrupts,
  65. high nibble version number. */
  66. #define XILINX_PRESUMED_VERSION 14
  67. /*
  68. * Important S5920 registers
  69. */
  70. #define S5920_INTCSR 0x38
  71. #define S5920_PTCR 0x60
  72. #define INTCSR_ADDON_INTENABLE_M 0x2000
  73. #define KVASER_PCI_PORT_BYTES 0x20
  74. #define PCI_CONFIG_PORT_SIZE 0x80 /* size of the config io-memory */
  75. #define PCI_PORT_SIZE 0x80 /* size of a channel io-memory */
  76. #define PCI_PORT_XILINX_SIZE 0x08 /* size of a xilinx io-memory */
  77. #define KVASER_PCI_VENDOR_ID1 0x10e8 /* the PCI device and vendor IDs */
  78. #define KVASER_PCI_DEVICE_ID1 0x8406
  79. #define KVASER_PCI_VENDOR_ID2 0x1a07 /* the PCI device and vendor IDs */
  80. #define KVASER_PCI_DEVICE_ID2 0x0008
  81. static const struct pci_device_id kvaser_pci_tbl[] = {
  82. {KVASER_PCI_VENDOR_ID1, KVASER_PCI_DEVICE_ID1, PCI_ANY_ID, PCI_ANY_ID,},
  83. {KVASER_PCI_VENDOR_ID2, KVASER_PCI_DEVICE_ID2, PCI_ANY_ID, PCI_ANY_ID,},
  84. { 0,}
  85. };
  86. MODULE_DEVICE_TABLE(pci, kvaser_pci_tbl);
  87. static u8 kvaser_pci_read_reg(const struct sja1000_priv *priv, int port)
  88. {
  89. return ioread8(priv->reg_base + port);
  90. }
  91. static void kvaser_pci_write_reg(const struct sja1000_priv *priv,
  92. int port, u8 val)
  93. {
  94. iowrite8(val, priv->reg_base + port);
  95. }
  96. static void kvaser_pci_disable_irq(struct net_device *dev)
  97. {
  98. struct sja1000_priv *priv = netdev_priv(dev);
  99. struct kvaser_pci *board = priv->priv;
  100. u32 intcsr;
  101. /* Disable interrupts from card */
  102. intcsr = ioread32(board->conf_addr + S5920_INTCSR);
  103. intcsr &= ~INTCSR_ADDON_INTENABLE_M;
  104. iowrite32(intcsr, board->conf_addr + S5920_INTCSR);
  105. }
  106. static void kvaser_pci_enable_irq(struct net_device *dev)
  107. {
  108. struct sja1000_priv *priv = netdev_priv(dev);
  109. struct kvaser_pci *board = priv->priv;
  110. u32 tmp_en_io;
  111. /* Enable interrupts from card */
  112. tmp_en_io = ioread32(board->conf_addr + S5920_INTCSR);
  113. tmp_en_io |= INTCSR_ADDON_INTENABLE_M;
  114. iowrite32(tmp_en_io, board->conf_addr + S5920_INTCSR);
  115. }
  116. static int number_of_sja1000_chip(void __iomem *base_addr)
  117. {
  118. u8 status;
  119. int i;
  120. for (i = 0; i < MAX_NO_OF_CHANNELS; i++) {
  121. /* reset chip */
  122. iowrite8(MOD_RM, base_addr +
  123. (i * KVASER_PCI_PORT_BYTES) + SJA1000_MOD);
  124. status = ioread8(base_addr +
  125. (i * KVASER_PCI_PORT_BYTES) + SJA1000_MOD);
  126. /* check reset bit */
  127. if (!(status & MOD_RM))
  128. break;
  129. }
  130. return i;
  131. }
  132. static void kvaser_pci_del_chan(struct net_device *dev)
  133. {
  134. struct sja1000_priv *priv;
  135. struct kvaser_pci *board;
  136. int i;
  137. if (!dev)
  138. return;
  139. priv = netdev_priv(dev);
  140. board = priv->priv;
  141. if (!board)
  142. return;
  143. dev_info(&board->pci_dev->dev, "Removing device %s\n",
  144. dev->name);
  145. /* Disable PCI interrupts */
  146. kvaser_pci_disable_irq(dev);
  147. for (i = 0; i < board->no_channels - 1; i++) {
  148. if (board->slave_dev[i]) {
  149. dev_info(&board->pci_dev->dev, "Removing device %s\n",
  150. board->slave_dev[i]->name);
  151. unregister_sja1000dev(board->slave_dev[i]);
  152. free_sja1000dev(board->slave_dev[i]);
  153. }
  154. }
  155. unregister_sja1000dev(dev);
  156. pci_iounmap(board->pci_dev, priv->reg_base);
  157. pci_iounmap(board->pci_dev, board->conf_addr);
  158. pci_iounmap(board->pci_dev, board->res_addr);
  159. free_sja1000dev(dev);
  160. }
  161. static int kvaser_pci_add_chan(struct pci_dev *pdev, int channel,
  162. struct net_device **master_dev,
  163. void __iomem *conf_addr,
  164. void __iomem *res_addr,
  165. void __iomem *base_addr)
  166. {
  167. struct net_device *dev;
  168. struct sja1000_priv *priv;
  169. struct kvaser_pci *board;
  170. int err;
  171. dev = alloc_sja1000dev(sizeof(struct kvaser_pci));
  172. if (dev == NULL)
  173. return -ENOMEM;
  174. priv = netdev_priv(dev);
  175. board = priv->priv;
  176. board->pci_dev = pdev;
  177. board->channel = channel;
  178. /* S5920 */
  179. board->conf_addr = conf_addr;
  180. /* XILINX board wide address */
  181. board->res_addr = res_addr;
  182. if (channel == 0) {
  183. board->xilinx_ver =
  184. ioread8(board->res_addr + XILINX_VERINT) >> 4;
  185. /* Assert PTADR# - we're in passive mode so the other bits are
  186. not important */
  187. iowrite32(0x80808080UL, board->conf_addr + S5920_PTCR);
  188. /* Enable interrupts from card */
  189. kvaser_pci_enable_irq(dev);
  190. } else {
  191. struct sja1000_priv *master_priv = netdev_priv(*master_dev);
  192. struct kvaser_pci *master_board = master_priv->priv;
  193. master_board->slave_dev[channel - 1] = dev;
  194. master_board->no_channels = channel + 1;
  195. board->xilinx_ver = master_board->xilinx_ver;
  196. }
  197. priv->reg_base = base_addr + channel * KVASER_PCI_PORT_BYTES;
  198. priv->read_reg = kvaser_pci_read_reg;
  199. priv->write_reg = kvaser_pci_write_reg;
  200. priv->can.clock.freq = KVASER_PCI_CAN_CLOCK;
  201. priv->ocr = KVASER_PCI_OCR;
  202. priv->cdr = KVASER_PCI_CDR;
  203. priv->irq_flags = IRQF_SHARED;
  204. dev->irq = pdev->irq;
  205. dev_info(&pdev->dev, "reg_base=%p conf_addr=%p irq=%d\n",
  206. priv->reg_base, board->conf_addr, dev->irq);
  207. SET_NETDEV_DEV(dev, &pdev->dev);
  208. dev->dev_id = channel;
  209. /* Register SJA1000 device */
  210. err = register_sja1000dev(dev);
  211. if (err) {
  212. dev_err(&pdev->dev, "Registering device failed (err=%d)\n",
  213. err);
  214. goto failure;
  215. }
  216. if (channel == 0)
  217. *master_dev = dev;
  218. return 0;
  219. failure:
  220. kvaser_pci_del_chan(dev);
  221. return err;
  222. }
  223. static int kvaser_pci_init_one(struct pci_dev *pdev,
  224. const struct pci_device_id *ent)
  225. {
  226. int err;
  227. struct net_device *master_dev = NULL;
  228. struct sja1000_priv *priv;
  229. struct kvaser_pci *board;
  230. int no_channels;
  231. void __iomem *base_addr = NULL;
  232. void __iomem *conf_addr = NULL;
  233. void __iomem *res_addr = NULL;
  234. int i;
  235. dev_info(&pdev->dev, "initializing device %04x:%04x\n",
  236. pdev->vendor, pdev->device);
  237. err = pci_enable_device(pdev);
  238. if (err)
  239. goto failure;
  240. err = pci_request_regions(pdev, DRV_NAME);
  241. if (err)
  242. goto failure_release_pci;
  243. /* S5920 */
  244. conf_addr = pci_iomap(pdev, 0, PCI_CONFIG_PORT_SIZE);
  245. if (conf_addr == NULL) {
  246. err = -ENODEV;
  247. goto failure_release_regions;
  248. }
  249. /* XILINX board wide address */
  250. res_addr = pci_iomap(pdev, 2, PCI_PORT_XILINX_SIZE);
  251. if (res_addr == NULL) {
  252. err = -ENOMEM;
  253. goto failure_iounmap;
  254. }
  255. base_addr = pci_iomap(pdev, 1, PCI_PORT_SIZE);
  256. if (base_addr == NULL) {
  257. err = -ENOMEM;
  258. goto failure_iounmap;
  259. }
  260. no_channels = number_of_sja1000_chip(base_addr);
  261. if (no_channels == 0) {
  262. err = -ENOMEM;
  263. goto failure_iounmap;
  264. }
  265. for (i = 0; i < no_channels; i++) {
  266. err = kvaser_pci_add_chan(pdev, i, &master_dev,
  267. conf_addr, res_addr,
  268. base_addr);
  269. if (err)
  270. goto failure_cleanup;
  271. }
  272. priv = netdev_priv(master_dev);
  273. board = priv->priv;
  274. dev_info(&pdev->dev, "xilinx version=%d number of channels=%d\n",
  275. board->xilinx_ver, board->no_channels);
  276. pci_set_drvdata(pdev, master_dev);
  277. return 0;
  278. failure_cleanup:
  279. kvaser_pci_del_chan(master_dev);
  280. failure_iounmap:
  281. if (conf_addr != NULL)
  282. pci_iounmap(pdev, conf_addr);
  283. if (res_addr != NULL)
  284. pci_iounmap(pdev, res_addr);
  285. if (base_addr != NULL)
  286. pci_iounmap(pdev, base_addr);
  287. failure_release_regions:
  288. pci_release_regions(pdev);
  289. failure_release_pci:
  290. pci_disable_device(pdev);
  291. failure:
  292. return err;
  293. }
  294. static void kvaser_pci_remove_one(struct pci_dev *pdev)
  295. {
  296. struct net_device *dev = pci_get_drvdata(pdev);
  297. kvaser_pci_del_chan(dev);
  298. pci_release_regions(pdev);
  299. pci_disable_device(pdev);
  300. }
  301. static struct pci_driver kvaser_pci_driver = {
  302. .name = DRV_NAME,
  303. .id_table = kvaser_pci_tbl,
  304. .probe = kvaser_pci_init_one,
  305. .remove = kvaser_pci_remove_one,
  306. };
  307. module_pci_driver(kvaser_pci_driver);