f81601.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Fintek F81601 PCIE to 2 CAN controller driver
  3. *
  4. * Copyright (C) 2019 Peter Hong <peter_hong@fintek.com.tw>
  5. * Copyright (C) 2019 Linux Foundation
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/pci.h>
  14. #include <linux/can/dev.h>
  15. #include <linux/io.h>
  16. #include "sja1000.h"
  17. #define F81601_PCI_MAX_CHAN 2
  18. #define F81601_DECODE_REG 0x209
  19. #define F81601_IO_MODE BIT(7)
  20. #define F81601_MEM_MODE BIT(6)
  21. #define F81601_CFG_MODE BIT(5)
  22. #define F81601_CAN2_INTERNAL_CLK BIT(3)
  23. #define F81601_CAN1_INTERNAL_CLK BIT(2)
  24. #define F81601_CAN2_EN BIT(1)
  25. #define F81601_CAN1_EN BIT(0)
  26. #define F81601_TRAP_REG 0x20a
  27. #define F81601_CAN2_HAS_EN BIT(4)
  28. struct f81601_pci_card {
  29. void __iomem *addr;
  30. spinlock_t lock; /* use this spin lock only for write access */
  31. struct pci_dev *dev;
  32. struct net_device *net_dev[F81601_PCI_MAX_CHAN];
  33. };
  34. static const struct pci_device_id f81601_pci_tbl[] = {
  35. { PCI_DEVICE(0x1c29, 0x1703) },
  36. { /* sentinel */ },
  37. };
  38. MODULE_DEVICE_TABLE(pci, f81601_pci_tbl);
  39. static bool internal_clk = true;
  40. module_param(internal_clk, bool, 0444);
  41. MODULE_PARM_DESC(internal_clk, "Use internal clock, default true (24MHz)");
  42. static unsigned int external_clk;
  43. module_param(external_clk, uint, 0444);
  44. MODULE_PARM_DESC(external_clk, "External clock when internal_clk disabled");
  45. static u8 f81601_pci_read_reg(const struct sja1000_priv *priv, int port)
  46. {
  47. return readb(priv->reg_base + port);
  48. }
  49. static void f81601_pci_write_reg(const struct sja1000_priv *priv, int port,
  50. u8 val)
  51. {
  52. struct f81601_pci_card *card = priv->priv;
  53. unsigned long flags;
  54. spin_lock_irqsave(&card->lock, flags);
  55. writeb(val, priv->reg_base + port);
  56. readb(priv->reg_base);
  57. spin_unlock_irqrestore(&card->lock, flags);
  58. }
  59. static void f81601_pci_remove(struct pci_dev *pdev)
  60. {
  61. struct f81601_pci_card *card = pci_get_drvdata(pdev);
  62. struct net_device *dev;
  63. int i;
  64. for (i = 0; i < ARRAY_SIZE(card->net_dev); i++) {
  65. dev = card->net_dev[i];
  66. if (!dev)
  67. continue;
  68. dev_info(&pdev->dev, "%s: Removing %s\n", __func__, dev->name);
  69. unregister_sja1000dev(dev);
  70. free_sja1000dev(dev);
  71. }
  72. }
  73. /* Probe F81601 based device for the SJA1000 chips and register each
  74. * available CAN channel to SJA1000 Socket-CAN subsystem.
  75. */
  76. static int f81601_pci_probe(struct pci_dev *pdev,
  77. const struct pci_device_id *ent)
  78. {
  79. struct sja1000_priv *priv;
  80. struct net_device *dev;
  81. struct f81601_pci_card *card;
  82. int err, i, count;
  83. u8 tmp;
  84. if (pcim_enable_device(pdev) < 0) {
  85. dev_err(&pdev->dev, "Failed to enable PCI device\n");
  86. return -ENODEV;
  87. }
  88. dev_info(&pdev->dev, "Detected card at slot #%i\n",
  89. PCI_SLOT(pdev->devfn));
  90. card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
  91. if (!card)
  92. return -ENOMEM;
  93. card->dev = pdev;
  94. spin_lock_init(&card->lock);
  95. pci_set_drvdata(pdev, card);
  96. tmp = F81601_IO_MODE | F81601_MEM_MODE | F81601_CFG_MODE |
  97. F81601_CAN2_EN | F81601_CAN1_EN;
  98. if (internal_clk) {
  99. tmp |= F81601_CAN2_INTERNAL_CLK | F81601_CAN1_INTERNAL_CLK;
  100. dev_info(&pdev->dev,
  101. "F81601 running with internal clock: 24Mhz\n");
  102. } else {
  103. dev_info(&pdev->dev,
  104. "F81601 running with external clock: %dMhz\n",
  105. external_clk / 1000000);
  106. }
  107. pci_write_config_byte(pdev, F81601_DECODE_REG, tmp);
  108. card->addr = pcim_iomap(pdev, 0, pci_resource_len(pdev, 0));
  109. if (!card->addr) {
  110. err = -ENOMEM;
  111. dev_err(&pdev->dev, "%s: Failed to remap BAR\n", __func__);
  112. goto failure_cleanup;
  113. }
  114. /* read CAN2_HW_EN strap pin to detect how many CANBUS do we have */
  115. count = ARRAY_SIZE(card->net_dev);
  116. pci_read_config_byte(pdev, F81601_TRAP_REG, &tmp);
  117. if (!(tmp & F81601_CAN2_HAS_EN))
  118. count = 1;
  119. for (i = 0; i < count; i++) {
  120. dev = alloc_sja1000dev(0);
  121. if (!dev) {
  122. err = -ENOMEM;
  123. goto failure_cleanup;
  124. }
  125. priv = netdev_priv(dev);
  126. priv->priv = card;
  127. priv->irq_flags = IRQF_SHARED;
  128. priv->reg_base = card->addr + 0x80 * i;
  129. priv->read_reg = f81601_pci_read_reg;
  130. priv->write_reg = f81601_pci_write_reg;
  131. if (internal_clk)
  132. priv->can.clock.freq = 24000000 / 2;
  133. else
  134. priv->can.clock.freq = external_clk / 2;
  135. priv->ocr = OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL;
  136. priv->cdr = CDR_CBP;
  137. SET_NETDEV_DEV(dev, &pdev->dev);
  138. dev->dev_id = i;
  139. dev->irq = pdev->irq;
  140. /* Register SJA1000 device */
  141. err = register_sja1000dev(dev);
  142. if (err) {
  143. dev_err(&pdev->dev,
  144. "%s: Registering device failed: %x\n", __func__,
  145. err);
  146. free_sja1000dev(dev);
  147. goto failure_cleanup;
  148. }
  149. card->net_dev[i] = dev;
  150. dev_info(&pdev->dev, "Channel #%d, %s at 0x%p, irq %d\n", i,
  151. dev->name, priv->reg_base, dev->irq);
  152. }
  153. return 0;
  154. failure_cleanup:
  155. dev_err(&pdev->dev, "%s: failed: %d. Cleaning Up.\n", __func__, err);
  156. f81601_pci_remove(pdev);
  157. return err;
  158. }
  159. static struct pci_driver f81601_pci_driver = {
  160. .name = "f81601",
  161. .id_table = f81601_pci_tbl,
  162. .probe = f81601_pci_probe,
  163. .remove = f81601_pci_remove,
  164. };
  165. MODULE_DESCRIPTION("Fintek F81601 PCIE to 2 CANBUS adaptor driver");
  166. MODULE_AUTHOR("Peter Hong <peter_hong@fintek.com.tw>");
  167. MODULE_LICENSE("GPL v2");
  168. module_pci_driver(f81601_pci_driver);