c_can_platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Platform CAN bus driver for Bosch C_CAN controller
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Bhupesh Sharma <bhupesh.sharma@st.com>
  6. *
  7. * Borrowed heavily from the C_CAN driver originally written by:
  8. * Copyright (C) 2007
  9. * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
  10. * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
  11. *
  12. * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
  13. * Bosch C_CAN user manual can be obtained from:
  14. * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
  15. * users_manual_c_can.pdf
  16. *
  17. * This file is licensed under the terms of the GNU General Public
  18. * License version 2. This program is licensed "as is" without any
  19. * warranty of any kind, whether express or implied.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/list.h>
  29. #include <linux/io.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/clk.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/can/dev.h>
  35. #include "c_can.h"
  36. #define CAN_RAMINIT_START_MASK(i) (0x001 << (i))
  37. #define CAN_RAMINIT_DONE_MASK(i) (0x100 << (i))
  38. #define CAN_RAMINIT_ALL_MASK(i) (0x101 << (i))
  39. #define DCAN_RAM_INIT_BIT (1 << 3)
  40. static DEFINE_SPINLOCK(raminit_lock);
  41. /*
  42. * 16-bit c_can registers can be arranged differently in the memory
  43. * architecture of different implementations. For example: 16-bit
  44. * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
  45. * Handle the same by providing a common read/write interface.
  46. */
  47. static u16 c_can_plat_read_reg_aligned_to_16bit(const struct c_can_priv *priv,
  48. enum reg index)
  49. {
  50. return readw(priv->base + priv->regs[index]);
  51. }
  52. static void c_can_plat_write_reg_aligned_to_16bit(const struct c_can_priv *priv,
  53. enum reg index, u16 val)
  54. {
  55. writew(val, priv->base + priv->regs[index]);
  56. }
  57. static u16 c_can_plat_read_reg_aligned_to_32bit(const struct c_can_priv *priv,
  58. enum reg index)
  59. {
  60. return readw(priv->base + 2 * priv->regs[index]);
  61. }
  62. static void c_can_plat_write_reg_aligned_to_32bit(const struct c_can_priv *priv,
  63. enum reg index, u16 val)
  64. {
  65. writew(val, priv->base + 2 * priv->regs[index]);
  66. }
  67. static void c_can_hw_raminit_wait_ti(const struct c_can_priv *priv, u32 mask,
  68. u32 val)
  69. {
  70. /* We look only at the bits of our instance. */
  71. val &= mask;
  72. while ((readl(priv->raminit_ctrlreg) & mask) != val)
  73. udelay(1);
  74. }
  75. static void c_can_hw_raminit_ti(const struct c_can_priv *priv, bool enable)
  76. {
  77. u32 mask = CAN_RAMINIT_ALL_MASK(priv->instance);
  78. u32 ctrl;
  79. spin_lock(&raminit_lock);
  80. ctrl = readl(priv->raminit_ctrlreg);
  81. /* We clear the done and start bit first. The start bit is
  82. * looking at the 0 -> transition, but is not self clearing;
  83. * And we clear the init done bit as well.
  84. */
  85. ctrl &= ~CAN_RAMINIT_START_MASK(priv->instance);
  86. ctrl |= CAN_RAMINIT_DONE_MASK(priv->instance);
  87. writel(ctrl, priv->raminit_ctrlreg);
  88. ctrl &= ~CAN_RAMINIT_DONE_MASK(priv->instance);
  89. c_can_hw_raminit_wait_ti(priv, ctrl, mask);
  90. if (enable) {
  91. /* Set start bit and wait for the done bit. */
  92. ctrl |= CAN_RAMINIT_START_MASK(priv->instance);
  93. writel(ctrl, priv->raminit_ctrlreg);
  94. ctrl |= CAN_RAMINIT_DONE_MASK(priv->instance);
  95. c_can_hw_raminit_wait_ti(priv, ctrl, mask);
  96. }
  97. spin_unlock(&raminit_lock);
  98. }
  99. static u32 c_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
  100. {
  101. u32 val;
  102. val = priv->read_reg(priv, index);
  103. val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
  104. return val;
  105. }
  106. static void c_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
  107. u32 val)
  108. {
  109. priv->write_reg(priv, index + 1, val >> 16);
  110. priv->write_reg(priv, index, val);
  111. }
  112. static u32 d_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
  113. {
  114. return readl(priv->base + priv->regs[index]);
  115. }
  116. static void d_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
  117. u32 val)
  118. {
  119. writel(val, priv->base + priv->regs[index]);
  120. }
  121. static void c_can_hw_raminit_wait(const struct c_can_priv *priv, u32 mask)
  122. {
  123. while (priv->read_reg32(priv, C_CAN_FUNCTION_REG) & mask)
  124. udelay(1);
  125. }
  126. static void c_can_hw_raminit(const struct c_can_priv *priv, bool enable)
  127. {
  128. u32 ctrl;
  129. ctrl = priv->read_reg32(priv, C_CAN_FUNCTION_REG);
  130. ctrl &= ~DCAN_RAM_INIT_BIT;
  131. priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
  132. c_can_hw_raminit_wait(priv, ctrl);
  133. if (enable) {
  134. ctrl |= DCAN_RAM_INIT_BIT;
  135. priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
  136. c_can_hw_raminit_wait(priv, ctrl);
  137. }
  138. }
  139. static struct platform_device_id c_can_id_table[] = {
  140. [BOSCH_C_CAN_PLATFORM] = {
  141. .name = KBUILD_MODNAME,
  142. .driver_data = BOSCH_C_CAN,
  143. },
  144. [BOSCH_C_CAN] = {
  145. .name = "c_can",
  146. .driver_data = BOSCH_C_CAN,
  147. },
  148. [BOSCH_D_CAN] = {
  149. .name = "d_can",
  150. .driver_data = BOSCH_D_CAN,
  151. }, {
  152. }
  153. };
  154. MODULE_DEVICE_TABLE(platform, c_can_id_table);
  155. static const struct of_device_id c_can_of_table[] = {
  156. { .compatible = "bosch,c_can", .data = &c_can_id_table[BOSCH_C_CAN] },
  157. { .compatible = "bosch,d_can", .data = &c_can_id_table[BOSCH_D_CAN] },
  158. { /* sentinel */ },
  159. };
  160. MODULE_DEVICE_TABLE(of, c_can_of_table);
  161. static int c_can_plat_probe(struct platform_device *pdev)
  162. {
  163. int ret;
  164. void __iomem *addr;
  165. struct net_device *dev;
  166. struct c_can_priv *priv;
  167. const struct of_device_id *match;
  168. const struct platform_device_id *id;
  169. struct resource *mem, *res;
  170. int irq;
  171. struct clk *clk;
  172. if (pdev->dev.of_node) {
  173. match = of_match_device(c_can_of_table, &pdev->dev);
  174. if (!match) {
  175. dev_err(&pdev->dev, "Failed to find matching dt id\n");
  176. ret = -EINVAL;
  177. goto exit;
  178. }
  179. id = match->data;
  180. } else {
  181. id = platform_get_device_id(pdev);
  182. }
  183. /* get the appropriate clk */
  184. clk = clk_get(&pdev->dev, NULL);
  185. if (IS_ERR(clk)) {
  186. dev_err(&pdev->dev, "no clock defined\n");
  187. ret = -ENODEV;
  188. goto exit;
  189. }
  190. /* get the platform data */
  191. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  192. irq = platform_get_irq(pdev, 0);
  193. if (!mem || irq <= 0) {
  194. ret = -ENODEV;
  195. goto exit_free_clk;
  196. }
  197. if (!request_mem_region(mem->start, resource_size(mem),
  198. KBUILD_MODNAME)) {
  199. dev_err(&pdev->dev, "resource unavailable\n");
  200. ret = -ENODEV;
  201. goto exit_free_clk;
  202. }
  203. addr = ioremap(mem->start, resource_size(mem));
  204. if (!addr) {
  205. dev_err(&pdev->dev, "failed to map can port\n");
  206. ret = -ENOMEM;
  207. goto exit_release_mem;
  208. }
  209. /* allocate the c_can device */
  210. dev = alloc_c_can_dev();
  211. if (!dev) {
  212. ret = -ENOMEM;
  213. goto exit_iounmap;
  214. }
  215. priv = netdev_priv(dev);
  216. switch (id->driver_data) {
  217. case BOSCH_C_CAN:
  218. priv->regs = reg_map_c_can;
  219. switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
  220. case IORESOURCE_MEM_32BIT:
  221. priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
  222. priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
  223. priv->read_reg32 = c_can_plat_read_reg32;
  224. priv->write_reg32 = c_can_plat_write_reg32;
  225. break;
  226. case IORESOURCE_MEM_16BIT:
  227. default:
  228. priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
  229. priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
  230. priv->read_reg32 = c_can_plat_read_reg32;
  231. priv->write_reg32 = c_can_plat_write_reg32;
  232. break;
  233. }
  234. break;
  235. case BOSCH_D_CAN:
  236. priv->regs = reg_map_d_can;
  237. priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  238. priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
  239. priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
  240. priv->read_reg32 = d_can_plat_read_reg32;
  241. priv->write_reg32 = d_can_plat_write_reg32;
  242. if (pdev->dev.of_node)
  243. priv->instance = of_alias_get_id(pdev->dev.of_node, "d_can");
  244. else
  245. priv->instance = pdev->id;
  246. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  247. /* Not all D_CAN modules have a separate register for the D_CAN
  248. * RAM initialization. Use default RAM init bit in D_CAN module
  249. * if not specified in DT.
  250. */
  251. if (!res) {
  252. priv->raminit = c_can_hw_raminit;
  253. break;
  254. }
  255. priv->raminit_ctrlreg = devm_ioremap_resource(&pdev->dev, res);
  256. if (IS_ERR(priv->raminit_ctrlreg) || priv->instance < 0)
  257. dev_info(&pdev->dev, "control memory is not used for raminit\n");
  258. else
  259. priv->raminit = c_can_hw_raminit_ti;
  260. break;
  261. default:
  262. ret = -EINVAL;
  263. goto exit_free_device;
  264. }
  265. dev->irq = irq;
  266. priv->base = addr;
  267. priv->device = &pdev->dev;
  268. priv->can.clock.freq = clk_get_rate(clk);
  269. priv->priv = clk;
  270. priv->type = id->driver_data;
  271. platform_set_drvdata(pdev, dev);
  272. SET_NETDEV_DEV(dev, &pdev->dev);
  273. ret = register_c_can_dev(dev);
  274. if (ret) {
  275. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  276. KBUILD_MODNAME, ret);
  277. goto exit_free_device;
  278. }
  279. dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
  280. KBUILD_MODNAME, priv->base, dev->irq);
  281. return 0;
  282. exit_free_device:
  283. free_c_can_dev(dev);
  284. exit_iounmap:
  285. iounmap(addr);
  286. exit_release_mem:
  287. release_mem_region(mem->start, resource_size(mem));
  288. exit_free_clk:
  289. clk_put(clk);
  290. exit:
  291. dev_err(&pdev->dev, "probe failed\n");
  292. return ret;
  293. }
  294. static int c_can_plat_remove(struct platform_device *pdev)
  295. {
  296. struct net_device *dev = platform_get_drvdata(pdev);
  297. struct c_can_priv *priv = netdev_priv(dev);
  298. struct resource *mem;
  299. unregister_c_can_dev(dev);
  300. free_c_can_dev(dev);
  301. iounmap(priv->base);
  302. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  303. release_mem_region(mem->start, resource_size(mem));
  304. clk_put(priv->priv);
  305. return 0;
  306. }
  307. #ifdef CONFIG_PM
  308. static int c_can_suspend(struct platform_device *pdev, pm_message_t state)
  309. {
  310. int ret;
  311. struct net_device *ndev = platform_get_drvdata(pdev);
  312. struct c_can_priv *priv = netdev_priv(ndev);
  313. if (priv->type != BOSCH_D_CAN) {
  314. dev_warn(&pdev->dev, "Not supported\n");
  315. return 0;
  316. }
  317. if (netif_running(ndev)) {
  318. netif_stop_queue(ndev);
  319. netif_device_detach(ndev);
  320. }
  321. ret = c_can_power_down(ndev);
  322. if (ret) {
  323. netdev_err(ndev, "failed to enter power down mode\n");
  324. return ret;
  325. }
  326. priv->can.state = CAN_STATE_SLEEPING;
  327. return 0;
  328. }
  329. static int c_can_resume(struct platform_device *pdev)
  330. {
  331. int ret;
  332. struct net_device *ndev = platform_get_drvdata(pdev);
  333. struct c_can_priv *priv = netdev_priv(ndev);
  334. if (priv->type != BOSCH_D_CAN) {
  335. dev_warn(&pdev->dev, "Not supported\n");
  336. return 0;
  337. }
  338. ret = c_can_power_up(ndev);
  339. if (ret) {
  340. netdev_err(ndev, "Still in power down mode\n");
  341. return ret;
  342. }
  343. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  344. if (netif_running(ndev)) {
  345. netif_device_attach(ndev);
  346. netif_start_queue(ndev);
  347. }
  348. return 0;
  349. }
  350. #else
  351. #define c_can_suspend NULL
  352. #define c_can_resume NULL
  353. #endif
  354. static struct platform_driver c_can_plat_driver = {
  355. .driver = {
  356. .name = KBUILD_MODNAME,
  357. .owner = THIS_MODULE,
  358. .of_match_table = c_can_of_table,
  359. },
  360. .probe = c_can_plat_probe,
  361. .remove = c_can_plat_remove,
  362. .suspend = c_can_suspend,
  363. .resume = c_can_resume,
  364. .id_table = c_can_id_table,
  365. };
  366. module_platform_driver(c_can_plat_driver);
  367. MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
  368. MODULE_LICENSE("GPL v2");
  369. MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");