peak_pci.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
  3. *
  4. * Derived from the PCAN project file driver/src/pcan_pci.c:
  5. *
  6. * Copyright (C) 2001-2006 PEAK System-Technik GmbH
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the version 2 of the GNU General Public License
  10. * as published by the Free Software Foundation
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/version.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/delay.h>
  27. #include <linux/pci.h>
  28. #include <linux/io.h>
  29. #include <linux/can.h>
  30. #include <linux/can/dev.h>
  31. #include "sja1000.h"
  32. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  33. MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI/PCIe cards");
  34. MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe CAN card");
  35. MODULE_LICENSE("GPL v2");
  36. #define DRV_NAME "peak_pci"
  37. struct peak_pci_chan {
  38. void __iomem *cfg_base; /* Common for all channels */
  39. struct net_device *next_dev; /* Chain of network devices */
  40. u16 icr_mask; /* Interrupt mask for fast ack */
  41. };
  42. #define PEAK_PCI_CAN_CLOCK (16000000 / 2)
  43. #define PEAK_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
  44. #define PEAK_PCI_OCR OCR_TX0_PUSHPULL
  45. /*
  46. * Important PITA registers
  47. */
  48. #define PITA_ICR 0x00 /* Interrupt control register */
  49. #define PITA_GPIOICR 0x18 /* GPIO interface control register */
  50. #define PITA_MISC 0x1C /* Miscellaneous register */
  51. #define PEAK_PCI_CFG_SIZE 0x1000 /* Size of the config PCI bar */
  52. #define PEAK_PCI_CHAN_SIZE 0x0400 /* Size used by the channel */
  53. #define PEAK_PCI_VENDOR_ID 0x001C /* The PCI device and vendor IDs */
  54. #define PEAK_PCI_DEVICE_ID 0x0001 /* for PCI/PCIe slot cards */
  55. static const u16 peak_pci_icr_masks[] = {0x02, 0x01, 0x40, 0x80};
  56. static DEFINE_PCI_DEVICE_TABLE(peak_pci_tbl) = {
  57. {PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  58. {0,}
  59. };
  60. MODULE_DEVICE_TABLE(pci, peak_pci_tbl);
  61. static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port)
  62. {
  63. return readb(priv->reg_base + (port << 2));
  64. }
  65. static void peak_pci_write_reg(const struct sja1000_priv *priv,
  66. int port, u8 val)
  67. {
  68. writeb(val, priv->reg_base + (port << 2));
  69. }
  70. static void peak_pci_post_irq(const struct sja1000_priv *priv)
  71. {
  72. struct peak_pci_chan *chan = priv->priv;
  73. u16 icr;
  74. /* Select and clear in PITA stored interrupt */
  75. icr = readw(chan->cfg_base + PITA_ICR);
  76. if (icr & chan->icr_mask)
  77. writew(chan->icr_mask, chan->cfg_base + PITA_ICR);
  78. }
  79. static int __devinit peak_pci_probe(struct pci_dev *pdev,
  80. const struct pci_device_id *ent)
  81. {
  82. struct sja1000_priv *priv;
  83. struct peak_pci_chan *chan;
  84. struct net_device *dev, *dev0 = NULL;
  85. void __iomem *cfg_base, *reg_base;
  86. u16 sub_sys_id, icr;
  87. int i, err, channels;
  88. err = pci_enable_device(pdev);
  89. if (err)
  90. return err;
  91. err = pci_request_regions(pdev, DRV_NAME);
  92. if (err)
  93. goto failure_disable_pci;
  94. err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
  95. if (err)
  96. goto failure_release_regions;
  97. dev_dbg(&pdev->dev, "probing device %04x:%04x:%04x\n",
  98. pdev->vendor, pdev->device, sub_sys_id);
  99. err = pci_write_config_word(pdev, 0x44, 0);
  100. if (err)
  101. goto failure_release_regions;
  102. if (sub_sys_id >= 12)
  103. channels = 4;
  104. else if (sub_sys_id >= 10)
  105. channels = 3;
  106. else if (sub_sys_id >= 4)
  107. channels = 2;
  108. else
  109. channels = 1;
  110. cfg_base = pci_iomap(pdev, 0, PEAK_PCI_CFG_SIZE);
  111. if (!cfg_base) {
  112. dev_err(&pdev->dev, "failed to map PCI resource #0\n");
  113. goto failure_release_regions;
  114. }
  115. reg_base = pci_iomap(pdev, 1, PEAK_PCI_CHAN_SIZE * channels);
  116. if (!reg_base) {
  117. dev_err(&pdev->dev, "failed to map PCI resource #1\n");
  118. goto failure_unmap_cfg_base;
  119. }
  120. /* Set GPIO control register */
  121. writew(0x0005, cfg_base + PITA_GPIOICR + 2);
  122. /* Enable all channels of this card */
  123. writeb(0x00, cfg_base + PITA_GPIOICR);
  124. /* Toggle reset */
  125. writeb(0x05, cfg_base + PITA_MISC + 3);
  126. mdelay(5);
  127. /* Leave parport mux mode */
  128. writeb(0x04, cfg_base + PITA_MISC + 3);
  129. icr = readw(cfg_base + PITA_ICR + 2);
  130. for (i = 0; i < channels; i++) {
  131. dev = alloc_sja1000dev(sizeof(struct peak_pci_chan));
  132. if (!dev) {
  133. err = -ENOMEM;
  134. goto failure_remove_channels;
  135. }
  136. priv = netdev_priv(dev);
  137. chan = priv->priv;
  138. chan->cfg_base = cfg_base;
  139. priv->reg_base = reg_base + i * PEAK_PCI_CHAN_SIZE;
  140. priv->read_reg = peak_pci_read_reg;
  141. priv->write_reg = peak_pci_write_reg;
  142. priv->post_irq = peak_pci_post_irq;
  143. priv->can.clock.freq = PEAK_PCI_CAN_CLOCK;
  144. priv->ocr = PEAK_PCI_OCR;
  145. priv->cdr = PEAK_PCI_CDR;
  146. /* Neither a slave nor a single device distributes the clock */
  147. if (channels == 1 || i > 0)
  148. priv->cdr |= CDR_CLK_OFF;
  149. /* Setup interrupt handling */
  150. priv->irq_flags = IRQF_SHARED;
  151. dev->irq = pdev->irq;
  152. chan->icr_mask = peak_pci_icr_masks[i];
  153. icr |= chan->icr_mask;
  154. SET_NETDEV_DEV(dev, &pdev->dev);
  155. err = register_sja1000dev(dev);
  156. if (err) {
  157. dev_err(&pdev->dev, "failed to register device\n");
  158. free_sja1000dev(dev);
  159. goto failure_remove_channels;
  160. }
  161. /* Create chain of SJA1000 devices */
  162. if (i == 0)
  163. dev0 = dev;
  164. else
  165. chan->next_dev = dev;
  166. dev_info(&pdev->dev,
  167. "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n",
  168. dev->name, priv->reg_base, chan->cfg_base, dev->irq);
  169. }
  170. pci_set_drvdata(pdev, dev0);
  171. /* Enable interrupts */
  172. writew(icr, cfg_base + PITA_ICR + 2);
  173. return 0;
  174. failure_remove_channels:
  175. /* Disable interrupts */
  176. writew(0x0, cfg_base + PITA_ICR + 2);
  177. for (dev = dev0; dev; dev = chan->next_dev) {
  178. unregister_sja1000dev(dev);
  179. free_sja1000dev(dev);
  180. priv = netdev_priv(dev);
  181. chan = priv->priv;
  182. dev = chan->next_dev;
  183. }
  184. pci_iounmap(pdev, reg_base);
  185. failure_unmap_cfg_base:
  186. pci_iounmap(pdev, cfg_base);
  187. failure_release_regions:
  188. pci_release_regions(pdev);
  189. failure_disable_pci:
  190. pci_disable_device(pdev);
  191. return err;
  192. }
  193. static void __devexit peak_pci_remove(struct pci_dev *pdev)
  194. {
  195. struct net_device *dev = pci_get_drvdata(pdev); /* First device */
  196. struct sja1000_priv *priv = netdev_priv(dev);
  197. struct peak_pci_chan *chan = priv->priv;
  198. void __iomem *cfg_base = chan->cfg_base;
  199. void __iomem *reg_base = priv->reg_base;
  200. /* Disable interrupts */
  201. writew(0x0, cfg_base + PITA_ICR + 2);
  202. /* Loop over all registered devices */
  203. while (1) {
  204. dev_info(&pdev->dev, "removing device %s\n", dev->name);
  205. unregister_sja1000dev(dev);
  206. free_sja1000dev(dev);
  207. dev = chan->next_dev;
  208. if (!dev)
  209. break;
  210. priv = netdev_priv(dev);
  211. chan = priv->priv;
  212. }
  213. pci_iounmap(pdev, reg_base);
  214. pci_iounmap(pdev, cfg_base);
  215. pci_release_regions(pdev);
  216. pci_disable_device(pdev);
  217. pci_set_drvdata(pdev, NULL);
  218. }
  219. static struct pci_driver peak_pci_driver = {
  220. .name = DRV_NAME,
  221. .id_table = peak_pci_tbl,
  222. .probe = peak_pci_probe,
  223. .remove = __devexit_p(peak_pci_remove),
  224. };
  225. static int __init peak_pci_init(void)
  226. {
  227. return pci_register_driver(&peak_pci_driver);
  228. }
  229. module_init(peak_pci_init);
  230. static void __exit peak_pci_exit(void)
  231. {
  232. pci_unregister_driver(&peak_pci_driver);
  233. }
  234. module_exit(peak_pci_exit);