cc770_platform.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Driver for CC770 and AN82527 CAN controllers on the platform bus
  3. *
  4. * Copyright (C) 2009, 2011 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. /*
  16. * If platform data are used you should have similar definitions
  17. * in your board-specific code:
  18. *
  19. * static struct cc770_platform_data myboard_cc770_pdata = {
  20. * .osc_freq = 16000000,
  21. * .cir = 0x41,
  22. * .cor = 0x20,
  23. * .bcr = 0x40,
  24. * };
  25. *
  26. * Please see include/linux/can/platform/cc770.h for description of
  27. * above fields.
  28. *
  29. * If the device tree is used, you need a CAN node definition in your
  30. * DTS file similar to:
  31. *
  32. * can@3,100 {
  33. * compatible = "bosch,cc770";
  34. * reg = <3 0x100 0x80>;
  35. * interrupts = <2 0>;
  36. * interrupt-parent = <&mpic>;
  37. * bosch,external-clock-frequency = <16000000>;
  38. * };
  39. *
  40. * See "Documentation/devicetree/bindings/net/can/cc770.txt" for further
  41. * information.
  42. */
  43. #include <linux/kernel.h>
  44. #include <linux/module.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/netdevice.h>
  47. #include <linux/delay.h>
  48. #include <linux/platform_device.h>
  49. #include <linux/of.h>
  50. #include <linux/can.h>
  51. #include <linux/can/dev.h>
  52. #include <linux/can/platform/cc770.h>
  53. #include "cc770.h"
  54. #define DRV_NAME "cc770_platform"
  55. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  56. MODULE_DESCRIPTION("Socket-CAN driver for CC770 on the platform bus");
  57. MODULE_LICENSE("GPL v2");
  58. MODULE_ALIAS("platform:" DRV_NAME);
  59. #define CC770_PLATFORM_CAN_CLOCK 16000000
  60. static u8 cc770_platform_read_reg(const struct cc770_priv *priv, int reg)
  61. {
  62. return ioread8(priv->reg_base + reg);
  63. }
  64. static void cc770_platform_write_reg(const struct cc770_priv *priv, int reg,
  65. u8 val)
  66. {
  67. iowrite8(val, priv->reg_base + reg);
  68. }
  69. static int cc770_get_of_node_data(struct platform_device *pdev,
  70. struct cc770_priv *priv)
  71. {
  72. struct device_node *np = pdev->dev.of_node;
  73. const u32 *prop;
  74. int prop_size;
  75. u32 clkext;
  76. prop = of_get_property(np, "bosch,external-clock-frequency",
  77. &prop_size);
  78. if (prop && (prop_size == sizeof(u32)))
  79. clkext = *prop;
  80. else
  81. clkext = CC770_PLATFORM_CAN_CLOCK; /* default */
  82. priv->can.clock.freq = clkext;
  83. /* The system clock may not exceed 10 MHz */
  84. if (priv->can.clock.freq > 10000000) {
  85. priv->cpu_interface |= CPUIF_DSC;
  86. priv->can.clock.freq /= 2;
  87. }
  88. /* The memory clock may not exceed 8 MHz */
  89. if (priv->can.clock.freq > 8000000)
  90. priv->cpu_interface |= CPUIF_DMC;
  91. if (of_get_property(np, "bosch,divide-memory-clock", NULL))
  92. priv->cpu_interface |= CPUIF_DMC;
  93. if (of_get_property(np, "bosch,iso-low-speed-mux", NULL))
  94. priv->cpu_interface |= CPUIF_MUX;
  95. if (!of_get_property(np, "bosch,no-comperator-bypass", NULL))
  96. priv->bus_config |= BUSCFG_CBY;
  97. if (of_get_property(np, "bosch,disconnect-rx0-input", NULL))
  98. priv->bus_config |= BUSCFG_DR0;
  99. if (of_get_property(np, "bosch,disconnect-rx1-input", NULL))
  100. priv->bus_config |= BUSCFG_DR1;
  101. if (of_get_property(np, "bosch,disconnect-tx1-output", NULL))
  102. priv->bus_config |= BUSCFG_DT1;
  103. if (of_get_property(np, "bosch,polarity-dominant", NULL))
  104. priv->bus_config |= BUSCFG_POL;
  105. prop = of_get_property(np, "bosch,clock-out-frequency", &prop_size);
  106. if (prop && (prop_size == sizeof(u32)) && *prop > 0) {
  107. u32 cdv = clkext / *prop;
  108. int slew;
  109. if (cdv > 0 && cdv < 16) {
  110. priv->cpu_interface |= CPUIF_CEN;
  111. priv->clkout |= (cdv - 1) & CLKOUT_CD_MASK;
  112. prop = of_get_property(np, "bosch,slew-rate",
  113. &prop_size);
  114. if (prop && (prop_size == sizeof(u32))) {
  115. slew = *prop;
  116. } else {
  117. /* Determine default slew rate */
  118. slew = (CLKOUT_SL_MASK >>
  119. CLKOUT_SL_SHIFT) -
  120. ((cdv * clkext - 1) / 8000000);
  121. if (slew < 0)
  122. slew = 0;
  123. }
  124. priv->clkout |= (slew << CLKOUT_SL_SHIFT) &
  125. CLKOUT_SL_MASK;
  126. } else {
  127. dev_dbg(&pdev->dev, "invalid clock-out-frequency\n");
  128. }
  129. }
  130. return 0;
  131. }
  132. static int cc770_get_platform_data(struct platform_device *pdev,
  133. struct cc770_priv *priv)
  134. {
  135. struct cc770_platform_data *pdata = dev_get_platdata(&pdev->dev);
  136. priv->can.clock.freq = pdata->osc_freq;
  137. if (priv->cpu_interface & CPUIF_DSC)
  138. priv->can.clock.freq /= 2;
  139. priv->clkout = pdata->cor;
  140. priv->bus_config = pdata->bcr;
  141. priv->cpu_interface = pdata->cir;
  142. return 0;
  143. }
  144. static int cc770_platform_probe(struct platform_device *pdev)
  145. {
  146. struct net_device *dev;
  147. struct cc770_priv *priv;
  148. struct resource *mem;
  149. resource_size_t mem_size;
  150. void __iomem *base;
  151. int err, irq;
  152. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  153. irq = platform_get_irq(pdev, 0);
  154. if (!mem || irq <= 0)
  155. return -ENODEV;
  156. mem_size = resource_size(mem);
  157. if (!request_mem_region(mem->start, mem_size, pdev->name))
  158. return -EBUSY;
  159. base = ioremap(mem->start, mem_size);
  160. if (!base) {
  161. err = -ENOMEM;
  162. goto exit_release_mem;
  163. }
  164. dev = alloc_cc770dev(0);
  165. if (!dev) {
  166. err = -ENOMEM;
  167. goto exit_unmap_mem;
  168. }
  169. dev->irq = irq;
  170. priv = netdev_priv(dev);
  171. priv->read_reg = cc770_platform_read_reg;
  172. priv->write_reg = cc770_platform_write_reg;
  173. priv->irq_flags = IRQF_SHARED;
  174. priv->reg_base = base;
  175. if (pdev->dev.of_node)
  176. err = cc770_get_of_node_data(pdev, priv);
  177. else if (dev_get_platdata(&pdev->dev))
  178. err = cc770_get_platform_data(pdev, priv);
  179. else
  180. err = -ENODEV;
  181. if (err)
  182. goto exit_free_cc770;
  183. dev_dbg(&pdev->dev,
  184. "reg_base=0x%p irq=%d clock=%d cpu_interface=0x%02x "
  185. "bus_config=0x%02x clkout=0x%02x\n",
  186. priv->reg_base, dev->irq, priv->can.clock.freq,
  187. priv->cpu_interface, priv->bus_config, priv->clkout);
  188. platform_set_drvdata(pdev, dev);
  189. SET_NETDEV_DEV(dev, &pdev->dev);
  190. err = register_cc770dev(dev);
  191. if (err) {
  192. dev_err(&pdev->dev,
  193. "couldn't register CC700 device (err=%d)\n", err);
  194. goto exit_free_cc770;
  195. }
  196. return 0;
  197. exit_free_cc770:
  198. free_cc770dev(dev);
  199. exit_unmap_mem:
  200. iounmap(base);
  201. exit_release_mem:
  202. release_mem_region(mem->start, mem_size);
  203. return err;
  204. }
  205. static int cc770_platform_remove(struct platform_device *pdev)
  206. {
  207. struct net_device *dev = platform_get_drvdata(pdev);
  208. struct cc770_priv *priv = netdev_priv(dev);
  209. struct resource *mem;
  210. unregister_cc770dev(dev);
  211. iounmap(priv->reg_base);
  212. free_cc770dev(dev);
  213. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  214. release_mem_region(mem->start, resource_size(mem));
  215. return 0;
  216. }
  217. static const struct of_device_id cc770_platform_table[] = {
  218. {.compatible = "bosch,cc770"}, /* CC770 from Bosch */
  219. {.compatible = "intc,82527"}, /* AN82527 from Intel CP */
  220. {},
  221. };
  222. MODULE_DEVICE_TABLE(of, cc770_platform_table);
  223. static struct platform_driver cc770_platform_driver = {
  224. .driver = {
  225. .name = DRV_NAME,
  226. .of_match_table = cc770_platform_table,
  227. },
  228. .probe = cc770_platform_probe,
  229. .remove = cc770_platform_remove,
  230. };
  231. module_platform_driver(cc770_platform_driver);