/drivers/net/can/sja1000/sja1000_of_platform.c

http://github.com/mirrors/linux · C · 222 lines · 149 code · 40 blank · 33 comment · 15 complexity · c1019ed38acc08012ec8739a02414a9a MD5 · raw file

  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 = platform_get_drvdata(ofdev);
  65. struct sja1000_priv *priv = netdev_priv(dev);
  66. struct device_node *np = ofdev->dev.of_node;
  67. struct resource res;
  68. unregister_sja1000dev(dev);
  69. free_sja1000dev(dev);
  70. iounmap(priv->reg_base);
  71. irq_dispose_mapping(dev->irq);
  72. of_address_to_resource(np, 0, &res);
  73. release_mem_region(res.start, resource_size(&res));
  74. return 0;
  75. }
  76. static int sja1000_ofp_probe(struct platform_device *ofdev)
  77. {
  78. struct device_node *np = ofdev->dev.of_node;
  79. struct net_device *dev;
  80. struct sja1000_priv *priv;
  81. struct resource res;
  82. u32 prop;
  83. int err, irq, res_size;
  84. void __iomem *base;
  85. err = of_address_to_resource(np, 0, &res);
  86. if (err) {
  87. dev_err(&ofdev->dev, "invalid address\n");
  88. return err;
  89. }
  90. res_size = resource_size(&res);
  91. if (!request_mem_region(res.start, res_size, DRV_NAME)) {
  92. dev_err(&ofdev->dev, "couldn't request %pR\n", &res);
  93. return -EBUSY;
  94. }
  95. base = ioremap_nocache(res.start, res_size);
  96. if (!base) {
  97. dev_err(&ofdev->dev, "couldn't ioremap %pR\n", &res);
  98. err = -ENOMEM;
  99. goto exit_release_mem;
  100. }
  101. irq = irq_of_parse_and_map(np, 0);
  102. if (irq == 0) {
  103. dev_err(&ofdev->dev, "no irq found\n");
  104. err = -ENODEV;
  105. goto exit_unmap_mem;
  106. }
  107. dev = alloc_sja1000dev(0);
  108. if (!dev) {
  109. err = -ENOMEM;
  110. goto exit_dispose_irq;
  111. }
  112. priv = netdev_priv(dev);
  113. priv->read_reg = sja1000_ofp_read_reg;
  114. priv->write_reg = sja1000_ofp_write_reg;
  115. err = of_property_read_u32(np, "nxp,external-clock-frequency", &prop);
  116. if (!err)
  117. priv->can.clock.freq = prop / 2;
  118. else
  119. priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
  120. err = of_property_read_u32(np, "nxp,tx-output-mode", &prop);
  121. if (!err)
  122. priv->ocr |= prop & OCR_MODE_MASK;
  123. else
  124. priv->ocr |= OCR_MODE_NORMAL; /* default */
  125. err = of_property_read_u32(np, "nxp,tx-output-config", &prop);
  126. if (!err)
  127. priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
  128. else
  129. priv->ocr |= OCR_TX0_PULLDOWN; /* default */
  130. err = of_property_read_u32(np, "nxp,clock-out-frequency", &prop);
  131. if (!err && prop) {
  132. u32 divider = priv->can.clock.freq * 2 / prop;
  133. if (divider > 1)
  134. priv->cdr |= divider / 2 - 1;
  135. else
  136. priv->cdr |= CDR_CLKOUT_MASK;
  137. } else {
  138. priv->cdr |= CDR_CLK_OFF; /* default */
  139. }
  140. if (!of_property_read_bool(np, "nxp,no-comparator-bypass"))
  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. platform_set_drvdata(ofdev, 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 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 = sja1000_ofp_remove,
  181. };
  182. module_platform_driver(sja1000_ofp_driver);