ems_pci.c 12 KB

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