kvaser_pci.c 10 KB

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