mpc5xxx_can.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * CAN bus driver for the Freescale MPC5xxx embedded CPU.
  3. *
  4. * Copyright (C) 2004-2005 Andrey Volkov <avolkov@varma-el.com>,
  5. * Varma Electronics Oy
  6. * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
  7. * Copyright (C) 2009 Wolfram Sang, Pengutronix <w.sang@pengutronix.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the version 2 of the GNU General Public License
  11. * as published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/can/dev.h>
  27. #include <linux/of_platform.h>
  28. #include <sysdev/fsl_soc.h>
  29. #include <linux/clk.h>
  30. #include <linux/io.h>
  31. #include <asm/mpc52xx.h>
  32. #include "mscan.h"
  33. #define DRV_NAME "mpc5xxx_can"
  34. struct mpc5xxx_can_data {
  35. unsigned int type;
  36. u32 (*get_clock)(struct platform_device *ofdev, const char *clock_name,
  37. int *mscan_clksrc);
  38. void (*put_clock)(struct platform_device *ofdev);
  39. };
  40. #ifdef CONFIG_PPC_MPC52xx
  41. static const struct of_device_id mpc52xx_cdm_ids[] = {
  42. { .compatible = "fsl,mpc5200-cdm", },
  43. {}
  44. };
  45. static u32 mpc52xx_can_get_clock(struct platform_device *ofdev,
  46. const char *clock_name, int *mscan_clksrc)
  47. {
  48. unsigned int pvr;
  49. struct mpc52xx_cdm __iomem *cdm;
  50. struct device_node *np_cdm;
  51. unsigned int freq;
  52. u32 val;
  53. pvr = mfspr(SPRN_PVR);
  54. /*
  55. * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock
  56. * (IP_CLK) can be selected as MSCAN clock source. According to
  57. * the MPC5200 user's manual, the oscillator clock is the better
  58. * choice as it has less jitter. For this reason, it is selected
  59. * by default. Unfortunately, it can not be selected for the old
  60. * MPC5200 Rev. A chips due to a hardware bug (check errata).
  61. */
  62. if (clock_name && strcmp(clock_name, "ip") == 0)
  63. *mscan_clksrc = MSCAN_CLKSRC_BUS;
  64. else
  65. *mscan_clksrc = MSCAN_CLKSRC_XTAL;
  66. freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
  67. if (!freq)
  68. return 0;
  69. if (*mscan_clksrc == MSCAN_CLKSRC_BUS || pvr == 0x80822011)
  70. return freq;
  71. /* Determine SYS_XTAL_IN frequency from the clock domain settings */
  72. np_cdm = of_find_matching_node(NULL, mpc52xx_cdm_ids);
  73. if (!np_cdm) {
  74. dev_err(&ofdev->dev, "can't get clock node!\n");
  75. return 0;
  76. }
  77. cdm = of_iomap(np_cdm, 0);
  78. if (!cdm) {
  79. of_node_put(np_cdm);
  80. dev_err(&ofdev->dev, "can't map clock node!\n");
  81. return 0;
  82. }
  83. if (in_8(&cdm->ipb_clk_sel) & 0x1)
  84. freq *= 2;
  85. val = in_be32(&cdm->rstcfg);
  86. freq *= (val & (1 << 5)) ? 8 : 4;
  87. freq /= (val & (1 << 6)) ? 12 : 16;
  88. of_node_put(np_cdm);
  89. iounmap(cdm);
  90. return freq;
  91. }
  92. #else /* !CONFIG_PPC_MPC52xx */
  93. static u32 mpc52xx_can_get_clock(struct platform_device *ofdev,
  94. const char *clock_name, int *mscan_clksrc)
  95. {
  96. return 0;
  97. }
  98. #endif /* CONFIG_PPC_MPC52xx */
  99. #ifdef CONFIG_PPC_MPC512x
  100. static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
  101. const char *clock_source, int *mscan_clksrc)
  102. {
  103. struct device_node *np;
  104. u32 clockdiv;
  105. enum {
  106. CLK_FROM_AUTO,
  107. CLK_FROM_IPS,
  108. CLK_FROM_SYS,
  109. CLK_FROM_REF,
  110. } clk_from;
  111. struct clk *clk_in, *clk_can;
  112. unsigned long freq_calc;
  113. struct mscan_priv *priv;
  114. struct clk *clk_ipg;
  115. /* the caller passed in the clock source spec that was read from
  116. * the device tree, get the optional clock divider as well
  117. */
  118. np = ofdev->dev.of_node;
  119. clockdiv = 1;
  120. of_property_read_u32(np, "fsl,mscan-clock-divider", &clockdiv);
  121. dev_dbg(&ofdev->dev, "device tree specs: clk src[%s] div[%d]\n",
  122. clock_source ? clock_source : "<NULL>", clockdiv);
  123. /* when clock-source is 'ip', the CANCTL1[CLKSRC] bit needs to
  124. * get set, and the 'ips' clock is the input to the MSCAN
  125. * component
  126. *
  127. * for clock-source values of 'ref' or 'sys' the CANCTL1[CLKSRC]
  128. * bit needs to get cleared, an optional clock-divider may have
  129. * been specified (the default value is 1), the appropriate
  130. * MSCAN related MCLK is the input to the MSCAN component
  131. *
  132. * in the absence of a clock-source spec, first an optimal clock
  133. * gets determined based on the 'sys' clock, if that fails the
  134. * 'ref' clock is used
  135. */
  136. clk_from = CLK_FROM_AUTO;
  137. if (clock_source) {
  138. /* interpret the device tree's spec for the clock source */
  139. if (!strcmp(clock_source, "ip"))
  140. clk_from = CLK_FROM_IPS;
  141. else if (!strcmp(clock_source, "sys"))
  142. clk_from = CLK_FROM_SYS;
  143. else if (!strcmp(clock_source, "ref"))
  144. clk_from = CLK_FROM_REF;
  145. else
  146. goto err_invalid;
  147. dev_dbg(&ofdev->dev, "got a clk source spec[%d]\n", clk_from);
  148. }
  149. if (clk_from == CLK_FROM_AUTO) {
  150. /* no spec so far, try the 'sys' clock; round to the
  151. * next MHz and see if we can get a multiple of 16MHz
  152. */
  153. dev_dbg(&ofdev->dev, "no clk source spec, trying SYS\n");
  154. clk_in = devm_clk_get(&ofdev->dev, "sys");
  155. if (IS_ERR(clk_in))
  156. goto err_notavail;
  157. freq_calc = clk_get_rate(clk_in);
  158. freq_calc += 499999;
  159. freq_calc /= 1000000;
  160. freq_calc *= 1000000;
  161. if ((freq_calc % 16000000) == 0) {
  162. clk_from = CLK_FROM_SYS;
  163. clockdiv = freq_calc / 16000000;
  164. dev_dbg(&ofdev->dev,
  165. "clk fit, sys[%lu] div[%d] freq[%lu]\n",
  166. freq_calc, clockdiv, freq_calc / clockdiv);
  167. }
  168. }
  169. if (clk_from == CLK_FROM_AUTO) {
  170. /* no spec so far, use the 'ref' clock */
  171. dev_dbg(&ofdev->dev, "no clk source spec, trying REF\n");
  172. clk_in = devm_clk_get(&ofdev->dev, "ref");
  173. if (IS_ERR(clk_in))
  174. goto err_notavail;
  175. clk_from = CLK_FROM_REF;
  176. freq_calc = clk_get_rate(clk_in);
  177. dev_dbg(&ofdev->dev,
  178. "clk fit, ref[%lu] (no div) freq[%lu]\n",
  179. freq_calc, freq_calc);
  180. }
  181. /* select IPS or MCLK as the MSCAN input (returned to the caller),
  182. * setup the MCLK mux source and rate if applicable, apply the
  183. * optionally specified or derived above divider, and determine
  184. * the actual resulting clock rate to return to the caller
  185. */
  186. switch (clk_from) {
  187. case CLK_FROM_IPS:
  188. clk_can = devm_clk_get(&ofdev->dev, "ips");
  189. if (IS_ERR(clk_can))
  190. goto err_notavail;
  191. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  192. priv->clk_can = clk_can;
  193. freq_calc = clk_get_rate(clk_can);
  194. *mscan_clksrc = MSCAN_CLKSRC_IPS;
  195. dev_dbg(&ofdev->dev, "clk from IPS, clksrc[%d] freq[%lu]\n",
  196. *mscan_clksrc, freq_calc);
  197. break;
  198. case CLK_FROM_SYS:
  199. case CLK_FROM_REF:
  200. clk_can = devm_clk_get(&ofdev->dev, "mclk");
  201. if (IS_ERR(clk_can))
  202. goto err_notavail;
  203. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  204. priv->clk_can = clk_can;
  205. if (clk_from == CLK_FROM_SYS)
  206. clk_in = devm_clk_get(&ofdev->dev, "sys");
  207. if (clk_from == CLK_FROM_REF)
  208. clk_in = devm_clk_get(&ofdev->dev, "ref");
  209. if (IS_ERR(clk_in))
  210. goto err_notavail;
  211. clk_set_parent(clk_can, clk_in);
  212. freq_calc = clk_get_rate(clk_in);
  213. freq_calc /= clockdiv;
  214. clk_set_rate(clk_can, freq_calc);
  215. freq_calc = clk_get_rate(clk_can);
  216. *mscan_clksrc = MSCAN_CLKSRC_BUS;
  217. dev_dbg(&ofdev->dev, "clk from MCLK, clksrc[%d] freq[%lu]\n",
  218. *mscan_clksrc, freq_calc);
  219. break;
  220. default:
  221. goto err_invalid;
  222. }
  223. /* the above clk_can item is used for the bitrate, access to
  224. * the peripheral's register set needs the clk_ipg item
  225. */
  226. clk_ipg = devm_clk_get(&ofdev->dev, "ipg");
  227. if (IS_ERR(clk_ipg))
  228. goto err_notavail_ipg;
  229. if (clk_prepare_enable(clk_ipg))
  230. goto err_notavail_ipg;
  231. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  232. priv->clk_ipg = clk_ipg;
  233. /* return the determined clock source rate */
  234. return freq_calc;
  235. err_invalid:
  236. dev_err(&ofdev->dev, "invalid clock source specification\n");
  237. /* clock source rate could not get determined */
  238. return 0;
  239. err_notavail:
  240. dev_err(&ofdev->dev, "cannot acquire or setup bitrate clock source\n");
  241. /* clock source rate could not get determined */
  242. return 0;
  243. err_notavail_ipg:
  244. dev_err(&ofdev->dev, "cannot acquire or setup register clock\n");
  245. /* clock source rate could not get determined */
  246. return 0;
  247. }
  248. static void mpc512x_can_put_clock(struct platform_device *ofdev)
  249. {
  250. struct mscan_priv *priv;
  251. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  252. if (priv->clk_ipg)
  253. clk_disable_unprepare(priv->clk_ipg);
  254. }
  255. #else /* !CONFIG_PPC_MPC512x */
  256. static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
  257. const char *clock_name, int *mscan_clksrc)
  258. {
  259. return 0;
  260. }
  261. #define mpc512x_can_put_clock NULL
  262. #endif /* CONFIG_PPC_MPC512x */
  263. static const struct of_device_id mpc5xxx_can_table[];
  264. static int mpc5xxx_can_probe(struct platform_device *ofdev)
  265. {
  266. const struct of_device_id *match;
  267. const struct mpc5xxx_can_data *data;
  268. struct device_node *np = ofdev->dev.of_node;
  269. struct net_device *dev;
  270. struct mscan_priv *priv;
  271. void __iomem *base;
  272. const char *clock_name = NULL;
  273. int irq, mscan_clksrc = 0;
  274. int err = -ENOMEM;
  275. match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
  276. if (!match)
  277. return -EINVAL;
  278. data = match->data;
  279. base = of_iomap(np, 0);
  280. if (!base) {
  281. dev_err(&ofdev->dev, "couldn't ioremap\n");
  282. return err;
  283. }
  284. irq = irq_of_parse_and_map(np, 0);
  285. if (!irq) {
  286. dev_err(&ofdev->dev, "no irq found\n");
  287. err = -ENODEV;
  288. goto exit_unmap_mem;
  289. }
  290. dev = alloc_mscandev();
  291. if (!dev)
  292. goto exit_dispose_irq;
  293. platform_set_drvdata(ofdev, dev);
  294. SET_NETDEV_DEV(dev, &ofdev->dev);
  295. priv = netdev_priv(dev);
  296. priv->reg_base = base;
  297. dev->irq = irq;
  298. clock_name = of_get_property(np, "fsl,mscan-clock-source", NULL);
  299. BUG_ON(!data);
  300. priv->type = data->type;
  301. priv->can.clock.freq = data->get_clock(ofdev, clock_name,
  302. &mscan_clksrc);
  303. if (!priv->can.clock.freq) {
  304. dev_err(&ofdev->dev, "couldn't get MSCAN clock properties\n");
  305. goto exit_free_mscan;
  306. }
  307. err = register_mscandev(dev, mscan_clksrc);
  308. if (err) {
  309. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  310. DRV_NAME, err);
  311. goto exit_free_mscan;
  312. }
  313. dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n",
  314. priv->reg_base, dev->irq, priv->can.clock.freq);
  315. return 0;
  316. exit_free_mscan:
  317. free_candev(dev);
  318. exit_dispose_irq:
  319. irq_dispose_mapping(irq);
  320. exit_unmap_mem:
  321. iounmap(base);
  322. return err;
  323. }
  324. static int mpc5xxx_can_remove(struct platform_device *ofdev)
  325. {
  326. const struct of_device_id *match;
  327. const struct mpc5xxx_can_data *data;
  328. struct net_device *dev = platform_get_drvdata(ofdev);
  329. struct mscan_priv *priv = netdev_priv(dev);
  330. match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
  331. data = match ? match->data : NULL;
  332. unregister_mscandev(dev);
  333. if (data && data->put_clock)
  334. data->put_clock(ofdev);
  335. iounmap(priv->reg_base);
  336. irq_dispose_mapping(dev->irq);
  337. free_candev(dev);
  338. return 0;
  339. }
  340. #ifdef CONFIG_PM
  341. static struct mscan_regs saved_regs;
  342. static int mpc5xxx_can_suspend(struct platform_device *ofdev, pm_message_t state)
  343. {
  344. struct net_device *dev = platform_get_drvdata(ofdev);
  345. struct mscan_priv *priv = netdev_priv(dev);
  346. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  347. _memcpy_fromio(&saved_regs, regs, sizeof(*regs));
  348. return 0;
  349. }
  350. static int mpc5xxx_can_resume(struct platform_device *ofdev)
  351. {
  352. struct net_device *dev = platform_get_drvdata(ofdev);
  353. struct mscan_priv *priv = netdev_priv(dev);
  354. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  355. regs->canctl0 |= MSCAN_INITRQ;
  356. while (!(regs->canctl1 & MSCAN_INITAK))
  357. udelay(10);
  358. regs->canctl1 = saved_regs.canctl1;
  359. regs->canbtr0 = saved_regs.canbtr0;
  360. regs->canbtr1 = saved_regs.canbtr1;
  361. regs->canidac = saved_regs.canidac;
  362. /* restore masks, buffers etc. */
  363. _memcpy_toio(&regs->canidar1_0, (void *)&saved_regs.canidar1_0,
  364. sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));
  365. regs->canctl0 &= ~MSCAN_INITRQ;
  366. regs->cantbsel = saved_regs.cantbsel;
  367. regs->canrier = saved_regs.canrier;
  368. regs->cantier = saved_regs.cantier;
  369. regs->canctl0 = saved_regs.canctl0;
  370. return 0;
  371. }
  372. #endif
  373. static const struct mpc5xxx_can_data mpc5200_can_data = {
  374. .type = MSCAN_TYPE_MPC5200,
  375. .get_clock = mpc52xx_can_get_clock,
  376. /* .put_clock not applicable */
  377. };
  378. static const struct mpc5xxx_can_data mpc5121_can_data = {
  379. .type = MSCAN_TYPE_MPC5121,
  380. .get_clock = mpc512x_can_get_clock,
  381. .put_clock = mpc512x_can_put_clock,
  382. };
  383. static const struct of_device_id mpc5xxx_can_table[] = {
  384. { .compatible = "fsl,mpc5200-mscan", .data = &mpc5200_can_data, },
  385. /* Note that only MPC5121 Rev. 2 (and later) is supported */
  386. { .compatible = "fsl,mpc5121-mscan", .data = &mpc5121_can_data, },
  387. {},
  388. };
  389. MODULE_DEVICE_TABLE(of, mpc5xxx_can_table);
  390. static struct platform_driver mpc5xxx_can_driver = {
  391. .driver = {
  392. .name = "mpc5xxx_can",
  393. .of_match_table = mpc5xxx_can_table,
  394. },
  395. .probe = mpc5xxx_can_probe,
  396. .remove = mpc5xxx_can_remove,
  397. #ifdef CONFIG_PM
  398. .suspend = mpc5xxx_can_suspend,
  399. .resume = mpc5xxx_can_resume,
  400. #endif
  401. };
  402. module_platform_driver(mpc5xxx_can_driver);
  403. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  404. MODULE_DESCRIPTION("Freescale MPC5xxx CAN driver");
  405. MODULE_LICENSE("GPL v2");