ems_pci.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007 Wolfgang Grandegger <[email protected]>
  4. * Copyright (C) 2008 Markus Plessing <[email protected]>
  5. * Copyright (C) 2008 Sebastian Haas <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/pci.h>
  14. #include <linux/can/dev.h>
  15. #include <linux/io.h>
  16. #include "sja1000.h"
  17. #define DRV_NAME "ems_pci"
  18. MODULE_AUTHOR("Sebastian Haas <[email protected]>");
  19. MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-PCI/PCIe/104P CAN cards");
  20. MODULE_LICENSE("GPL v2");
  21. #define EMS_PCI_V1_MAX_CHAN 2
  22. #define EMS_PCI_V2_MAX_CHAN 4
  23. #define EMS_PCI_MAX_CHAN EMS_PCI_V2_MAX_CHAN
  24. struct ems_pci_card {
  25. int version;
  26. int channels;
  27. struct pci_dev *pci_dev;
  28. struct net_device *net_dev[EMS_PCI_MAX_CHAN];
  29. void __iomem *conf_addr;
  30. void __iomem *base_addr;
  31. };
  32. #define EMS_PCI_CAN_CLOCK (16000000 / 2)
  33. /*
  34. * Register definitions and descriptions are from LinCAN 0.3.3.
  35. *
  36. * PSB4610 PITA-2 bridge control registers
  37. */
  38. #define PITA2_ICR 0x00 /* Interrupt Control Register */
  39. #define PITA2_ICR_INT0 0x00000002 /* [RC] INT0 Active/Clear */
  40. #define PITA2_ICR_INT0_EN 0x00020000 /* [RW] Enable INT0 */
  41. #define PITA2_MISC 0x1c /* Miscellaneous Register */
  42. #define PITA2_MISC_CONFIG 0x04000000 /* Multiplexed parallel interface */
  43. /*
  44. * Register definitions for the PLX 9030
  45. */
  46. #define PLX_ICSR 0x4c /* Interrupt Control/Status register */
  47. #define PLX_ICSR_LINTI1_ENA 0x0001 /* LINTi1 Enable */
  48. #define PLX_ICSR_PCIINT_ENA 0x0040 /* PCI Interrupt Enable */
  49. #define PLX_ICSR_LINTI1_CLR 0x0400 /* Local Edge Triggerable Interrupt Clear */
  50. #define PLX_ICSR_ENA_CLR (PLX_ICSR_LINTI1_ENA | PLX_ICSR_PCIINT_ENA | \
  51. PLX_ICSR_LINTI1_CLR)
  52. /*
  53. * The board configuration is probably following:
  54. * RX1 is connected to ground.
  55. * TX1 is not connected.
  56. * CLKO is not connected.
  57. * Setting the OCR register to 0xDA is a good idea.
  58. * This means normal output mode, push-pull and the correct polarity.
  59. */
  60. #define EMS_PCI_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
  61. /*
  62. * In the CDR register, you should set CBP to 1.
  63. * You will probably also want to set the clock divider value to 7
  64. * (meaning direct oscillator output) because the second SJA1000 chip
  65. * is driven by the first one CLKOUT output.
  66. */
  67. #define EMS_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  68. #define EMS_PCI_V1_BASE_BAR 1
  69. #define EMS_PCI_V1_CONF_SIZE 4096 /* size of PITA control area */
  70. #define EMS_PCI_V2_BASE_BAR 2
  71. #define EMS_PCI_V2_CONF_SIZE 128 /* size of PLX control area */
  72. #define EMS_PCI_CAN_BASE_OFFSET 0x400 /* offset where the controllers starts */
  73. #define EMS_PCI_CAN_CTRL_SIZE 0x200 /* memory size for each controller */
  74. #define EMS_PCI_BASE_SIZE 4096 /* size of controller area */
  75. static const struct pci_device_id ems_pci_tbl[] = {
  76. /* CPC-PCI v1 */
  77. {PCI_VENDOR_ID_SIEMENS, 0x2104, PCI_ANY_ID, PCI_ANY_ID,},
  78. /* CPC-PCI v2 */
  79. {PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4000},
  80. /* CPC-104P v2 */
  81. {PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_9030, PCI_VENDOR_ID_PLX, 0x4002},
  82. {0,}
  83. };
  84. MODULE_DEVICE_TABLE(pci, ems_pci_tbl);
  85. /*
  86. * Helper to read internal registers from card logic (not CAN)
  87. */
  88. static u8 ems_pci_v1_readb(struct ems_pci_card *card, unsigned int port)
  89. {
  90. return readb(card->base_addr + (port * 4));
  91. }
  92. static u8 ems_pci_v1_read_reg(const struct sja1000_priv *priv, int port)
  93. {
  94. return readb(priv->reg_base + (port * 4));
  95. }
  96. static void ems_pci_v1_write_reg(const struct sja1000_priv *priv,
  97. int port, u8 val)
  98. {
  99. writeb(val, priv->reg_base + (port * 4));
  100. }
  101. static void ems_pci_v1_post_irq(const struct sja1000_priv *priv)
  102. {
  103. struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
  104. /* reset int flag of pita */
  105. writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
  106. card->conf_addr + PITA2_ICR);
  107. }
  108. static u8 ems_pci_v2_read_reg(const struct sja1000_priv *priv, int port)
  109. {
  110. return readb(priv->reg_base + port);
  111. }
  112. static void ems_pci_v2_write_reg(const struct sja1000_priv *priv,
  113. int port, u8 val)
  114. {
  115. writeb(val, priv->reg_base + port);
  116. }
  117. static void ems_pci_v2_post_irq(const struct sja1000_priv *priv)
  118. {
  119. struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
  120. writel(PLX_ICSR_ENA_CLR, card->conf_addr + PLX_ICSR);
  121. }
  122. /*
  123. * Check if a CAN controller is present at the specified location
  124. * by trying to set 'em into the PeliCAN mode
  125. */
  126. static inline int ems_pci_check_chan(const struct sja1000_priv *priv)
  127. {
  128. unsigned char res;
  129. /* Make sure SJA1000 is in reset mode */
  130. priv->write_reg(priv, SJA1000_MOD, 1);
  131. priv->write_reg(priv, SJA1000_CDR, CDR_PELICAN);
  132. /* read reset-values */
  133. res = priv->read_reg(priv, SJA1000_CDR);
  134. if (res == CDR_PELICAN)
  135. return 1;
  136. return 0;
  137. }
  138. static void ems_pci_del_card(struct pci_dev *pdev)
  139. {
  140. struct ems_pci_card *card = pci_get_drvdata(pdev);
  141. struct net_device *dev;
  142. int i = 0;
  143. for (i = 0; i < card->channels; i++) {
  144. dev = card->net_dev[i];
  145. if (!dev)
  146. continue;
  147. dev_info(&pdev->dev, "Removing %s.\n", dev->name);
  148. unregister_sja1000dev(dev);
  149. free_sja1000dev(dev);
  150. }
  151. if (card->base_addr != NULL)
  152. pci_iounmap(card->pci_dev, card->base_addr);
  153. if (card->conf_addr != NULL)
  154. pci_iounmap(card->pci_dev, card->conf_addr);
  155. kfree(card);
  156. pci_disable_device(pdev);
  157. }
  158. static void ems_pci_card_reset(struct ems_pci_card *card)
  159. {
  160. /* Request board reset */
  161. writeb(0, card->base_addr);
  162. }
  163. /*
  164. * Probe PCI device for EMS CAN signature and register each available
  165. * CAN channel to SJA1000 Socket-CAN subsystem.
  166. */
  167. static int ems_pci_add_card(struct pci_dev *pdev,
  168. const struct pci_device_id *ent)
  169. {
  170. struct sja1000_priv *priv;
  171. struct net_device *dev;
  172. struct ems_pci_card *card;
  173. int max_chan, conf_size, base_bar;
  174. int err, i;
  175. /* Enabling PCI device */
  176. if (pci_enable_device(pdev) < 0) {
  177. dev_err(&pdev->dev, "Enabling PCI device failed\n");
  178. return -ENODEV;
  179. }
  180. /* Allocating card structures to hold addresses, ... */
  181. card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL);
  182. if (card == NULL) {
  183. pci_disable_device(pdev);
  184. return -ENOMEM;
  185. }
  186. pci_set_drvdata(pdev, card);
  187. card->pci_dev = pdev;
  188. card->channels = 0;
  189. if (pdev->vendor == PCI_VENDOR_ID_PLX) {
  190. card->version = 2; /* CPC-PCI v2 */
  191. max_chan = EMS_PCI_V2_MAX_CHAN;
  192. base_bar = EMS_PCI_V2_BASE_BAR;
  193. conf_size = EMS_PCI_V2_CONF_SIZE;
  194. } else {
  195. card->version = 1; /* CPC-PCI v1 */
  196. max_chan = EMS_PCI_V1_MAX_CHAN;
  197. base_bar = EMS_PCI_V1_BASE_BAR;
  198. conf_size = EMS_PCI_V1_CONF_SIZE;
  199. }
  200. /* Remap configuration space and controller memory area */
  201. card->conf_addr = pci_iomap(pdev, 0, conf_size);
  202. if (card->conf_addr == NULL) {
  203. err = -ENOMEM;
  204. goto failure_cleanup;
  205. }
  206. card->base_addr = pci_iomap(pdev, base_bar, EMS_PCI_BASE_SIZE);
  207. if (card->base_addr == NULL) {
  208. err = -ENOMEM;
  209. goto failure_cleanup;
  210. }
  211. if (card->version == 1) {
  212. /* Configure PITA-2 parallel interface (enable MUX) */
  213. writel(PITA2_MISC_CONFIG, card->conf_addr + PITA2_MISC);
  214. /* Check for unique EMS CAN signature */
  215. if (ems_pci_v1_readb(card, 0) != 0x55 ||
  216. ems_pci_v1_readb(card, 1) != 0xAA ||
  217. ems_pci_v1_readb(card, 2) != 0x01 ||
  218. ems_pci_v1_readb(card, 3) != 0xCB ||
  219. ems_pci_v1_readb(card, 4) != 0x11) {
  220. dev_err(&pdev->dev,
  221. "Not EMS Dr. Thomas Wuensche interface\n");
  222. err = -ENODEV;
  223. goto failure_cleanup;
  224. }
  225. }
  226. ems_pci_card_reset(card);
  227. /* Detect available channels */
  228. for (i = 0; i < max_chan; i++) {
  229. dev = alloc_sja1000dev(0);
  230. if (dev == NULL) {
  231. err = -ENOMEM;
  232. goto failure_cleanup;
  233. }
  234. card->net_dev[i] = dev;
  235. priv = netdev_priv(dev);
  236. priv->priv = card;
  237. priv->irq_flags = IRQF_SHARED;
  238. dev->irq = pdev->irq;
  239. priv->reg_base = card->base_addr + EMS_PCI_CAN_BASE_OFFSET
  240. + (i * EMS_PCI_CAN_CTRL_SIZE);
  241. if (card->version == 1) {
  242. priv->read_reg = ems_pci_v1_read_reg;
  243. priv->write_reg = ems_pci_v1_write_reg;
  244. priv->post_irq = ems_pci_v1_post_irq;
  245. } else {
  246. priv->read_reg = ems_pci_v2_read_reg;
  247. priv->write_reg = ems_pci_v2_write_reg;
  248. priv->post_irq = ems_pci_v2_post_irq;
  249. }
  250. /* Check if channel is present */
  251. if (ems_pci_check_chan(priv)) {
  252. priv->can.clock.freq = EMS_PCI_CAN_CLOCK;
  253. priv->ocr = EMS_PCI_OCR;
  254. priv->cdr = EMS_PCI_CDR;
  255. SET_NETDEV_DEV(dev, &pdev->dev);
  256. dev->dev_id = i;
  257. if (card->version == 1)
  258. /* reset int flag of pita */
  259. writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0,
  260. card->conf_addr + PITA2_ICR);
  261. else
  262. /* enable IRQ in PLX 9030 */
  263. writel(PLX_ICSR_ENA_CLR,
  264. card->conf_addr + PLX_ICSR);
  265. /* Register SJA1000 device */
  266. err = register_sja1000dev(dev);
  267. if (err) {
  268. dev_err(&pdev->dev, "Registering device failed "
  269. "(err=%d)\n", err);
  270. free_sja1000dev(dev);
  271. goto failure_cleanup;
  272. }
  273. card->channels++;
  274. dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d\n",
  275. i + 1, priv->reg_base, dev->irq);
  276. } else {
  277. free_sja1000dev(dev);
  278. }
  279. }
  280. return 0;
  281. failure_cleanup:
  282. dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
  283. ems_pci_del_card(pdev);
  284. return err;
  285. }
  286. static struct pci_driver ems_pci_driver = {
  287. .name = DRV_NAME,
  288. .id_table = ems_pci_tbl,
  289. .probe = ems_pci_add_card,
  290. .remove = ems_pci_del_card,
  291. };
  292. module_pci_driver(ems_pci_driver);