mpc5xxx_can.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 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. struct mpc512x_clockctl {
  96. u32 spmr; /* System PLL Mode Reg */
  97. u32 sccr[2]; /* System Clk Ctrl Reg 1 & 2 */
  98. u32 scfr1; /* System Clk Freq Reg 1 */
  99. u32 scfr2; /* System Clk Freq Reg 2 */
  100. u32 reserved;
  101. u32 bcr; /* Bread Crumb Reg */
  102. u32 pccr[12]; /* PSC Clk Ctrl Reg 0-11 */
  103. u32 spccr; /* SPDIF Clk Ctrl Reg */
  104. u32 cccr; /* CFM Clk Ctrl Reg */
  105. u32 dccr; /* DIU Clk Cnfg Reg */
  106. u32 mccr[4]; /* MSCAN Clk Ctrl Reg 1-3 */
  107. };
  108. static struct of_device_id mpc512x_clock_ids[] = {
  109. { .compatible = "fsl,mpc5121-clock", },
  110. {}
  111. };
  112. static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
  113. const char *clock_name, int *mscan_clksrc)
  114. {
  115. struct mpc512x_clockctl __iomem *clockctl;
  116. struct device_node *np_clock;
  117. struct clk *sys_clk, *ref_clk;
  118. int plen, clockidx, clocksrc = -1;
  119. u32 sys_freq, val, clockdiv = 1, freq = 0;
  120. const u32 *pval;
  121. np_clock = of_find_matching_node(NULL, mpc512x_clock_ids);
  122. if (!np_clock) {
  123. dev_err(&ofdev->dev, "couldn't find clock node\n");
  124. return 0;
  125. }
  126. clockctl = of_iomap(np_clock, 0);
  127. if (!clockctl) {
  128. dev_err(&ofdev->dev, "couldn't map clock registers\n");
  129. goto exit_put;
  130. }
  131. /* Determine the MSCAN device index from the physical address */
  132. pval = of_get_property(ofdev->dev.of_node, "reg", &plen);
  133. BUG_ON(!pval || plen < sizeof(*pval));
  134. clockidx = (*pval & 0x80) ? 1 : 0;
  135. if (*pval & 0x2000)
  136. clockidx += 2;
  137. /*
  138. * Clock source and divider selection: 3 different clock sources
  139. * can be selected: "ip", "ref" or "sys". For the latter two, a
  140. * clock divider can be defined as well. If the clock source is
  141. * not specified by the device tree, we first try to find an
  142. * optimal CAN source clock based on the system clock. If that
  143. * is not posslible, the reference clock will be used.
  144. */
  145. if (clock_name && !strcmp(clock_name, "ip")) {
  146. *mscan_clksrc = MSCAN_CLKSRC_IPS;
  147. freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
  148. } else {
  149. *mscan_clksrc = MSCAN_CLKSRC_BUS;
  150. pval = of_get_property(ofdev->dev.of_node,
  151. "fsl,mscan-clock-divider", &plen);
  152. if (pval && plen == sizeof(*pval))
  153. clockdiv = *pval;
  154. if (!clockdiv)
  155. clockdiv = 1;
  156. if (!clock_name || !strcmp(clock_name, "sys")) {
  157. sys_clk = clk_get(&ofdev->dev, "sys_clk");
  158. if (IS_ERR(sys_clk)) {
  159. dev_err(&ofdev->dev, "couldn't get sys_clk\n");
  160. goto exit_unmap;
  161. }
  162. /* Get and round up/down sys clock rate */
  163. sys_freq = 1000000 *
  164. ((clk_get_rate(sys_clk) + 499999) / 1000000);
  165. if (!clock_name) {
  166. /* A multiple of 16 MHz would be optimal */
  167. if ((sys_freq % 16000000) == 0) {
  168. clocksrc = 0;
  169. clockdiv = sys_freq / 16000000;
  170. freq = sys_freq / clockdiv;
  171. }
  172. } else {
  173. clocksrc = 0;
  174. freq = sys_freq / clockdiv;
  175. }
  176. }
  177. if (clocksrc < 0) {
  178. ref_clk = clk_get(&ofdev->dev, "ref_clk");
  179. if (IS_ERR(ref_clk)) {
  180. dev_err(&ofdev->dev, "couldn't get ref_clk\n");
  181. goto exit_unmap;
  182. }
  183. clocksrc = 1;
  184. freq = clk_get_rate(ref_clk) / clockdiv;
  185. }
  186. }
  187. /* Disable clock */
  188. out_be32(&clockctl->mccr[clockidx], 0x0);
  189. if (clocksrc >= 0) {
  190. /* Set source and divider */
  191. val = (clocksrc << 14) | ((clockdiv - 1) << 17);
  192. out_be32(&clockctl->mccr[clockidx], val);
  193. /* Enable clock */
  194. out_be32(&clockctl->mccr[clockidx], val | 0x10000);
  195. }
  196. /* Enable MSCAN clock domain */
  197. val = in_be32(&clockctl->sccr[1]);
  198. if (!(val & (1 << 25)))
  199. out_be32(&clockctl->sccr[1], val | (1 << 25));
  200. dev_dbg(&ofdev->dev, "using '%s' with frequency divider %d\n",
  201. *mscan_clksrc == MSCAN_CLKSRC_IPS ? "ips_clk" :
  202. clocksrc == 1 ? "ref_clk" : "sys_clk", clockdiv);
  203. exit_unmap:
  204. iounmap(clockctl);
  205. exit_put:
  206. of_node_put(np_clock);
  207. return freq;
  208. }
  209. #else /* !CONFIG_PPC_MPC512x */
  210. static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
  211. const char *clock_name, int *mscan_clksrc)
  212. {
  213. return 0;
  214. }
  215. #endif /* CONFIG_PPC_MPC512x */
  216. static const struct of_device_id mpc5xxx_can_table[];
  217. static int mpc5xxx_can_probe(struct platform_device *ofdev)
  218. {
  219. const struct of_device_id *match;
  220. const struct mpc5xxx_can_data *data;
  221. struct device_node *np = ofdev->dev.of_node;
  222. struct net_device *dev;
  223. struct mscan_priv *priv;
  224. void __iomem *base;
  225. const char *clock_name = NULL;
  226. int irq, mscan_clksrc = 0;
  227. int err = -ENOMEM;
  228. match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
  229. if (!match)
  230. return -EINVAL;
  231. data = match->data;
  232. base = of_iomap(np, 0);
  233. if (!base) {
  234. dev_err(&ofdev->dev, "couldn't ioremap\n");
  235. return err;
  236. }
  237. irq = irq_of_parse_and_map(np, 0);
  238. if (!irq) {
  239. dev_err(&ofdev->dev, "no irq found\n");
  240. err = -ENODEV;
  241. goto exit_unmap_mem;
  242. }
  243. dev = alloc_mscandev();
  244. if (!dev)
  245. goto exit_dispose_irq;
  246. priv = netdev_priv(dev);
  247. priv->reg_base = base;
  248. dev->irq = irq;
  249. clock_name = of_get_property(np, "fsl,mscan-clock-source", NULL);
  250. BUG_ON(!data);
  251. priv->type = data->type;
  252. priv->can.clock.freq = data->get_clock(ofdev, clock_name,
  253. &mscan_clksrc);
  254. if (!priv->can.clock.freq) {
  255. dev_err(&ofdev->dev, "couldn't get MSCAN clock properties\n");
  256. goto exit_free_mscan;
  257. }
  258. SET_NETDEV_DEV(dev, &ofdev->dev);
  259. err = register_mscandev(dev, mscan_clksrc);
  260. if (err) {
  261. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  262. DRV_NAME, err);
  263. goto exit_free_mscan;
  264. }
  265. dev_set_drvdata(&ofdev->dev, dev);
  266. dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n",
  267. priv->reg_base, dev->irq, priv->can.clock.freq);
  268. return 0;
  269. exit_free_mscan:
  270. free_candev(dev);
  271. exit_dispose_irq:
  272. irq_dispose_mapping(irq);
  273. exit_unmap_mem:
  274. iounmap(base);
  275. return err;
  276. }
  277. static int mpc5xxx_can_remove(struct platform_device *ofdev)
  278. {
  279. struct net_device *dev = dev_get_drvdata(&ofdev->dev);
  280. struct mscan_priv *priv = netdev_priv(dev);
  281. dev_set_drvdata(&ofdev->dev, NULL);
  282. unregister_mscandev(dev);
  283. iounmap(priv->reg_base);
  284. irq_dispose_mapping(dev->irq);
  285. free_candev(dev);
  286. return 0;
  287. }
  288. #ifdef CONFIG_PM
  289. static struct mscan_regs saved_regs;
  290. static int mpc5xxx_can_suspend(struct platform_device *ofdev, pm_message_t state)
  291. {
  292. struct net_device *dev = dev_get_drvdata(&ofdev->dev);
  293. struct mscan_priv *priv = netdev_priv(dev);
  294. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  295. _memcpy_fromio(&saved_regs, regs, sizeof(*regs));
  296. return 0;
  297. }
  298. static int mpc5xxx_can_resume(struct platform_device *ofdev)
  299. {
  300. struct net_device *dev = dev_get_drvdata(&ofdev->dev);
  301. struct mscan_priv *priv = netdev_priv(dev);
  302. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  303. regs->canctl0 |= MSCAN_INITRQ;
  304. while (!(regs->canctl1 & MSCAN_INITAK))
  305. udelay(10);
  306. regs->canctl1 = saved_regs.canctl1;
  307. regs->canbtr0 = saved_regs.canbtr0;
  308. regs->canbtr1 = saved_regs.canbtr1;
  309. regs->canidac = saved_regs.canidac;
  310. /* restore masks, buffers etc. */
  311. _memcpy_toio(&regs->canidar1_0, (void *)&saved_regs.canidar1_0,
  312. sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));
  313. regs->canctl0 &= ~MSCAN_INITRQ;
  314. regs->cantbsel = saved_regs.cantbsel;
  315. regs->canrier = saved_regs.canrier;
  316. regs->cantier = saved_regs.cantier;
  317. regs->canctl0 = saved_regs.canctl0;
  318. return 0;
  319. }
  320. #endif
  321. static const struct mpc5xxx_can_data mpc5200_can_data = {
  322. .type = MSCAN_TYPE_MPC5200,
  323. .get_clock = mpc52xx_can_get_clock,
  324. };
  325. static const struct mpc5xxx_can_data mpc5121_can_data = {
  326. .type = MSCAN_TYPE_MPC5121,
  327. .get_clock = mpc512x_can_get_clock,
  328. };
  329. static const struct of_device_id mpc5xxx_can_table[] = {
  330. { .compatible = "fsl,mpc5200-mscan", .data = &mpc5200_can_data, },
  331. /* Note that only MPC5121 Rev. 2 (and later) is supported */
  332. { .compatible = "fsl,mpc5121-mscan", .data = &mpc5121_can_data, },
  333. {},
  334. };
  335. MODULE_DEVICE_TABLE(of, mpc5xxx_can_table);
  336. static struct platform_driver mpc5xxx_can_driver = {
  337. .driver = {
  338. .name = "mpc5xxx_can",
  339. .owner = THIS_MODULE,
  340. .of_match_table = mpc5xxx_can_table,
  341. },
  342. .probe = mpc5xxx_can_probe,
  343. .remove = mpc5xxx_can_remove,
  344. #ifdef CONFIG_PM
  345. .suspend = mpc5xxx_can_suspend,
  346. .resume = mpc5xxx_can_resume,
  347. #endif
  348. };
  349. module_platform_driver(mpc5xxx_can_driver);
  350. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  351. MODULE_DESCRIPTION("Freescale MPC5xxx CAN driver");
  352. MODULE_LICENSE("GPL v2");