mpc5xxx_can.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/can/dev.h>
  28. #include <linux/of_platform.h>
  29. #include <sysdev/fsl_soc.h>
  30. #include <linux/clk.h>
  31. #include <linux/io.h>
  32. #include <asm/mpc52xx.h>
  33. #include "mscan.h"
  34. #define DRV_NAME "mpc5xxx_can"
  35. struct mpc5xxx_can_data {
  36. unsigned int type;
  37. u32 (*get_clock)(struct platform_device *ofdev, const char *clock_name,
  38. int *mscan_clksrc);
  39. };
  40. #ifdef CONFIG_PPC_MPC52xx
  41. static struct of_device_id __devinitdata mpc52xx_cdm_ids[] = {
  42. { .compatible = "fsl,mpc5200-cdm", },
  43. {}
  44. };
  45. static u32 __devinit mpc52xx_can_get_clock(struct platform_device *ofdev,
  46. const char *clock_name,
  47. int *mscan_clksrc)
  48. {
  49. unsigned int pvr;
  50. struct mpc52xx_cdm __iomem *cdm;
  51. struct device_node *np_cdm;
  52. unsigned int freq;
  53. u32 val;
  54. pvr = mfspr(SPRN_PVR);
  55. /*
  56. * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock
  57. * (IP_CLK) can be selected as MSCAN clock source. According to
  58. * the MPC5200 user's manual, the oscillator clock is the better
  59. * choice as it has less jitter. For this reason, it is selected
  60. * by default. Unfortunately, it can not be selected for the old
  61. * MPC5200 Rev. A chips due to a hardware bug (check errata).
  62. */
  63. if (clock_name && strcmp(clock_name, "ip") == 0)
  64. *mscan_clksrc = MSCAN_CLKSRC_BUS;
  65. else
  66. *mscan_clksrc = MSCAN_CLKSRC_XTAL;
  67. freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
  68. if (!freq)
  69. return 0;
  70. if (*mscan_clksrc == MSCAN_CLKSRC_BUS || pvr == 0x80822011)
  71. return freq;
  72. /* Determine SYS_XTAL_IN frequency from the clock domain settings */
  73. np_cdm = of_find_matching_node(NULL, mpc52xx_cdm_ids);
  74. if (!np_cdm) {
  75. dev_err(&ofdev->dev, "can't get clock node!\n");
  76. return 0;
  77. }
  78. cdm = of_iomap(np_cdm, 0);
  79. if (in_8(&cdm->ipb_clk_sel) & 0x1)
  80. freq *= 2;
  81. val = in_be32(&cdm->rstcfg);
  82. freq *= (val & (1 << 5)) ? 8 : 4;
  83. freq /= (val & (1 << 6)) ? 12 : 16;
  84. of_node_put(np_cdm);
  85. iounmap(cdm);
  86. return freq;
  87. }
  88. #else /* !CONFIG_PPC_MPC52xx */
  89. static u32 __devinit mpc52xx_can_get_clock(struct platform_device *ofdev,
  90. const char *clock_name,
  91. int *mscan_clksrc)
  92. {
  93. return 0;
  94. }
  95. #endif /* CONFIG_PPC_MPC52xx */
  96. #ifdef CONFIG_PPC_MPC512x
  97. struct mpc512x_clockctl {
  98. u32 spmr; /* System PLL Mode Reg */
  99. u32 sccr[2]; /* System Clk Ctrl Reg 1 & 2 */
  100. u32 scfr1; /* System Clk Freq Reg 1 */
  101. u32 scfr2; /* System Clk Freq Reg 2 */
  102. u32 reserved;
  103. u32 bcr; /* Bread Crumb Reg */
  104. u32 pccr[12]; /* PSC Clk Ctrl Reg 0-11 */
  105. u32 spccr; /* SPDIF Clk Ctrl Reg */
  106. u32 cccr; /* CFM Clk Ctrl Reg */
  107. u32 dccr; /* DIU Clk Cnfg Reg */
  108. u32 mccr[4]; /* MSCAN Clk Ctrl Reg 1-3 */
  109. };
  110. static struct of_device_id __devinitdata mpc512x_clock_ids[] = {
  111. { .compatible = "fsl,mpc5121-clock", },
  112. {}
  113. };
  114. static u32 __devinit mpc512x_can_get_clock(struct platform_device *ofdev,
  115. const char *clock_name,
  116. int *mscan_clksrc)
  117. {
  118. struct mpc512x_clockctl __iomem *clockctl;
  119. struct device_node *np_clock;
  120. struct clk *sys_clk, *ref_clk;
  121. int plen, clockidx, clocksrc = -1;
  122. u32 sys_freq, val, clockdiv = 1, freq = 0;
  123. const u32 *pval;
  124. np_clock = of_find_matching_node(NULL, mpc512x_clock_ids);
  125. if (!np_clock) {
  126. dev_err(&ofdev->dev, "couldn't find clock node\n");
  127. return 0;
  128. }
  129. clockctl = of_iomap(np_clock, 0);
  130. if (!clockctl) {
  131. dev_err(&ofdev->dev, "couldn't map clock registers\n");
  132. goto exit_put;
  133. }
  134. /* Determine the MSCAN device index from the physical address */
  135. pval = of_get_property(ofdev->dev.of_node, "reg", &plen);
  136. BUG_ON(!pval || plen < sizeof(*pval));
  137. clockidx = (*pval & 0x80) ? 1 : 0;
  138. if (*pval & 0x2000)
  139. clockidx += 2;
  140. /*
  141. * Clock source and divider selection: 3 different clock sources
  142. * can be selected: "ip", "ref" or "sys". For the latter two, a
  143. * clock divider can be defined as well. If the clock source is
  144. * not specified by the device tree, we first try to find an
  145. * optimal CAN source clock based on the system clock. If that
  146. * is not posslible, the reference clock will be used.
  147. */
  148. if (clock_name && !strcmp(clock_name, "ip")) {
  149. *mscan_clksrc = MSCAN_CLKSRC_IPS;
  150. freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
  151. } else {
  152. *mscan_clksrc = MSCAN_CLKSRC_BUS;
  153. pval = of_get_property(ofdev->dev.of_node,
  154. "fsl,mscan-clock-divider", &plen);
  155. if (pval && plen == sizeof(*pval))
  156. clockdiv = *pval;
  157. if (!clockdiv)
  158. clockdiv = 1;
  159. if (!clock_name || !strcmp(clock_name, "sys")) {
  160. sys_clk = clk_get(&ofdev->dev, "sys_clk");
  161. if (!sys_clk) {
  162. dev_err(&ofdev->dev, "couldn't get sys_clk\n");
  163. goto exit_unmap;
  164. }
  165. /* Get and round up/down sys clock rate */
  166. sys_freq = 1000000 *
  167. ((clk_get_rate(sys_clk) + 499999) / 1000000);
  168. if (!clock_name) {
  169. /* A multiple of 16 MHz would be optimal */
  170. if ((sys_freq % 16000000) == 0) {
  171. clocksrc = 0;
  172. clockdiv = sys_freq / 16000000;
  173. freq = sys_freq / clockdiv;
  174. }
  175. } else {
  176. clocksrc = 0;
  177. freq = sys_freq / clockdiv;
  178. }
  179. }
  180. if (clocksrc < 0) {
  181. ref_clk = clk_get(&ofdev->dev, "ref_clk");
  182. if (!ref_clk) {
  183. dev_err(&ofdev->dev, "couldn't get ref_clk\n");
  184. goto exit_unmap;
  185. }
  186. clocksrc = 1;
  187. freq = clk_get_rate(ref_clk) / clockdiv;
  188. }
  189. }
  190. /* Disable clock */
  191. out_be32(&clockctl->mccr[clockidx], 0x0);
  192. if (clocksrc >= 0) {
  193. /* Set source and divider */
  194. val = (clocksrc << 14) | ((clockdiv - 1) << 17);
  195. out_be32(&clockctl->mccr[clockidx], val);
  196. /* Enable clock */
  197. out_be32(&clockctl->mccr[clockidx], val | 0x10000);
  198. }
  199. /* Enable MSCAN clock domain */
  200. val = in_be32(&clockctl->sccr[1]);
  201. if (!(val & (1 << 25)))
  202. out_be32(&clockctl->sccr[1], val | (1 << 25));
  203. dev_dbg(&ofdev->dev, "using '%s' with frequency divider %d\n",
  204. *mscan_clksrc == MSCAN_CLKSRC_IPS ? "ips_clk" :
  205. clocksrc == 1 ? "ref_clk" : "sys_clk", clockdiv);
  206. exit_unmap:
  207. iounmap(clockctl);
  208. exit_put:
  209. of_node_put(np_clock);
  210. return freq;
  211. }
  212. #else /* !CONFIG_PPC_MPC512x */
  213. static u32 __devinit mpc512x_can_get_clock(struct platform_device *ofdev,
  214. const char *clock_name,
  215. int *mscan_clksrc)
  216. {
  217. return 0;
  218. }
  219. #endif /* CONFIG_PPC_MPC512x */
  220. static struct of_device_id mpc5xxx_can_table[];
  221. static int __devinit mpc5xxx_can_probe(struct platform_device *ofdev)
  222. {
  223. const struct of_device_id *match;
  224. struct mpc5xxx_can_data *data;
  225. struct device_node *np = ofdev->dev.of_node;
  226. struct net_device *dev;
  227. struct mscan_priv *priv;
  228. void __iomem *base;
  229. const char *clock_name = NULL;
  230. int irq, mscan_clksrc = 0;
  231. int err = -ENOMEM;
  232. match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
  233. if (!match)
  234. return -EINVAL;
  235. data = match->data;
  236. base = of_iomap(np, 0);
  237. if (!base) {
  238. dev_err(&ofdev->dev, "couldn't ioremap\n");
  239. return err;
  240. }
  241. irq = irq_of_parse_and_map(np, 0);
  242. if (!irq) {
  243. dev_err(&ofdev->dev, "no irq found\n");
  244. err = -ENODEV;
  245. goto exit_unmap_mem;
  246. }
  247. dev = alloc_mscandev();
  248. if (!dev)
  249. goto exit_dispose_irq;
  250. priv = netdev_priv(dev);
  251. priv->reg_base = base;
  252. dev->irq = irq;
  253. clock_name = of_get_property(np, "fsl,mscan-clock-source", NULL);
  254. BUG_ON(!data);
  255. priv->type = data->type;
  256. priv->can.clock.freq = data->get_clock(ofdev, clock_name,
  257. &mscan_clksrc);
  258. if (!priv->can.clock.freq) {
  259. dev_err(&ofdev->dev, "couldn't get MSCAN clock properties\n");
  260. goto exit_free_mscan;
  261. }
  262. SET_NETDEV_DEV(dev, &ofdev->dev);
  263. err = register_mscandev(dev, mscan_clksrc);
  264. if (err) {
  265. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  266. DRV_NAME, err);
  267. goto exit_free_mscan;
  268. }
  269. dev_set_drvdata(&ofdev->dev, dev);
  270. dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n",
  271. priv->reg_base, dev->irq, priv->can.clock.freq);
  272. return 0;
  273. exit_free_mscan:
  274. free_candev(dev);
  275. exit_dispose_irq:
  276. irq_dispose_mapping(irq);
  277. exit_unmap_mem:
  278. iounmap(base);
  279. return err;
  280. }
  281. static int __devexit mpc5xxx_can_remove(struct platform_device *ofdev)
  282. {
  283. struct net_device *dev = dev_get_drvdata(&ofdev->dev);
  284. struct mscan_priv *priv = netdev_priv(dev);
  285. dev_set_drvdata(&ofdev->dev, NULL);
  286. unregister_mscandev(dev);
  287. iounmap(priv->reg_base);
  288. irq_dispose_mapping(dev->irq);
  289. free_candev(dev);
  290. return 0;
  291. }
  292. #ifdef CONFIG_PM
  293. static struct mscan_regs saved_regs;
  294. static int mpc5xxx_can_suspend(struct platform_device *ofdev, pm_message_t state)
  295. {
  296. struct net_device *dev = dev_get_drvdata(&ofdev->dev);
  297. struct mscan_priv *priv = netdev_priv(dev);
  298. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  299. _memcpy_fromio(&saved_regs, regs, sizeof(*regs));
  300. return 0;
  301. }
  302. static int mpc5xxx_can_resume(struct platform_device *ofdev)
  303. {
  304. struct net_device *dev = dev_get_drvdata(&ofdev->dev);
  305. struct mscan_priv *priv = netdev_priv(dev);
  306. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  307. regs->canctl0 |= MSCAN_INITRQ;
  308. while (!(regs->canctl1 & MSCAN_INITAK))
  309. udelay(10);
  310. regs->canctl1 = saved_regs.canctl1;
  311. regs->canbtr0 = saved_regs.canbtr0;
  312. regs->canbtr1 = saved_regs.canbtr1;
  313. regs->canidac = saved_regs.canidac;
  314. /* restore masks, buffers etc. */
  315. _memcpy_toio(&regs->canidar1_0, (void *)&saved_regs.canidar1_0,
  316. sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));
  317. regs->canctl0 &= ~MSCAN_INITRQ;
  318. regs->cantbsel = saved_regs.cantbsel;
  319. regs->canrier = saved_regs.canrier;
  320. regs->cantier = saved_regs.cantier;
  321. regs->canctl0 = saved_regs.canctl0;
  322. return 0;
  323. }
  324. #endif
  325. static struct mpc5xxx_can_data __devinitdata mpc5200_can_data = {
  326. .type = MSCAN_TYPE_MPC5200,
  327. .get_clock = mpc52xx_can_get_clock,
  328. };
  329. static struct mpc5xxx_can_data __devinitdata mpc5121_can_data = {
  330. .type = MSCAN_TYPE_MPC5121,
  331. .get_clock = mpc512x_can_get_clock,
  332. };
  333. static struct of_device_id __devinitdata mpc5xxx_can_table[] = {
  334. { .compatible = "fsl,mpc5200-mscan", .data = &mpc5200_can_data, },
  335. /* Note that only MPC5121 Rev. 2 (and later) is supported */
  336. { .compatible = "fsl,mpc5121-mscan", .data = &mpc5121_can_data, },
  337. {},
  338. };
  339. static struct platform_driver mpc5xxx_can_driver = {
  340. .driver = {
  341. .name = "mpc5xxx_can",
  342. .owner = THIS_MODULE,
  343. .of_match_table = mpc5xxx_can_table,
  344. },
  345. .probe = mpc5xxx_can_probe,
  346. .remove = __devexit_p(mpc5xxx_can_remove),
  347. #ifdef CONFIG_PM
  348. .suspend = mpc5xxx_can_suspend,
  349. .resume = mpc5xxx_can_resume,
  350. #endif
  351. };
  352. static int __init mpc5xxx_can_init(void)
  353. {
  354. return platform_driver_register(&mpc5xxx_can_driver);
  355. }
  356. module_init(mpc5xxx_can_init);
  357. static void __exit mpc5xxx_can_exit(void)
  358. {
  359. platform_driver_unregister(&mpc5xxx_can_driver);
  360. };
  361. module_exit(mpc5xxx_can_exit);
  362. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  363. MODULE_DESCRIPTION("Freescale MPC5xxx CAN driver");
  364. MODULE_LICENSE("GPL v2");