sja1000_of_platform.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Driver for SJA1000 CAN controllers on the OpenFirmware platform bus
  3. *
  4. * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.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. /* This is a generic driver for SJA1000 chips on the OpenFirmware platform
  19. * bus found on embedded PowerPC systems. You need a SJA1000 CAN node
  20. * definition in your flattened device tree source (DTS) file similar to:
  21. *
  22. * can@3,100 {
  23. * compatible = "nxp,sja1000";
  24. * reg = <3 0x100 0x80>;
  25. * interrupts = <2 0>;
  26. * interrupt-parent = <&mpic>;
  27. * nxp,external-clock-frequency = <16000000>;
  28. * };
  29. *
  30. * See "Documentation/devicetree/bindings/net/can/sja1000.txt" for further
  31. * information.
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/delay.h>
  38. #include <linux/io.h>
  39. #include <linux/can/dev.h>
  40. #include <linux/of_platform.h>
  41. #include <linux/of_address.h>
  42. #include <linux/of_irq.h>
  43. #include "sja1000.h"
  44. #define DRV_NAME "sja1000_of_platform"
  45. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  46. MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the OF platform bus");
  47. MODULE_LICENSE("GPL v2");
  48. #define SJA1000_OFP_CAN_CLOCK (16000000 / 2)
  49. #define SJA1000_OFP_OCR OCR_TX0_PULLDOWN
  50. #define SJA1000_OFP_CDR (CDR_CBP | CDR_CLK_OFF)
  51. static u8 sja1000_ofp_read_reg(const struct sja1000_priv *priv, int reg)
  52. {
  53. return ioread8(priv->reg_base + reg);
  54. }
  55. static void sja1000_ofp_write_reg(const struct sja1000_priv *priv,
  56. int reg, u8 val)
  57. {
  58. iowrite8(val, priv->reg_base + reg);
  59. }
  60. static int sja1000_ofp_remove(struct platform_device *ofdev)
  61. {
  62. struct net_device *dev = platform_get_drvdata(ofdev);
  63. struct sja1000_priv *priv = netdev_priv(dev);
  64. struct device_node *np = ofdev->dev.of_node;
  65. struct resource res;
  66. unregister_sja1000dev(dev);
  67. free_sja1000dev(dev);
  68. iounmap(priv->reg_base);
  69. irq_dispose_mapping(dev->irq);
  70. of_address_to_resource(np, 0, &res);
  71. release_mem_region(res.start, resource_size(&res));
  72. return 0;
  73. }
  74. static int sja1000_ofp_probe(struct platform_device *ofdev)
  75. {
  76. struct device_node *np = ofdev->dev.of_node;
  77. struct net_device *dev;
  78. struct sja1000_priv *priv;
  79. struct resource res;
  80. u32 prop;
  81. int err, irq, res_size;
  82. void __iomem *base;
  83. err = of_address_to_resource(np, 0, &res);
  84. if (err) {
  85. dev_err(&ofdev->dev, "invalid address\n");
  86. return err;
  87. }
  88. res_size = resource_size(&res);
  89. if (!request_mem_region(res.start, res_size, DRV_NAME)) {
  90. dev_err(&ofdev->dev, "couldn't request %pR\n", &res);
  91. return -EBUSY;
  92. }
  93. base = ioremap_nocache(res.start, res_size);
  94. if (!base) {
  95. dev_err(&ofdev->dev, "couldn't ioremap %pR\n", &res);
  96. err = -ENOMEM;
  97. goto exit_release_mem;
  98. }
  99. irq = irq_of_parse_and_map(np, 0);
  100. if (irq == 0) {
  101. dev_err(&ofdev->dev, "no irq found\n");
  102. err = -ENODEV;
  103. goto exit_unmap_mem;
  104. }
  105. dev = alloc_sja1000dev(0);
  106. if (!dev) {
  107. err = -ENOMEM;
  108. goto exit_dispose_irq;
  109. }
  110. priv = netdev_priv(dev);
  111. priv->read_reg = sja1000_ofp_read_reg;
  112. priv->write_reg = sja1000_ofp_write_reg;
  113. err = of_property_read_u32(np, "nxp,external-clock-frequency", &prop);
  114. if (!err)
  115. priv->can.clock.freq = prop / 2;
  116. else
  117. priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
  118. err = of_property_read_u32(np, "nxp,tx-output-mode", &prop);
  119. if (!err)
  120. priv->ocr |= prop & OCR_MODE_MASK;
  121. else
  122. priv->ocr |= OCR_MODE_NORMAL; /* default */
  123. err = of_property_read_u32(np, "nxp,tx-output-config", &prop);
  124. if (!err)
  125. priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
  126. else
  127. priv->ocr |= OCR_TX0_PULLDOWN; /* default */
  128. err = of_property_read_u32(np, "nxp,clock-out-frequency", &prop);
  129. if (!err && prop) {
  130. u32 divider = priv->can.clock.freq * 2 / prop;
  131. if (divider > 1)
  132. priv->cdr |= divider / 2 - 1;
  133. else
  134. priv->cdr |= CDR_CLKOUT_MASK;
  135. } else {
  136. priv->cdr |= CDR_CLK_OFF; /* default */
  137. }
  138. if (!of_property_read_bool(np, "nxp,no-comparator-bypass"))
  139. priv->cdr |= CDR_CBP; /* default */
  140. priv->irq_flags = IRQF_SHARED;
  141. priv->reg_base = base;
  142. dev->irq = irq;
  143. dev_info(&ofdev->dev,
  144. "reg_base=0x%p irq=%d clock=%d ocr=0x%02x cdr=0x%02x\n",
  145. priv->reg_base, dev->irq, priv->can.clock.freq,
  146. priv->ocr, priv->cdr);
  147. platform_set_drvdata(ofdev, dev);
  148. SET_NETDEV_DEV(dev, &ofdev->dev);
  149. err = register_sja1000dev(dev);
  150. if (err) {
  151. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  152. DRV_NAME, err);
  153. goto exit_free_sja1000;
  154. }
  155. return 0;
  156. exit_free_sja1000:
  157. free_sja1000dev(dev);
  158. exit_dispose_irq:
  159. irq_dispose_mapping(irq);
  160. exit_unmap_mem:
  161. iounmap(base);
  162. exit_release_mem:
  163. release_mem_region(res.start, res_size);
  164. return err;
  165. }
  166. static struct of_device_id sja1000_ofp_table[] = {
  167. {.compatible = "nxp,sja1000"},
  168. {},
  169. };
  170. MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
  171. static struct platform_driver sja1000_ofp_driver = {
  172. .driver = {
  173. .owner = THIS_MODULE,
  174. .name = DRV_NAME,
  175. .of_match_table = sja1000_ofp_table,
  176. },
  177. .probe = sja1000_ofp_probe,
  178. .remove = sja1000_ofp_remove,
  179. };
  180. module_platform_driver(sja1000_ofp_driver);