sja1000_of_platform.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <asm/prom.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 in_8(priv->reg_base + reg);
  54. }
  55. static void sja1000_ofp_write_reg(const struct sja1000_priv *priv,
  56. int reg, u8 val)
  57. {
  58. out_8(priv->reg_base + reg, val);
  59. }
  60. static int __devexit sja1000_ofp_remove(struct platform_device *ofdev)
  61. {
  62. struct net_device *dev = dev_get_drvdata(&ofdev->dev);
  63. struct sja1000_priv *priv = netdev_priv(dev);
  64. struct device_node *np = ofdev->dev.of_node;
  65. struct resource res;
  66. dev_set_drvdata(&ofdev->dev, NULL);
  67. unregister_sja1000dev(dev);
  68. free_sja1000dev(dev);
  69. iounmap(priv->reg_base);
  70. irq_dispose_mapping(dev->irq);
  71. of_address_to_resource(np, 0, &res);
  72. release_mem_region(res.start, resource_size(&res));
  73. return 0;
  74. }
  75. static int __devinit sja1000_ofp_probe(struct platform_device *ofdev)
  76. {
  77. struct device_node *np = ofdev->dev.of_node;
  78. struct net_device *dev;
  79. struct sja1000_priv *priv;
  80. struct resource res;
  81. const u32 *prop;
  82. int err, irq, res_size, prop_size;
  83. void __iomem *base;
  84. err = of_address_to_resource(np, 0, &res);
  85. if (err) {
  86. dev_err(&ofdev->dev, "invalid address\n");
  87. return err;
  88. }
  89. res_size = resource_size(&res);
  90. if (!request_mem_region(res.start, res_size, DRV_NAME)) {
  91. dev_err(&ofdev->dev, "couldn't request %pR\n", &res);
  92. return -EBUSY;
  93. }
  94. base = ioremap_nocache(res.start, res_size);
  95. if (!base) {
  96. dev_err(&ofdev->dev, "couldn't ioremap %pR\n", &res);
  97. err = -ENOMEM;
  98. goto exit_release_mem;
  99. }
  100. irq = irq_of_parse_and_map(np, 0);
  101. if (irq == NO_IRQ) {
  102. dev_err(&ofdev->dev, "no irq found\n");
  103. err = -ENODEV;
  104. goto exit_unmap_mem;
  105. }
  106. dev = alloc_sja1000dev(0);
  107. if (!dev) {
  108. err = -ENOMEM;
  109. goto exit_dispose_irq;
  110. }
  111. priv = netdev_priv(dev);
  112. priv->read_reg = sja1000_ofp_read_reg;
  113. priv->write_reg = sja1000_ofp_write_reg;
  114. prop = of_get_property(np, "nxp,external-clock-frequency", &prop_size);
  115. if (prop && (prop_size == sizeof(u32)))
  116. priv->can.clock.freq = *prop / 2;
  117. else
  118. priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
  119. prop = of_get_property(np, "nxp,tx-output-mode", &prop_size);
  120. if (prop && (prop_size == sizeof(u32)))
  121. priv->ocr |= *prop & OCR_MODE_MASK;
  122. else
  123. priv->ocr |= OCR_MODE_NORMAL; /* default */
  124. prop = of_get_property(np, "nxp,tx-output-config", &prop_size);
  125. if (prop && (prop_size == sizeof(u32)))
  126. priv->ocr |= (*prop << OCR_TX_SHIFT) & OCR_TX_MASK;
  127. else
  128. priv->ocr |= OCR_TX0_PULLDOWN; /* default */
  129. prop = of_get_property(np, "nxp,clock-out-frequency", &prop_size);
  130. if (prop && (prop_size == sizeof(u32)) && *prop) {
  131. u32 divider = priv->can.clock.freq * 2 / *prop;
  132. if (divider > 1)
  133. priv->cdr |= divider / 2 - 1;
  134. else
  135. priv->cdr |= CDR_CLKOUT_MASK;
  136. } else {
  137. priv->cdr |= CDR_CLK_OFF; /* default */
  138. }
  139. prop = of_get_property(np, "nxp,no-comparator-bypass", NULL);
  140. if (!prop)
  141. priv->cdr |= CDR_CBP; /* default */
  142. priv->irq_flags = IRQF_SHARED;
  143. priv->reg_base = base;
  144. dev->irq = irq;
  145. dev_info(&ofdev->dev,
  146. "reg_base=0x%p irq=%d clock=%d ocr=0x%02x cdr=0x%02x\n",
  147. priv->reg_base, dev->irq, priv->can.clock.freq,
  148. priv->ocr, priv->cdr);
  149. dev_set_drvdata(&ofdev->dev, dev);
  150. SET_NETDEV_DEV(dev, &ofdev->dev);
  151. err = register_sja1000dev(dev);
  152. if (err) {
  153. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  154. DRV_NAME, err);
  155. goto exit_free_sja1000;
  156. }
  157. return 0;
  158. exit_free_sja1000:
  159. free_sja1000dev(dev);
  160. exit_dispose_irq:
  161. irq_dispose_mapping(irq);
  162. exit_unmap_mem:
  163. iounmap(base);
  164. exit_release_mem:
  165. release_mem_region(res.start, res_size);
  166. return err;
  167. }
  168. static struct of_device_id __devinitdata sja1000_ofp_table[] = {
  169. {.compatible = "nxp,sja1000"},
  170. {},
  171. };
  172. MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
  173. static struct platform_driver sja1000_ofp_driver = {
  174. .driver = {
  175. .owner = THIS_MODULE,
  176. .name = DRV_NAME,
  177. .of_match_table = sja1000_ofp_table,
  178. },
  179. .probe = sja1000_ofp_probe,
  180. .remove = __devexit_p(sja1000_ofp_remove),
  181. };
  182. module_platform_driver(sja1000_ofp_driver);