mpc5xxx_can.c 11 KB

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