sja1000_of_platform.c 5.6 KB

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