peak_pci.c 7.2 KB

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