c_can_platform.c 12 KB

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