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