ems_pci.c 9.8 KB

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