mpc5xxx_can.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 peripheral's
  132. * physical address. Register address offsets against the
  133. * IMMR base are: 0x1300, 0x1380, 0x2300, 0x2380
  134. */
  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 (IS_ERR(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 (IS_ERR(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 mpc512x_can_get_clock(struct platform_device *ofdev,
  214. const char *clock_name, int *mscan_clksrc)
  215. {
  216. return 0;
  217. }
  218. #endif /* CONFIG_PPC_MPC512x */
  219. static const struct of_device_id mpc5xxx_can_table[];
  220. static int mpc5xxx_can_probe(struct platform_device *ofdev)
  221. {
  222. const struct of_device_id *match;
  223. const struct mpc5xxx_can_data *data;
  224. struct device_node *np = ofdev->dev.of_node;
  225. struct net_device *dev;
  226. struct mscan_priv *priv;
  227. void __iomem *base;
  228. const char *clock_name = NULL;
  229. int irq, mscan_clksrc = 0;
  230. int err = -ENOMEM;
  231. match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
  232. if (!match)
  233. return -EINVAL;
  234. data = match->data;
  235. base = of_iomap(np, 0);
  236. if (!base) {
  237. dev_err(&ofdev->dev, "couldn't ioremap\n");
  238. return err;
  239. }
  240. irq = irq_of_parse_and_map(np, 0);
  241. if (!irq) {
  242. dev_err(&ofdev->dev, "no irq found\n");
  243. err = -ENODEV;
  244. goto exit_unmap_mem;
  245. }
  246. dev = alloc_mscandev();
  247. if (!dev)
  248. goto exit_dispose_irq;
  249. priv = netdev_priv(dev);
  250. priv->reg_base = base;
  251. dev->irq = irq;
  252. clock_name = of_get_property(np, "fsl,mscan-clock-source", NULL);
  253. BUG_ON(!data);
  254. priv->type = data->type;
  255. priv->can.clock.freq = data->get_clock(ofdev, clock_name,
  256. &mscan_clksrc);
  257. if (!priv->can.clock.freq) {
  258. dev_err(&ofdev->dev, "couldn't get MSCAN clock properties\n");
  259. goto exit_free_mscan;
  260. }
  261. SET_NETDEV_DEV(dev, &ofdev->dev);
  262. err = register_mscandev(dev, mscan_clksrc);
  263. if (err) {
  264. dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
  265. DRV_NAME, err);
  266. goto exit_free_mscan;
  267. }
  268. platform_set_drvdata(ofdev, dev);
  269. dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n",
  270. priv->reg_base, dev->irq, priv->can.clock.freq);
  271. return 0;
  272. exit_free_mscan:
  273. free_candev(dev);
  274. exit_dispose_irq:
  275. irq_dispose_mapping(irq);
  276. exit_unmap_mem:
  277. iounmap(base);
  278. return err;
  279. }
  280. static int mpc5xxx_can_remove(struct platform_device *ofdev)
  281. {
  282. struct net_device *dev = platform_get_drvdata(ofdev);
  283. struct mscan_priv *priv = netdev_priv(dev);
  284. unregister_mscandev(dev);
  285. iounmap(priv->reg_base);
  286. irq_dispose_mapping(dev->irq);
  287. free_candev(dev);
  288. return 0;
  289. }
  290. #ifdef CONFIG_PM
  291. static struct mscan_regs saved_regs;
  292. static int mpc5xxx_can_suspend(struct platform_device *ofdev, pm_message_t state)
  293. {
  294. struct net_device *dev = platform_get_drvdata(ofdev);
  295. struct mscan_priv *priv = netdev_priv(dev);
  296. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  297. _memcpy_fromio(&saved_regs, regs, sizeof(*regs));
  298. return 0;
  299. }
  300. static int mpc5xxx_can_resume(struct platform_device *ofdev)
  301. {
  302. struct net_device *dev = platform_get_drvdata(ofdev);
  303. struct mscan_priv *priv = netdev_priv(dev);
  304. struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
  305. regs->canctl0 |= MSCAN_INITRQ;
  306. while (!(regs->canctl1 & MSCAN_INITAK))
  307. udelay(10);
  308. regs->canctl1 = saved_regs.canctl1;
  309. regs->canbtr0 = saved_regs.canbtr0;
  310. regs->canbtr1 = saved_regs.canbtr1;
  311. regs->canidac = saved_regs.canidac;
  312. /* restore masks, buffers etc. */
  313. _memcpy_toio(&regs->canidar1_0, (void *)&saved_regs.canidar1_0,
  314. sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));
  315. regs->canctl0 &= ~MSCAN_INITRQ;
  316. regs->cantbsel = saved_regs.cantbsel;
  317. regs->canrier = saved_regs.canrier;
  318. regs->cantier = saved_regs.cantier;
  319. regs->canctl0 = saved_regs.canctl0;
  320. return 0;
  321. }
  322. #endif
  323. static const struct mpc5xxx_can_data mpc5200_can_data = {
  324. .type = MSCAN_TYPE_MPC5200,
  325. .get_clock = mpc52xx_can_get_clock,
  326. };
  327. static const struct mpc5xxx_can_data mpc5121_can_data = {
  328. .type = MSCAN_TYPE_MPC5121,
  329. .get_clock = mpc512x_can_get_clock,
  330. };
  331. static const struct of_device_id mpc5xxx_can_table[] = {
  332. { .compatible = "fsl,mpc5200-mscan", .data = &mpc5200_can_data, },
  333. /* Note that only MPC5121 Rev. 2 (and later) is supported */
  334. { .compatible = "fsl,mpc5121-mscan", .data = &mpc5121_can_data, },
  335. {},
  336. };
  337. MODULE_DEVICE_TABLE(of, mpc5xxx_can_table);
  338. static struct platform_driver mpc5xxx_can_driver = {
  339. .driver = {
  340. .name = "mpc5xxx_can",
  341. .owner = THIS_MODULE,
  342. .of_match_table = mpc5xxx_can_table,
  343. },
  344. .probe = mpc5xxx_can_probe,
  345. .remove = mpc5xxx_can_remove,
  346. #ifdef CONFIG_PM
  347. .suspend = mpc5xxx_can_suspend,
  348. .resume = mpc5xxx_can_resume,
  349. #endif
  350. };
  351. module_platform_driver(mpc5xxx_can_driver);
  352. MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
  353. MODULE_DESCRIPTION("Freescale MPC5xxx CAN driver");
  354. MODULE_LICENSE("GPL v2");