ems_pci.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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, write to the Free Software Foundation,
  17. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/delay.h>
  24. #include <linux/pci.h>
  25. #include <linux/can.h>
  26. #include <linux/can/dev.h>
  27. #include <linux/io.h>
  28. #include "sja1000.h"
  29. #define DRV_NAME "ems_pci"
  30. MODULE_AUTHOR("Sebastian Haas <haas@ems-wuenche.com>");
  31. MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-PCI/PCIe CAN cards");
  32. MODULE_SUPPORTED_DEVICE("EMS CPC-PCI/PCIe CAN card");
  33. MODULE_LICENSE("GPL v2");
  34. #define EMS_PCI_MAX_CHAN 2
  35. struct ems_pci_card {
  36. int channels;
  37. struct pci_dev *pci_dev;
  38. struct net_device *net_dev[EMS_PCI_MAX_CHAN];
  39. void __iomem *conf_addr;
  40. void __iomem *base_addr;
  41. };
  42. #define EMS_PCI_CAN_CLOCK (16000000 / 2)
  43. /*
  44. * Register definitions and descriptions are from LinCAN 0.3.3.
  45. *
  46. * PSB4610 PITA-2 bridge control registers
  47. */
  48. #define PITA2_ICR 0x00 /* Interrupt Control Register */
  49. #define PITA2_ICR_INT0 0x00000002 /* [RC] INT0 Active/Clear */
  50. #define PITA2_ICR_INT0_EN 0x00020000 /* [RW] Enable INT0 */
  51. #define PITA2_MISC 0x1c /* Miscellaneous Register */
  52. #define PITA2_MISC_CONFIG 0x04000000 /* Multiplexed parallel interface */
  53. /*
  54. * The board configuration is probably following:
  55. * RX1 is connected to ground.
  56. * TX1 is not connected.
  57. * CLKO is not connected.
  58. * Setting the OCR register to 0xDA is a good idea.
  59. * This means normal output mode , push-pull and the correct polarity.
  60. */
  61. #define EMS_PCI_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
  62. /*
  63. * In the CDR register, you should set CBP to 1.
  64. * You will probably also want to set the clock divider value to 7
  65. * (meaning direct oscillator output) because the second SJA1000 chip
  66. * is driven by the first one CLKOUT output.
  67. */
  68. #define EMS_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  69. #define EMS_PCI_MEM_SIZE 4096 /* Size of the remapped io-memory */
  70. #define EMS_PCI_CAN_BASE_OFFSET 0x400 /* offset where the controllers starts */
  71. #define EMS_PCI_CAN_CTRL_SIZE 0x200 /* memory size for each controller */
  72. #define EMS_PCI_PORT_BYTES 0x4 /* Each register occupies 4 bytes */
  73. #define EMS_PCI_VENDOR_ID 0x110a /* PCI device and vendor ID */
  74. #define EMS_PCI_DEVICE_ID 0x2104
  75. static struct pci_device_id ems_pci_tbl[] = {
  76. {EMS_PCI_VENDOR_ID, EMS_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  77. {0,}
  78. };
  79. MODULE_DEVICE_TABLE(pci, ems_pci_tbl);
  80. /*
  81. * Helper to read internal registers from card logic (not CAN)
  82. */
  83. static u8 ems_pci_readb(struct ems_pci_card *card, unsigned int port)
  84. {
  85. return readb(card->base_addr + (port * EMS_PCI_PORT_BYTES));
  86. }
  87. static u8 ems_pci_read_reg(const struct sja1000_priv *priv, int port)
  88. {
  89. return readb(priv->reg_base + (port * EMS_PCI_PORT_BYTES));
  90. }
  91. static void ems_pci_write_reg(const struct sja1000_priv *priv, int port, u8 val)
  92. {
  93. writeb(val, priv->reg_base + (port * EMS_PCI_PORT_BYTES));
  94. }
  95. static void ems_pci_post_irq(const struct sja1000_priv *priv)
  96. {
  97. struct ems_pci_card *card = (struct ems_pci_card *)priv->priv;
  98. /* reset int flag of pita */
  99. writel(PITA2_ICR_INT0_EN | PITA2_ICR_INT0, card->conf_addr
  100. + PITA2_ICR);
  101. }
  102. /*
  103. * Check if a CAN controller is present at the specified location
  104. * by trying to set 'em into the PeliCAN mode
  105. */
  106. static inline int ems_pci_check_chan(const struct sja1000_priv *priv)
  107. {
  108. unsigned char res;
  109. /* Make sure SJA1000 is in reset mode */
  110. ems_pci_write_reg(priv, REG_MOD, 1);
  111. ems_pci_write_reg(priv, REG_CDR, CDR_PELICAN);
  112. /* read reset-values */
  113. res = ems_pci_read_reg(priv, REG_CDR);
  114. if (res == CDR_PELICAN)
  115. return 1;
  116. return 0;
  117. }
  118. static void ems_pci_del_card(struct pci_dev *pdev)
  119. {
  120. struct ems_pci_card *card = pci_get_drvdata(pdev);
  121. struct net_device *dev;
  122. int i = 0;
  123. for (i = 0; i < card->channels; i++) {
  124. dev = card->net_dev[i];
  125. if (!dev)
  126. continue;
  127. dev_info(&pdev->dev, "Removing %s.\n", dev->name);
  128. unregister_sja1000dev(dev);
  129. free_sja1000dev(dev);
  130. }
  131. if (card->base_addr != NULL)
  132. pci_iounmap(card->pci_dev, card->base_addr);
  133. if (card->conf_addr != NULL)
  134. pci_iounmap(card->pci_dev, card->conf_addr);
  135. kfree(card);
  136. pci_disable_device(pdev);
  137. pci_set_drvdata(pdev, NULL);
  138. }
  139. static void ems_pci_card_reset(struct ems_pci_card *card)
  140. {
  141. /* Request board reset */
  142. writeb(0, card->base_addr);
  143. }
  144. /*
  145. * Probe PCI device for EMS CAN signature and register each available
  146. * CAN channel to SJA1000 Socket-CAN subsystem.
  147. */
  148. static int __devinit ems_pci_add_card(struct pci_dev *pdev,
  149. const struct pci_device_id *ent)
  150. {
  151. struct sja1000_priv *priv;
  152. struct net_device *dev;
  153. struct ems_pci_card *card;
  154. int err, i;
  155. /* Enabling PCI device */
  156. if (pci_enable_device(pdev) < 0) {
  157. dev_err(&pdev->dev, "Enabling PCI device failed\n");
  158. return -ENODEV;
  159. }
  160. /* Allocating card structures to hold addresses, ... */
  161. card = kzalloc(sizeof(struct ems_pci_card), GFP_KERNEL);
  162. if (card == NULL) {
  163. dev_err(&pdev->dev, "Unable to allocate memory\n");
  164. pci_disable_device(pdev);
  165. return -ENOMEM;
  166. }
  167. pci_set_drvdata(pdev, card);
  168. card->pci_dev = pdev;
  169. card->channels = 0;
  170. /* Remap PITA configuration space, and controller memory area */
  171. card->conf_addr = pci_iomap(pdev, 0, EMS_PCI_MEM_SIZE);
  172. if (card->conf_addr == NULL) {
  173. err = -ENOMEM;
  174. goto failure_cleanup;
  175. }
  176. card->base_addr = pci_iomap(pdev, 1, EMS_PCI_MEM_SIZE);
  177. if (card->base_addr == NULL) {
  178. err = -ENOMEM;
  179. goto failure_cleanup;
  180. }
  181. /* Configure PITA-2 parallel interface (enable MUX) */
  182. writel(PITA2_MISC_CONFIG, card->conf_addr + PITA2_MISC);
  183. /* Check for unique EMS CAN signature */
  184. if (ems_pci_readb(card, 0) != 0x55 ||
  185. ems_pci_readb(card, 1) != 0xAA ||
  186. ems_pci_readb(card, 2) != 0x01 ||
  187. ems_pci_readb(card, 3) != 0xCB ||
  188. ems_pci_readb(card, 4) != 0x11) {
  189. dev_err(&pdev->dev, "Not EMS Dr. Thomas Wuensche interface\n");
  190. err = -ENODEV;
  191. goto failure_cleanup;
  192. }
  193. ems_pci_card_reset(card);
  194. /* Detect available channels */
  195. for (i = 0; i < EMS_PCI_MAX_CHAN; i++) {
  196. dev = alloc_sja1000dev(0);
  197. if (dev == NULL) {
  198. err = -ENOMEM;
  199. goto failure_cleanup;
  200. }
  201. card->net_dev[i] = dev;
  202. priv = netdev_priv(dev);
  203. priv->priv = card;
  204. priv->irq_flags = IRQF_SHARED;
  205. dev->irq = pdev->irq;
  206. priv->reg_base = card->base_addr + EMS_PCI_CAN_BASE_OFFSET
  207. + (i * EMS_PCI_CAN_CTRL_SIZE);
  208. /* Check if channel is present */
  209. if (ems_pci_check_chan(priv)) {
  210. priv->read_reg = ems_pci_read_reg;
  211. priv->write_reg = ems_pci_write_reg;
  212. priv->post_irq = ems_pci_post_irq;
  213. priv->can.clock.freq = EMS_PCI_CAN_CLOCK;
  214. priv->ocr = EMS_PCI_OCR;
  215. priv->cdr = EMS_PCI_CDR;
  216. SET_NETDEV_DEV(dev, &pdev->dev);
  217. /* Enable interrupts from card */
  218. writel(PITA2_ICR_INT0_EN, card->conf_addr + PITA2_ICR);
  219. /* Register SJA1000 device */
  220. err = register_sja1000dev(dev);
  221. if (err) {
  222. dev_err(&pdev->dev, "Registering device failed "
  223. "(err=%d)\n", err);
  224. free_sja1000dev(dev);
  225. goto failure_cleanup;
  226. }
  227. card->channels++;
  228. dev_info(&pdev->dev, "Channel #%d at 0x%p, irq %d\n",
  229. i + 1, priv->reg_base, dev->irq);
  230. } else {
  231. free_sja1000dev(dev);
  232. }
  233. }
  234. return 0;
  235. failure_cleanup:
  236. dev_err(&pdev->dev, "Error: %d. Cleaning Up.\n", err);
  237. ems_pci_del_card(pdev);
  238. return err;
  239. }
  240. static struct pci_driver ems_pci_driver = {
  241. .name = DRV_NAME,
  242. .id_table = ems_pci_tbl,
  243. .probe = ems_pci_add_card,
  244. .remove = ems_pci_del_card,
  245. };
  246. static int __init ems_pci_init(void)
  247. {
  248. return pci_register_driver(&ems_pci_driver);
  249. }
  250. static void __exit ems_pci_exit(void)
  251. {
  252. pci_unregister_driver(&ems_pci_driver);
  253. }
  254. module_init(ems_pci_init);
  255. module_exit(ems_pci_exit);