c_can_platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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/mfd/syscon.h>
  35. #include <linux/regmap.h>
  36. #include <linux/can/dev.h>
  37. #include "c_can.h"
  38. #define DCAN_RAM_INIT_BIT (1 << 3)
  39. static DEFINE_SPINLOCK(raminit_lock);
  40. /*
  41. * 16-bit c_can registers can be arranged differently in the memory
  42. * architecture of different implementations. For example: 16-bit
  43. * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
  44. * Handle the same by providing a common read/write interface.
  45. */
  46. static u16 c_can_plat_read_reg_aligned_to_16bit(const struct c_can_priv *priv,
  47. enum reg index)
  48. {
  49. return readw(priv->base + priv->regs[index]);
  50. }
  51. static void c_can_plat_write_reg_aligned_to_16bit(const struct c_can_priv *priv,
  52. enum reg index, u16 val)
  53. {
  54. writew(val, priv->base + priv->regs[index]);
  55. }
  56. static u16 c_can_plat_read_reg_aligned_to_32bit(const struct c_can_priv *priv,
  57. enum reg index)
  58. {
  59. return readw(priv->base + 2 * priv->regs[index]);
  60. }
  61. static void c_can_plat_write_reg_aligned_to_32bit(const struct c_can_priv *priv,
  62. enum reg index, u16 val)
  63. {
  64. writew(val, priv->base + 2 * priv->regs[index]);
  65. }
  66. static void c_can_hw_raminit_wait_syscon(const struct c_can_priv *priv,
  67. u32 mask, u32 val)
  68. {
  69. const struct c_can_raminit *raminit = &priv->raminit_sys;
  70. int timeout = 0;
  71. u32 ctrl = 0;
  72. /* We look only at the bits of our instance. */
  73. val &= mask;
  74. do {
  75. udelay(1);
  76. timeout++;
  77. regmap_read(raminit->syscon, raminit->reg, &ctrl);
  78. if (timeout == 1000) {
  79. dev_err(&priv->dev->dev, "%s: time out\n", __func__);
  80. break;
  81. }
  82. } while ((ctrl & mask) != val);
  83. }
  84. static void c_can_hw_raminit_syscon(const struct c_can_priv *priv, bool enable)
  85. {
  86. const struct c_can_raminit *raminit = &priv->raminit_sys;
  87. u32 ctrl = 0;
  88. u32 mask;
  89. spin_lock(&raminit_lock);
  90. mask = 1 << raminit->bits.start | 1 << raminit->bits.done;
  91. regmap_read(raminit->syscon, raminit->reg, &ctrl);
  92. /* We clear the done and start bit first. The start bit is
  93. * looking at the 0 -> transition, but is not self clearing;
  94. * And we clear the init done bit as well.
  95. * NOTE: DONE must be written with 1 to clear it.
  96. */
  97. ctrl &= ~(1 << raminit->bits.start);
  98. ctrl |= 1 << raminit->bits.done;
  99. regmap_write(raminit->syscon, raminit->reg, ctrl);
  100. ctrl &= ~(1 << raminit->bits.done);
  101. c_can_hw_raminit_wait_syscon(priv, mask, ctrl);
  102. if (enable) {
  103. /* Set start bit and wait for the done bit. */
  104. ctrl |= 1 << raminit->bits.start;
  105. regmap_write(raminit->syscon, raminit->reg, ctrl);
  106. /* clear START bit if start pulse is needed */
  107. if (raminit->needs_pulse) {
  108. ctrl &= ~(1 << raminit->bits.start);
  109. regmap_write(raminit->syscon, raminit->reg, ctrl);
  110. }
  111. ctrl |= 1 << raminit->bits.done;
  112. c_can_hw_raminit_wait_syscon(priv, mask, ctrl);
  113. }
  114. spin_unlock(&raminit_lock);
  115. }
  116. static u32 c_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
  117. {
  118. u32 val;
  119. val = priv->read_reg(priv, index);
  120. val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
  121. return val;
  122. }
  123. static void c_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
  124. u32 val)
  125. {
  126. priv->write_reg(priv, index + 1, val >> 16);
  127. priv->write_reg(priv, index, val);
  128. }
  129. static u32 d_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
  130. {
  131. return readl(priv->base + priv->regs[index]);
  132. }
  133. static void d_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
  134. u32 val)
  135. {
  136. writel(val, priv->base + priv->regs[index]);
  137. }
  138. static void c_can_hw_raminit_wait(const struct c_can_priv *priv, u32 mask)
  139. {
  140. while (priv->read_reg32(priv, C_CAN_FUNCTION_REG) & mask)
  141. udelay(1);
  142. }
  143. static void c_can_hw_raminit(const struct c_can_priv *priv, bool enable)
  144. {
  145. u32 ctrl;
  146. ctrl = priv->read_reg32(priv, C_CAN_FUNCTION_REG);
  147. ctrl &= ~DCAN_RAM_INIT_BIT;
  148. priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
  149. c_can_hw_raminit_wait(priv, ctrl);
  150. if (enable) {
  151. ctrl |= DCAN_RAM_INIT_BIT;
  152. priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
  153. c_can_hw_raminit_wait(priv, ctrl);
  154. }
  155. }
  156. static const struct c_can_driver_data c_can_drvdata = {
  157. .id = BOSCH_C_CAN,
  158. };
  159. static const struct c_can_driver_data d_can_drvdata = {
  160. .id = BOSCH_D_CAN,
  161. };
  162. static const struct raminit_bits dra7_raminit_bits[] = {
  163. [0] = { .start = 3, .done = 1, },
  164. [1] = { .start = 5, .done = 2, },
  165. };
  166. static const struct c_can_driver_data dra7_dcan_drvdata = {
  167. .id = BOSCH_D_CAN,
  168. .raminit_num = ARRAY_SIZE(dra7_raminit_bits),
  169. .raminit_bits = dra7_raminit_bits,
  170. .raminit_pulse = true,
  171. };
  172. static const struct raminit_bits am3352_raminit_bits[] = {
  173. [0] = { .start = 0, .done = 8, },
  174. [1] = { .start = 1, .done = 9, },
  175. };
  176. static const struct c_can_driver_data am3352_dcan_drvdata = {
  177. .id = BOSCH_D_CAN,
  178. .raminit_num = ARRAY_SIZE(am3352_raminit_bits),
  179. .raminit_bits = am3352_raminit_bits,
  180. };
  181. static struct platform_device_id c_can_id_table[] = {
  182. {
  183. .name = KBUILD_MODNAME,
  184. .driver_data = (kernel_ulong_t)&c_can_drvdata,
  185. },
  186. {
  187. .name = "c_can",
  188. .driver_data = (kernel_ulong_t)&c_can_drvdata,
  189. },
  190. {
  191. .name = "d_can",
  192. .driver_data = (kernel_ulong_t)&d_can_drvdata,
  193. },
  194. { /* sentinel */ },
  195. };
  196. MODULE_DEVICE_TABLE(platform, c_can_id_table);
  197. static const struct of_device_id c_can_of_table[] = {
  198. { .compatible = "bosch,c_can", .data = &c_can_drvdata },
  199. { .compatible = "bosch,d_can", .data = &d_can_drvdata },
  200. { .compatible = "ti,dra7-d_can", .data = &dra7_dcan_drvdata },
  201. { .compatible = "ti,am3352-d_can", .data = &am3352_dcan_drvdata },
  202. { .compatible = "ti,am4372-d_can", .data = &am3352_dcan_drvdata },
  203. { /* sentinel */ },
  204. };
  205. MODULE_DEVICE_TABLE(of, c_can_of_table);
  206. static int c_can_plat_probe(struct platform_device *pdev)
  207. {
  208. int ret;
  209. void __iomem *addr;
  210. struct net_device *dev;
  211. struct c_can_priv *priv;
  212. const struct of_device_id *match;
  213. struct resource *mem;
  214. int irq;
  215. struct clk *clk;
  216. const struct c_can_driver_data *drvdata;
  217. struct device_node *np = pdev->dev.of_node;
  218. match = of_match_device(c_can_of_table, &pdev->dev);
  219. if (match) {
  220. drvdata = match->data;
  221. } else if (pdev->id_entry->driver_data) {
  222. drvdata = (struct c_can_driver_data *)
  223. platform_get_device_id(pdev)->driver_data;
  224. } else {
  225. return -ENODEV;
  226. }
  227. /* get the appropriate clk */
  228. clk = devm_clk_get(&pdev->dev, NULL);
  229. if (IS_ERR(clk)) {
  230. ret = PTR_ERR(clk);
  231. goto exit;
  232. }
  233. /* get the platform data */
  234. irq = platform_get_irq(pdev, 0);
  235. if (irq <= 0) {
  236. ret = -ENODEV;
  237. goto exit;
  238. }
  239. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. addr = devm_ioremap_resource(&pdev->dev, mem);
  241. if (IS_ERR(addr)) {
  242. ret = PTR_ERR(addr);
  243. goto exit;
  244. }
  245. /* allocate the c_can device */
  246. dev = alloc_c_can_dev();
  247. if (!dev) {
  248. ret = -ENOMEM;
  249. goto exit;
  250. }
  251. priv = netdev_priv(dev);
  252. switch (drvdata->id) {
  253. case BOSCH_C_CAN:
  254. priv->regs = reg_map_c_can;
  255. switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
  256. case IORESOURCE_MEM_32BIT:
  257. priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
  258. priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
  259. priv->read_reg32 = c_can_plat_read_reg32;
  260. priv->write_reg32 = c_can_plat_write_reg32;
  261. break;
  262. case IORESOURCE_MEM_16BIT:
  263. default:
  264. priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
  265. priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
  266. priv->read_reg32 = c_can_plat_read_reg32;
  267. priv->write_reg32 = c_can_plat_write_reg32;
  268. break;
  269. }
  270. break;
  271. case BOSCH_D_CAN:
  272. priv->regs = reg_map_d_can;
  273. priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
  274. priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
  275. priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
  276. priv->read_reg32 = d_can_plat_read_reg32;
  277. priv->write_reg32 = d_can_plat_write_reg32;
  278. /* Check if we need custom RAMINIT via syscon. Mostly for TI
  279. * platforms. Only supported with DT boot.
  280. */
  281. if (np && of_property_read_bool(np, "syscon-raminit")) {
  282. u32 id;
  283. struct c_can_raminit *raminit = &priv->raminit_sys;
  284. ret = -EINVAL;
  285. raminit->syscon = syscon_regmap_lookup_by_phandle(np,
  286. "syscon-raminit");
  287. if (IS_ERR(raminit->syscon)) {
  288. /* can fail with -EPROBE_DEFER */
  289. ret = PTR_ERR(raminit->syscon);
  290. free_c_can_dev(dev);
  291. return ret;
  292. }
  293. if (of_property_read_u32_index(np, "syscon-raminit", 1,
  294. &raminit->reg)) {
  295. dev_err(&pdev->dev,
  296. "couldn't get the RAMINIT reg. offset!\n");
  297. goto exit_free_device;
  298. }
  299. if (of_property_read_u32_index(np, "syscon-raminit", 2,
  300. &id)) {
  301. dev_err(&pdev->dev,
  302. "couldn't get the CAN instance ID\n");
  303. goto exit_free_device;
  304. }
  305. if (id >= drvdata->raminit_num) {
  306. dev_err(&pdev->dev,
  307. "Invalid CAN instance ID\n");
  308. goto exit_free_device;
  309. }
  310. raminit->bits = drvdata->raminit_bits[id];
  311. raminit->needs_pulse = drvdata->raminit_pulse;
  312. priv->raminit = c_can_hw_raminit_syscon;
  313. } else {
  314. priv->raminit = c_can_hw_raminit;
  315. }
  316. break;
  317. default:
  318. ret = -EINVAL;
  319. goto exit_free_device;
  320. }
  321. dev->irq = irq;
  322. priv->base = addr;
  323. priv->device = &pdev->dev;
  324. priv->can.clock.freq = clk_get_rate(clk);
  325. priv->priv = clk;
  326. priv->type = drvdata->id;
  327. platform_set_drvdata(pdev, dev);
  328. SET_NETDEV_DEV(dev, &pdev->dev);
  329. ret = register_c_can_dev(dev);
  330. if (ret) {
  331. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  332. KBUILD_MODNAME, ret);
  333. goto exit_free_device;
  334. }
  335. dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
  336. KBUILD_MODNAME, priv->base, dev->irq);
  337. return 0;
  338. exit_free_device:
  339. free_c_can_dev(dev);
  340. exit:
  341. dev_err(&pdev->dev, "probe failed\n");
  342. return ret;
  343. }
  344. static int c_can_plat_remove(struct platform_device *pdev)
  345. {
  346. struct net_device *dev = platform_get_drvdata(pdev);
  347. unregister_c_can_dev(dev);
  348. free_c_can_dev(dev);
  349. return 0;
  350. }
  351. #ifdef CONFIG_PM
  352. static int c_can_suspend(struct platform_device *pdev, pm_message_t state)
  353. {
  354. int ret;
  355. struct net_device *ndev = platform_get_drvdata(pdev);
  356. struct c_can_priv *priv = netdev_priv(ndev);
  357. if (priv->type != BOSCH_D_CAN) {
  358. dev_warn(&pdev->dev, "Not supported\n");
  359. return 0;
  360. }
  361. if (netif_running(ndev)) {
  362. netif_stop_queue(ndev);
  363. netif_device_detach(ndev);
  364. }
  365. ret = c_can_power_down(ndev);
  366. if (ret) {
  367. netdev_err(ndev, "failed to enter power down mode\n");
  368. return ret;
  369. }
  370. priv->can.state = CAN_STATE_SLEEPING;
  371. return 0;
  372. }
  373. static int c_can_resume(struct platform_device *pdev)
  374. {
  375. int ret;
  376. struct net_device *ndev = platform_get_drvdata(pdev);
  377. struct c_can_priv *priv = netdev_priv(ndev);
  378. if (priv->type != BOSCH_D_CAN) {
  379. dev_warn(&pdev->dev, "Not supported\n");
  380. return 0;
  381. }
  382. ret = c_can_power_up(ndev);
  383. if (ret) {
  384. netdev_err(ndev, "Still in power down mode\n");
  385. return ret;
  386. }
  387. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  388. if (netif_running(ndev)) {
  389. netif_device_attach(ndev);
  390. netif_start_queue(ndev);
  391. }
  392. return 0;
  393. }
  394. #else
  395. #define c_can_suspend NULL
  396. #define c_can_resume NULL
  397. #endif
  398. static struct platform_driver c_can_plat_driver = {
  399. .driver = {
  400. .name = KBUILD_MODNAME,
  401. .of_match_table = c_can_of_table,
  402. },
  403. .probe = c_can_plat_probe,
  404. .remove = c_can_plat_remove,
  405. .suspend = c_can_suspend,
  406. .resume = c_can_resume,
  407. .id_table = c_can_id_table,
  408. };
  409. module_platform_driver(c_can_plat_driver);
  410. MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
  411. MODULE_LICENSE("GPL v2");
  412. MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");