sja1000_of_platform.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. const u32 *prop;
  84. int err, irq, res_size, prop_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. prop = of_get_property(np, "nxp,external-clock-frequency", &prop_size);
  117. if (prop && (prop_size == sizeof(u32)))
  118. priv->can.clock.freq = *prop / 2;
  119. else
  120. priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
  121. prop = of_get_property(np, "nxp,tx-output-mode", &prop_size);
  122. if (prop && (prop_size == sizeof(u32)))
  123. priv->ocr |= *prop & OCR_MODE_MASK;
  124. else
  125. priv->ocr |= OCR_MODE_NORMAL; /* default */
  126. prop = of_get_property(np, "nxp,tx-output-config", &prop_size);
  127. if (prop && (prop_size == sizeof(u32)))
  128. priv->ocr |= (*prop << OCR_TX_SHIFT) & OCR_TX_MASK;
  129. else
  130. priv->ocr |= OCR_TX0_PULLDOWN; /* default */
  131. prop = of_get_property(np, "nxp,clock-out-frequency", &prop_size);
  132. if (prop && (prop_size == sizeof(u32)) && *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. prop = of_get_property(np, "nxp,no-comparator-bypass", NULL);
  142. if (!prop)
  143. priv->cdr |= CDR_CBP; /* default */
  144. priv->irq_flags = IRQF_SHARED;
  145. priv->reg_base = base;
  146. dev->irq = irq;
  147. dev_info(&ofdev->dev,
  148. "reg_base=0x%p irq=%d clock=%d ocr=0x%02x cdr=0x%02x\n",
  149. priv->reg_base, dev->irq, priv->can.clock.freq,
  150. priv->ocr, priv->cdr);
  151. dev_set_drvdata(&ofdev->dev, dev);
  152. SET_NETDEV_DEV(dev, &ofdev->dev);
  153. err = register_sja1000dev(dev);
  154. if (err) {
  155. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  156. DRV_NAME, err);
  157. goto exit_free_sja1000;
  158. }
  159. return 0;
  160. exit_free_sja1000:
  161. free_sja1000dev(dev);
  162. exit_dispose_irq:
  163. irq_dispose_mapping(irq);
  164. exit_unmap_mem:
  165. iounmap(base);
  166. exit_release_mem:
  167. release_mem_region(res.start, res_size);
  168. return err;
  169. }
  170. static struct of_device_id sja1000_ofp_table[] = {
  171. {.compatible = "nxp,sja1000"},
  172. {},
  173. };
  174. MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
  175. static struct platform_driver sja1000_ofp_driver = {
  176. .driver = {
  177. .owner = THIS_MODULE,
  178. .name = DRV_NAME,
  179. .of_match_table = sja1000_ofp_table,
  180. },
  181. .probe = sja1000_ofp_probe,
  182. .remove = sja1000_ofp_remove,
  183. };
  184. module_platform_driver(sja1000_ofp_driver);