mpc5xxx_can.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 (in_8(&cdm->ipb_clk_sel) & 0x1)
  79. freq *= 2;
  80. val = in_be32(&cdm->rstcfg);
  81. freq *= (val & (1 << 5)) ? 8 : 4;
  82. freq /= (val & (1 << 6)) ? 12 : 16;
  83. of_node_put(np_cdm);
  84. iounmap(cdm);
  85. return freq;
  86. }
  87. #else /* !CONFIG_PPC_MPC52xx */
  88. static u32 mpc52xx_can_get_clock(struct platform_device *ofdev,
  89. const char *clock_name, int *mscan_clksrc)
  90. {
  91. return 0;
  92. }
  93. #endif /* CONFIG_PPC_MPC52xx */
  94. #ifdef CONFIG_PPC_MPC512x
  95. static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
  96. const char *clock_source, int *mscan_clksrc)
  97. {
  98. struct device_node *np;
  99. u32 clockdiv;
  100. enum {
  101. CLK_FROM_AUTO,
  102. CLK_FROM_IPS,
  103. CLK_FROM_SYS,
  104. CLK_FROM_REF,
  105. } clk_from;
  106. struct clk *clk_in, *clk_can;
  107. unsigned long freq_calc;
  108. struct mscan_priv *priv;
  109. struct clk *clk_ipg;
  110. /* the caller passed in the clock source spec that was read from
  111. * the device tree, get the optional clock divider as well
  112. */
  113. np = ofdev->dev.of_node;
  114. clockdiv = 1;
  115. of_property_read_u32(np, "fsl,mscan-clock-divider", &clockdiv);
  116. dev_dbg(&ofdev->dev, "device tree specs: clk src[%s] div[%d]\n",
  117. clock_source ? clock_source : "<NULL>", clockdiv);
  118. /* when clock-source is 'ip', the CANCTL1[CLKSRC] bit needs to
  119. * get set, and the 'ips' clock is the input to the MSCAN
  120. * component
  121. *
  122. * for clock-source values of 'ref' or 'sys' the CANCTL1[CLKSRC]
  123. * bit needs to get cleared, an optional clock-divider may have
  124. * been specified (the default value is 1), the appropriate
  125. * MSCAN related MCLK is the input to the MSCAN component
  126. *
  127. * in the absence of a clock-source spec, first an optimal clock
  128. * gets determined based on the 'sys' clock, if that fails the
  129. * 'ref' clock is used
  130. */
  131. clk_from = CLK_FROM_AUTO;
  132. if (clock_source) {
  133. /* interpret the device tree's spec for the clock source */
  134. if (!strcmp(clock_source, "ip"))
  135. clk_from = CLK_FROM_IPS;
  136. else if (!strcmp(clock_source, "sys"))
  137. clk_from = CLK_FROM_SYS;
  138. else if (!strcmp(clock_source, "ref"))
  139. clk_from = CLK_FROM_REF;
  140. else
  141. goto err_invalid;
  142. dev_dbg(&ofdev->dev, "got a clk source spec[%d]\n", clk_from);
  143. }
  144. if (clk_from == CLK_FROM_AUTO) {
  145. /* no spec so far, try the 'sys' clock; round to the
  146. * next MHz and see if we can get a multiple of 16MHz
  147. */
  148. dev_dbg(&ofdev->dev, "no clk source spec, trying SYS\n");
  149. clk_in = devm_clk_get(&ofdev->dev, "sys");
  150. if (IS_ERR(clk_in))
  151. goto err_notavail;
  152. freq_calc = clk_get_rate(clk_in);
  153. freq_calc += 499999;
  154. freq_calc /= 1000000;
  155. freq_calc *= 1000000;
  156. if ((freq_calc % 16000000) == 0) {
  157. clk_from = CLK_FROM_SYS;
  158. clockdiv = freq_calc / 16000000;
  159. dev_dbg(&ofdev->dev,
  160. "clk fit, sys[%lu] div[%d] freq[%lu]\n",
  161. freq_calc, clockdiv, freq_calc / clockdiv);
  162. }
  163. }
  164. if (clk_from == CLK_FROM_AUTO) {
  165. /* no spec so far, use the 'ref' clock */
  166. dev_dbg(&ofdev->dev, "no clk source spec, trying REF\n");
  167. clk_in = devm_clk_get(&ofdev->dev, "ref");
  168. if (IS_ERR(clk_in))
  169. goto err_notavail;
  170. clk_from = CLK_FROM_REF;
  171. freq_calc = clk_get_rate(clk_in);
  172. dev_dbg(&ofdev->dev,
  173. "clk fit, ref[%lu] (no div) freq[%lu]\n",
  174. freq_calc, freq_calc);
  175. }
  176. /* select IPS or MCLK as the MSCAN input (returned to the caller),
  177. * setup the MCLK mux source and rate if applicable, apply the
  178. * optionally specified or derived above divider, and determine
  179. * the actual resulting clock rate to return to the caller
  180. */
  181. switch (clk_from) {
  182. case CLK_FROM_IPS:
  183. clk_can = devm_clk_get(&ofdev->dev, "ips");
  184. if (IS_ERR(clk_can))
  185. goto err_notavail;
  186. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  187. priv->clk_can = clk_can;
  188. freq_calc = clk_get_rate(clk_can);
  189. *mscan_clksrc = MSCAN_CLKSRC_IPS;
  190. dev_dbg(&ofdev->dev, "clk from IPS, clksrc[%d] freq[%lu]\n",
  191. *mscan_clksrc, freq_calc);
  192. break;
  193. case CLK_FROM_SYS:
  194. case CLK_FROM_REF:
  195. clk_can = devm_clk_get(&ofdev->dev, "mclk");
  196. if (IS_ERR(clk_can))
  197. goto err_notavail;
  198. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  199. priv->clk_can = clk_can;
  200. if (clk_from == CLK_FROM_SYS)
  201. clk_in = devm_clk_get(&ofdev->dev, "sys");
  202. if (clk_from == CLK_FROM_REF)
  203. clk_in = devm_clk_get(&ofdev->dev, "ref");
  204. if (IS_ERR(clk_in))
  205. goto err_notavail;
  206. clk_set_parent(clk_can, clk_in);
  207. freq_calc = clk_get_rate(clk_in);
  208. freq_calc /= clockdiv;
  209. clk_set_rate(clk_can, freq_calc);
  210. freq_calc = clk_get_rate(clk_can);
  211. *mscan_clksrc = MSCAN_CLKSRC_BUS;
  212. dev_dbg(&ofdev->dev, "clk from MCLK, clksrc[%d] freq[%lu]\n",
  213. *mscan_clksrc, freq_calc);
  214. break;
  215. default:
  216. goto err_invalid;
  217. }
  218. /* the above clk_can item is used for the bitrate, access to
  219. * the peripheral's register set needs the clk_ipg item
  220. */
  221. clk_ipg = devm_clk_get(&ofdev->dev, "ipg");
  222. if (IS_ERR(clk_ipg))
  223. goto err_notavail_ipg;
  224. if (clk_prepare_enable(clk_ipg))
  225. goto err_notavail_ipg;
  226. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  227. priv->clk_ipg = clk_ipg;
  228. /* return the determined clock source rate */
  229. return freq_calc;
  230. err_invalid:
  231. dev_err(&ofdev->dev, "invalid clock source specification\n");
  232. /* clock source rate could not get determined */
  233. return 0;
  234. err_notavail:
  235. dev_err(&ofdev->dev, "cannot acquire or setup bitrate clock source\n");
  236. /* clock source rate could not get determined */
  237. return 0;
  238. err_notavail_ipg:
  239. dev_err(&ofdev->dev, "cannot acquire or setup register clock\n");
  240. /* clock source rate could not get determined */
  241. return 0;
  242. }
  243. static void mpc512x_can_put_clock(struct platform_device *ofdev)
  244. {
  245. struct mscan_priv *priv;
  246. priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
  247. if (priv->clk_ipg)
  248. clk_disable_unprepare(priv->clk_ipg);
  249. }
  250. #else /* !CONFIG_PPC_MPC512x */
  251. static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
  252. const char *clock_name, int *mscan_clksrc)
  253. {
  254. return 0;
  255. }
  256. #define mpc512x_can_put_clock NULL
  257. #endif /* CONFIG_PPC_MPC512x */
  258. static const struct of_device_id mpc5xxx_can_table[];
  259. static int mpc5xxx_can_probe(struct platform_device *ofdev)
  260. {
  261. const struct of_device_id *match;
  262. const struct mpc5xxx_can_data *data;
  263. struct device_node *np = ofdev->dev.of_node;
  264. struct net_device *dev;
  265. struct mscan_priv *priv;
  266. void __iomem *base;
  267. const char *clock_name = NULL;
  268. int irq, mscan_clksrc = 0;
  269. int err = -ENOMEM;
  270. match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
  271. if (!match)
  272. return -EINVAL;
  273. data = match->data;
  274. base = of_iomap(np, 0);
  275. if (!base) {
  276. dev_err(&ofdev->dev, "couldn't ioremap\n");
  277. return err;
  278. }
  279. irq = irq_of_parse_and_map(np, 0);
  280. if (!irq) {
  281. dev_err(&ofdev->dev, "no irq found\n");
  282. err = -ENODEV;
  283. goto exit_unmap_mem;
  284. }
  285. dev = alloc_mscandev();
  286. if (!dev)
  287. goto exit_dispose_irq;
  288. platform_set_drvdata(ofdev, dev);
  289. SET_NETDEV_DEV(dev, &ofdev->dev);
  290. priv = netdev_priv(dev);
  291. priv->reg_base = base;
  292. dev->irq = irq;
  293. clock_name = of_get_property(np, "fsl,mscan-clock-source", NULL);
  294. BUG_ON(!data);
  295. priv->type = data->type;
  296. priv->can.clock.freq = data->get_clock(ofdev, clock_name,
  297. &mscan_clksrc);
  298. if (!priv->can.clock.freq) {
  299. dev_err(&ofdev->dev, "couldn't get MSCAN clock properties\n");
  300. goto exit_free_mscan;
  301. }
  302. err = register_mscandev(dev, mscan_clksrc);
  303. if (err) {
  304. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  305. DRV_NAME, err);
  306. goto exit_free_mscan;
  307. }
  308. dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n",
  309. priv->reg_base, dev->irq, priv->can.clock.freq);
  310. return 0;
  311. exit_free_mscan:
  312. free_candev(dev);
  313. exit_dispose_irq:
  314. irq_dispose_mapping(irq);
  315. exit_unmap_mem:
  316. iounmap(base);
  317. return err;
  318. }
  319. static int mpc5xxx_can_remove(struct platform_device *ofdev)
  320. {
  321. const struct of_device_id *match;
  322. const struct mpc5xxx_can_data *data;
  323. struct net_device *dev = platform_get_drvdata(ofdev);
  324. struct mscan_priv *priv = netdev_priv(dev);
  325. match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
  326. data = match ? match->data : NULL;
  327. unregister_mscandev(dev);
  328. if (data && data->put_clock)
  329. data->put_clock(ofdev);
  330. iounmap(priv->reg_base);
  331. irq_dispose_mapping(dev->irq);
  332. free_candev(dev);
  333. return 0;
  334. }
  335. #ifdef CONFIG_PM
  336. static struct mscan_regs saved_regs;
  337. static int mpc5xxx_can_suspend(struct platform_device *ofdev, pm_message_t state)
  338. {
  339. struct net_device *dev = platform_get_drvdata(ofdev);
  340. struct mscan_priv *priv = netdev_priv(dev);
  341. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  342. _memcpy_fromio(&saved_regs, regs, sizeof(*regs));
  343. return 0;
  344. }
  345. static int mpc5xxx_can_resume(struct platform_device *ofdev)
  346. {
  347. struct net_device *dev = platform_get_drvdata(ofdev);
  348. struct mscan_priv *priv = netdev_priv(dev);
  349. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  350. regs->canctl0 |= MSCAN_INITRQ;
  351. while (!(regs->canctl1 & MSCAN_INITAK))
  352. udelay(10);
  353. regs->canctl1 = saved_regs.canctl1;
  354. regs->canbtr0 = saved_regs.canbtr0;
  355. regs->canbtr1 = saved_regs.canbtr1;
  356. regs->canidac = saved_regs.canidac;
  357. /* restore masks, buffers etc. */
  358. _memcpy_toio(&regs->canidar1_0, (void *)&saved_regs.canidar1_0,
  359. sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));
  360. regs->canctl0 &= ~MSCAN_INITRQ;
  361. regs->cantbsel = saved_regs.cantbsel;
  362. regs->canrier = saved_regs.canrier;
  363. regs->cantier = saved_regs.cantier;
  364. regs->canctl0 = saved_regs.canctl0;
  365. return 0;
  366. }
  367. #endif
  368. static const struct mpc5xxx_can_data mpc5200_can_data = {
  369. .type = MSCAN_TYPE_MPC5200,
  370. .get_clock = mpc52xx_can_get_clock,
  371. /* .put_clock not applicable */
  372. };
  373. static const struct mpc5xxx_can_data mpc5121_can_data = {
  374. .type = MSCAN_TYPE_MPC5121,
  375. .get_clock = mpc512x_can_get_clock,
  376. .put_clock = mpc512x_can_put_clock,
  377. };
  378. static const struct of_device_id mpc5xxx_can_table[] = {
  379. { .compatible = "fsl,mpc5200-mscan", .data = &mpc5200_can_data, },
  380. /* Note that only MPC5121 Rev. 2 (and later) is supported */
  381. { .compatible = "fsl,mpc5121-mscan", .data = &mpc5121_can_data, },
  382. {},
  383. };
  384. MODULE_DEVICE_TABLE(of, mpc5xxx_can_table);
  385. static struct platform_driver mpc5xxx_can_driver = {
  386. .driver = {
  387. .name = "mpc5xxx_can",
  388. .of_match_table = mpc5xxx_can_table,
  389. },
  390. .probe = mpc5xxx_can_probe,
  391. .remove = mpc5xxx_can_remove,
  392. #ifdef CONFIG_PM
  393. .suspend = mpc5xxx_can_suspend,
  394. .resume = mpc5xxx_can_resume,
  395. #endif
  396. };
  397. module_platform_driver(mpc5xxx_can_driver);
  398. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  399. MODULE_DESCRIPTION("Freescale MPC5xxx CAN driver");
  400. MODULE_LICENSE("GPL v2");