xref: /OK3568_Linux_fs/kernel/drivers/net/can/mscan/mpc5xxx_can.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * CAN bus driver for the Freescale MPC5xxx embedded CPU.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2004-2005 Andrey Volkov <avolkov@varma-el.com>,
6*4882a593Smuzhiyun  *                         Varma Electronics Oy
7*4882a593Smuzhiyun  * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
8*4882a593Smuzhiyun  * Copyright (C) 2009 Wolfram Sang, Pengutronix <kernel@pengutronix.de>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/interrupt.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/netdevice.h>
16*4882a593Smuzhiyun #include <linux/can/dev.h>
17*4882a593Smuzhiyun #include <linux/of_platform.h>
18*4882a593Smuzhiyun #include <sysdev/fsl_soc.h>
19*4882a593Smuzhiyun #include <linux/clk.h>
20*4882a593Smuzhiyun #include <linux/io.h>
21*4882a593Smuzhiyun #include <asm/mpc52xx.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun #include "mscan.h"
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define DRV_NAME "mpc5xxx_can"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct mpc5xxx_can_data {
28*4882a593Smuzhiyun 	unsigned int type;
29*4882a593Smuzhiyun 	u32 (*get_clock)(struct platform_device *ofdev, const char *clock_name,
30*4882a593Smuzhiyun 			 int *mscan_clksrc);
31*4882a593Smuzhiyun 	void (*put_clock)(struct platform_device *ofdev);
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #ifdef CONFIG_PPC_MPC52xx
35*4882a593Smuzhiyun static const struct of_device_id mpc52xx_cdm_ids[] = {
36*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5200-cdm", },
37*4882a593Smuzhiyun 	{}
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun 
mpc52xx_can_get_clock(struct platform_device * ofdev,const char * clock_name,int * mscan_clksrc)40*4882a593Smuzhiyun static u32 mpc52xx_can_get_clock(struct platform_device *ofdev,
41*4882a593Smuzhiyun 				 const char *clock_name, int *mscan_clksrc)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	unsigned int pvr;
44*4882a593Smuzhiyun 	struct mpc52xx_cdm  __iomem *cdm;
45*4882a593Smuzhiyun 	struct device_node *np_cdm;
46*4882a593Smuzhiyun 	unsigned int freq;
47*4882a593Smuzhiyun 	u32 val;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	pvr = mfspr(SPRN_PVR);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	/*
52*4882a593Smuzhiyun 	 * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock
53*4882a593Smuzhiyun 	 * (IP_CLK) can be selected as MSCAN clock source. According to
54*4882a593Smuzhiyun 	 * the MPC5200 user's manual, the oscillator clock is the better
55*4882a593Smuzhiyun 	 * choice as it has less jitter. For this reason, it is selected
56*4882a593Smuzhiyun 	 * by default. Unfortunately, it can not be selected for the old
57*4882a593Smuzhiyun 	 * MPC5200 Rev. A chips due to a hardware bug (check errata).
58*4882a593Smuzhiyun 	 */
59*4882a593Smuzhiyun 	if (clock_name && strcmp(clock_name, "ip") == 0)
60*4882a593Smuzhiyun 		*mscan_clksrc = MSCAN_CLKSRC_BUS;
61*4882a593Smuzhiyun 	else
62*4882a593Smuzhiyun 		*mscan_clksrc = MSCAN_CLKSRC_XTAL;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
65*4882a593Smuzhiyun 	if (!freq)
66*4882a593Smuzhiyun 		return 0;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if (*mscan_clksrc == MSCAN_CLKSRC_BUS || pvr == 0x80822011)
69*4882a593Smuzhiyun 		return freq;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	/* Determine SYS_XTAL_IN frequency from the clock domain settings */
72*4882a593Smuzhiyun 	np_cdm = of_find_matching_node(NULL, mpc52xx_cdm_ids);
73*4882a593Smuzhiyun 	if (!np_cdm) {
74*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "can't get clock node!\n");
75*4882a593Smuzhiyun 		return 0;
76*4882a593Smuzhiyun 	}
77*4882a593Smuzhiyun 	cdm = of_iomap(np_cdm, 0);
78*4882a593Smuzhiyun 	if (!cdm) {
79*4882a593Smuzhiyun 		of_node_put(np_cdm);
80*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "can't map clock node!\n");
81*4882a593Smuzhiyun 		return 0;
82*4882a593Smuzhiyun 	}
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	if (in_8(&cdm->ipb_clk_sel) & 0x1)
85*4882a593Smuzhiyun 		freq *= 2;
86*4882a593Smuzhiyun 	val = in_be32(&cdm->rstcfg);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	freq *= (val & (1 << 5)) ? 8 : 4;
89*4882a593Smuzhiyun 	freq /= (val & (1 << 6)) ? 12 : 16;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	of_node_put(np_cdm);
92*4882a593Smuzhiyun 	iounmap(cdm);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	return freq;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun #else /* !CONFIG_PPC_MPC52xx */
mpc52xx_can_get_clock(struct platform_device * ofdev,const char * clock_name,int * mscan_clksrc)97*4882a593Smuzhiyun static u32 mpc52xx_can_get_clock(struct platform_device *ofdev,
98*4882a593Smuzhiyun 				 const char *clock_name, int *mscan_clksrc)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun #endif /* CONFIG_PPC_MPC52xx */
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #ifdef CONFIG_PPC_MPC512x
mpc512x_can_get_clock(struct platform_device * ofdev,const char * clock_source,int * mscan_clksrc)105*4882a593Smuzhiyun static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
106*4882a593Smuzhiyun 				 const char *clock_source, int *mscan_clksrc)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	struct device_node *np;
109*4882a593Smuzhiyun 	u32 clockdiv;
110*4882a593Smuzhiyun 	enum {
111*4882a593Smuzhiyun 		CLK_FROM_AUTO,
112*4882a593Smuzhiyun 		CLK_FROM_IPS,
113*4882a593Smuzhiyun 		CLK_FROM_SYS,
114*4882a593Smuzhiyun 		CLK_FROM_REF,
115*4882a593Smuzhiyun 	} clk_from;
116*4882a593Smuzhiyun 	struct clk *clk_in, *clk_can;
117*4882a593Smuzhiyun 	unsigned long freq_calc;
118*4882a593Smuzhiyun 	struct mscan_priv *priv;
119*4882a593Smuzhiyun 	struct clk *clk_ipg;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/* the caller passed in the clock source spec that was read from
122*4882a593Smuzhiyun 	 * the device tree, get the optional clock divider as well
123*4882a593Smuzhiyun 	 */
124*4882a593Smuzhiyun 	np = ofdev->dev.of_node;
125*4882a593Smuzhiyun 	clockdiv = 1;
126*4882a593Smuzhiyun 	of_property_read_u32(np, "fsl,mscan-clock-divider", &clockdiv);
127*4882a593Smuzhiyun 	dev_dbg(&ofdev->dev, "device tree specs: clk src[%s] div[%d]\n",
128*4882a593Smuzhiyun 		clock_source ? clock_source : "<NULL>", clockdiv);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	/* when clock-source is 'ip', the CANCTL1[CLKSRC] bit needs to
131*4882a593Smuzhiyun 	 * get set, and the 'ips' clock is the input to the MSCAN
132*4882a593Smuzhiyun 	 * component
133*4882a593Smuzhiyun 	 *
134*4882a593Smuzhiyun 	 * for clock-source values of 'ref' or 'sys' the CANCTL1[CLKSRC]
135*4882a593Smuzhiyun 	 * bit needs to get cleared, an optional clock-divider may have
136*4882a593Smuzhiyun 	 * been specified (the default value is 1), the appropriate
137*4882a593Smuzhiyun 	 * MSCAN related MCLK is the input to the MSCAN component
138*4882a593Smuzhiyun 	 *
139*4882a593Smuzhiyun 	 * in the absence of a clock-source spec, first an optimal clock
140*4882a593Smuzhiyun 	 * gets determined based on the 'sys' clock, if that fails the
141*4882a593Smuzhiyun 	 * 'ref' clock is used
142*4882a593Smuzhiyun 	 */
143*4882a593Smuzhiyun 	clk_from = CLK_FROM_AUTO;
144*4882a593Smuzhiyun 	if (clock_source) {
145*4882a593Smuzhiyun 		/* interpret the device tree's spec for the clock source */
146*4882a593Smuzhiyun 		if (!strcmp(clock_source, "ip"))
147*4882a593Smuzhiyun 			clk_from = CLK_FROM_IPS;
148*4882a593Smuzhiyun 		else if (!strcmp(clock_source, "sys"))
149*4882a593Smuzhiyun 			clk_from = CLK_FROM_SYS;
150*4882a593Smuzhiyun 		else if (!strcmp(clock_source, "ref"))
151*4882a593Smuzhiyun 			clk_from = CLK_FROM_REF;
152*4882a593Smuzhiyun 		else
153*4882a593Smuzhiyun 			goto err_invalid;
154*4882a593Smuzhiyun 		dev_dbg(&ofdev->dev, "got a clk source spec[%d]\n", clk_from);
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 	if (clk_from == CLK_FROM_AUTO) {
157*4882a593Smuzhiyun 		/* no spec so far, try the 'sys' clock; round to the
158*4882a593Smuzhiyun 		 * next MHz and see if we can get a multiple of 16MHz
159*4882a593Smuzhiyun 		 */
160*4882a593Smuzhiyun 		dev_dbg(&ofdev->dev, "no clk source spec, trying SYS\n");
161*4882a593Smuzhiyun 		clk_in = devm_clk_get(&ofdev->dev, "sys");
162*4882a593Smuzhiyun 		if (IS_ERR(clk_in))
163*4882a593Smuzhiyun 			goto err_notavail;
164*4882a593Smuzhiyun 		freq_calc = clk_get_rate(clk_in);
165*4882a593Smuzhiyun 		freq_calc +=  499999;
166*4882a593Smuzhiyun 		freq_calc /= 1000000;
167*4882a593Smuzhiyun 		freq_calc *= 1000000;
168*4882a593Smuzhiyun 		if ((freq_calc % 16000000) == 0) {
169*4882a593Smuzhiyun 			clk_from = CLK_FROM_SYS;
170*4882a593Smuzhiyun 			clockdiv = freq_calc / 16000000;
171*4882a593Smuzhiyun 			dev_dbg(&ofdev->dev,
172*4882a593Smuzhiyun 				"clk fit, sys[%lu] div[%d] freq[%lu]\n",
173*4882a593Smuzhiyun 				freq_calc, clockdiv, freq_calc / clockdiv);
174*4882a593Smuzhiyun 		}
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	if (clk_from == CLK_FROM_AUTO) {
177*4882a593Smuzhiyun 		/* no spec so far, use the 'ref' clock */
178*4882a593Smuzhiyun 		dev_dbg(&ofdev->dev, "no clk source spec, trying REF\n");
179*4882a593Smuzhiyun 		clk_in = devm_clk_get(&ofdev->dev, "ref");
180*4882a593Smuzhiyun 		if (IS_ERR(clk_in))
181*4882a593Smuzhiyun 			goto err_notavail;
182*4882a593Smuzhiyun 		clk_from = CLK_FROM_REF;
183*4882a593Smuzhiyun 		freq_calc = clk_get_rate(clk_in);
184*4882a593Smuzhiyun 		dev_dbg(&ofdev->dev,
185*4882a593Smuzhiyun 			"clk fit, ref[%lu] (no div) freq[%lu]\n",
186*4882a593Smuzhiyun 			freq_calc, freq_calc);
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* select IPS or MCLK as the MSCAN input (returned to the caller),
190*4882a593Smuzhiyun 	 * setup the MCLK mux source and rate if applicable, apply the
191*4882a593Smuzhiyun 	 * optionally specified or derived above divider, and determine
192*4882a593Smuzhiyun 	 * the actual resulting clock rate to return to the caller
193*4882a593Smuzhiyun 	 */
194*4882a593Smuzhiyun 	switch (clk_from) {
195*4882a593Smuzhiyun 	case CLK_FROM_IPS:
196*4882a593Smuzhiyun 		clk_can = devm_clk_get(&ofdev->dev, "ips");
197*4882a593Smuzhiyun 		if (IS_ERR(clk_can))
198*4882a593Smuzhiyun 			goto err_notavail;
199*4882a593Smuzhiyun 		priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
200*4882a593Smuzhiyun 		priv->clk_can = clk_can;
201*4882a593Smuzhiyun 		freq_calc = clk_get_rate(clk_can);
202*4882a593Smuzhiyun 		*mscan_clksrc = MSCAN_CLKSRC_IPS;
203*4882a593Smuzhiyun 		dev_dbg(&ofdev->dev, "clk from IPS, clksrc[%d] freq[%lu]\n",
204*4882a593Smuzhiyun 			*mscan_clksrc, freq_calc);
205*4882a593Smuzhiyun 		break;
206*4882a593Smuzhiyun 	case CLK_FROM_SYS:
207*4882a593Smuzhiyun 	case CLK_FROM_REF:
208*4882a593Smuzhiyun 		clk_can = devm_clk_get(&ofdev->dev, "mclk");
209*4882a593Smuzhiyun 		if (IS_ERR(clk_can))
210*4882a593Smuzhiyun 			goto err_notavail;
211*4882a593Smuzhiyun 		priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
212*4882a593Smuzhiyun 		priv->clk_can = clk_can;
213*4882a593Smuzhiyun 		if (clk_from == CLK_FROM_SYS)
214*4882a593Smuzhiyun 			clk_in = devm_clk_get(&ofdev->dev, "sys");
215*4882a593Smuzhiyun 		if (clk_from == CLK_FROM_REF)
216*4882a593Smuzhiyun 			clk_in = devm_clk_get(&ofdev->dev, "ref");
217*4882a593Smuzhiyun 		if (IS_ERR(clk_in))
218*4882a593Smuzhiyun 			goto err_notavail;
219*4882a593Smuzhiyun 		clk_set_parent(clk_can, clk_in);
220*4882a593Smuzhiyun 		freq_calc = clk_get_rate(clk_in);
221*4882a593Smuzhiyun 		freq_calc /= clockdiv;
222*4882a593Smuzhiyun 		clk_set_rate(clk_can, freq_calc);
223*4882a593Smuzhiyun 		freq_calc = clk_get_rate(clk_can);
224*4882a593Smuzhiyun 		*mscan_clksrc = MSCAN_CLKSRC_BUS;
225*4882a593Smuzhiyun 		dev_dbg(&ofdev->dev, "clk from MCLK, clksrc[%d] freq[%lu]\n",
226*4882a593Smuzhiyun 			*mscan_clksrc, freq_calc);
227*4882a593Smuzhiyun 		break;
228*4882a593Smuzhiyun 	default:
229*4882a593Smuzhiyun 		goto err_invalid;
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	/* the above clk_can item is used for the bitrate, access to
233*4882a593Smuzhiyun 	 * the peripheral's register set needs the clk_ipg item
234*4882a593Smuzhiyun 	 */
235*4882a593Smuzhiyun 	clk_ipg = devm_clk_get(&ofdev->dev, "ipg");
236*4882a593Smuzhiyun 	if (IS_ERR(clk_ipg))
237*4882a593Smuzhiyun 		goto err_notavail_ipg;
238*4882a593Smuzhiyun 	if (clk_prepare_enable(clk_ipg))
239*4882a593Smuzhiyun 		goto err_notavail_ipg;
240*4882a593Smuzhiyun 	priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
241*4882a593Smuzhiyun 	priv->clk_ipg = clk_ipg;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	/* return the determined clock source rate */
244*4882a593Smuzhiyun 	return freq_calc;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun err_invalid:
247*4882a593Smuzhiyun 	dev_err(&ofdev->dev, "invalid clock source specification\n");
248*4882a593Smuzhiyun 	/* clock source rate could not get determined */
249*4882a593Smuzhiyun 	return 0;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun err_notavail:
252*4882a593Smuzhiyun 	dev_err(&ofdev->dev, "cannot acquire or setup bitrate clock source\n");
253*4882a593Smuzhiyun 	/* clock source rate could not get determined */
254*4882a593Smuzhiyun 	return 0;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun err_notavail_ipg:
257*4882a593Smuzhiyun 	dev_err(&ofdev->dev, "cannot acquire or setup register clock\n");
258*4882a593Smuzhiyun 	/* clock source rate could not get determined */
259*4882a593Smuzhiyun 	return 0;
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
mpc512x_can_put_clock(struct platform_device * ofdev)262*4882a593Smuzhiyun static void mpc512x_can_put_clock(struct platform_device *ofdev)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct mscan_priv *priv;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
267*4882a593Smuzhiyun 	if (priv->clk_ipg)
268*4882a593Smuzhiyun 		clk_disable_unprepare(priv->clk_ipg);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun #else /* !CONFIG_PPC_MPC512x */
mpc512x_can_get_clock(struct platform_device * ofdev,const char * clock_name,int * mscan_clksrc)271*4882a593Smuzhiyun static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
272*4882a593Smuzhiyun 				 const char *clock_name, int *mscan_clksrc)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	return 0;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun #define mpc512x_can_put_clock NULL
277*4882a593Smuzhiyun #endif /* CONFIG_PPC_MPC512x */
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun static const struct of_device_id mpc5xxx_can_table[];
mpc5xxx_can_probe(struct platform_device * ofdev)280*4882a593Smuzhiyun static int mpc5xxx_can_probe(struct platform_device *ofdev)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	const struct of_device_id *match;
283*4882a593Smuzhiyun 	const struct mpc5xxx_can_data *data;
284*4882a593Smuzhiyun 	struct device_node *np = ofdev->dev.of_node;
285*4882a593Smuzhiyun 	struct net_device *dev;
286*4882a593Smuzhiyun 	struct mscan_priv *priv;
287*4882a593Smuzhiyun 	void __iomem *base;
288*4882a593Smuzhiyun 	const char *clock_name = NULL;
289*4882a593Smuzhiyun 	int irq, mscan_clksrc = 0;
290*4882a593Smuzhiyun 	int err = -ENOMEM;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
293*4882a593Smuzhiyun 	if (!match)
294*4882a593Smuzhiyun 		return -EINVAL;
295*4882a593Smuzhiyun 	data = match->data;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	base = of_iomap(np, 0);
298*4882a593Smuzhiyun 	if (!base) {
299*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "couldn't ioremap\n");
300*4882a593Smuzhiyun 		return err;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	irq = irq_of_parse_and_map(np, 0);
304*4882a593Smuzhiyun 	if (!irq) {
305*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "no irq found\n");
306*4882a593Smuzhiyun 		err = -ENODEV;
307*4882a593Smuzhiyun 		goto exit_unmap_mem;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	dev = alloc_mscandev();
311*4882a593Smuzhiyun 	if (!dev)
312*4882a593Smuzhiyun 		goto exit_dispose_irq;
313*4882a593Smuzhiyun 	platform_set_drvdata(ofdev, dev);
314*4882a593Smuzhiyun 	SET_NETDEV_DEV(dev, &ofdev->dev);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	priv = netdev_priv(dev);
317*4882a593Smuzhiyun 	priv->reg_base = base;
318*4882a593Smuzhiyun 	dev->irq = irq;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	clock_name = of_get_property(np, "fsl,mscan-clock-source", NULL);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	BUG_ON(!data);
323*4882a593Smuzhiyun 	priv->type = data->type;
324*4882a593Smuzhiyun 	priv->can.clock.freq = data->get_clock(ofdev, clock_name,
325*4882a593Smuzhiyun 					       &mscan_clksrc);
326*4882a593Smuzhiyun 	if (!priv->can.clock.freq) {
327*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "couldn't get MSCAN clock properties\n");
328*4882a593Smuzhiyun 		goto exit_put_clock;
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	err = register_mscandev(dev, mscan_clksrc);
332*4882a593Smuzhiyun 	if (err) {
333*4882a593Smuzhiyun 		dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
334*4882a593Smuzhiyun 			DRV_NAME, err);
335*4882a593Smuzhiyun 		goto exit_put_clock;
336*4882a593Smuzhiyun 	}
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n",
339*4882a593Smuzhiyun 		 priv->reg_base, dev->irq, priv->can.clock.freq);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	return 0;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun exit_put_clock:
344*4882a593Smuzhiyun 	if (data->put_clock)
345*4882a593Smuzhiyun 		data->put_clock(ofdev);
346*4882a593Smuzhiyun 	free_candev(dev);
347*4882a593Smuzhiyun exit_dispose_irq:
348*4882a593Smuzhiyun 	irq_dispose_mapping(irq);
349*4882a593Smuzhiyun exit_unmap_mem:
350*4882a593Smuzhiyun 	iounmap(base);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	return err;
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
mpc5xxx_can_remove(struct platform_device * ofdev)355*4882a593Smuzhiyun static int mpc5xxx_can_remove(struct platform_device *ofdev)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	const struct of_device_id *match;
358*4882a593Smuzhiyun 	const struct mpc5xxx_can_data *data;
359*4882a593Smuzhiyun 	struct net_device *dev = platform_get_drvdata(ofdev);
360*4882a593Smuzhiyun 	struct mscan_priv *priv = netdev_priv(dev);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
363*4882a593Smuzhiyun 	data = match ? match->data : NULL;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	unregister_mscandev(dev);
366*4882a593Smuzhiyun 	if (data && data->put_clock)
367*4882a593Smuzhiyun 		data->put_clock(ofdev);
368*4882a593Smuzhiyun 	iounmap(priv->reg_base);
369*4882a593Smuzhiyun 	irq_dispose_mapping(dev->irq);
370*4882a593Smuzhiyun 	free_candev(dev);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	return 0;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun #ifdef CONFIG_PM
376*4882a593Smuzhiyun static struct mscan_regs saved_regs;
mpc5xxx_can_suspend(struct platform_device * ofdev,pm_message_t state)377*4882a593Smuzhiyun static int mpc5xxx_can_suspend(struct platform_device *ofdev, pm_message_t state)
378*4882a593Smuzhiyun {
379*4882a593Smuzhiyun 	struct net_device *dev = platform_get_drvdata(ofdev);
380*4882a593Smuzhiyun 	struct mscan_priv *priv = netdev_priv(dev);
381*4882a593Smuzhiyun 	struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	_memcpy_fromio(&saved_regs, regs, sizeof(*regs));
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	return 0;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
mpc5xxx_can_resume(struct platform_device * ofdev)388*4882a593Smuzhiyun static int mpc5xxx_can_resume(struct platform_device *ofdev)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	struct net_device *dev = platform_get_drvdata(ofdev);
391*4882a593Smuzhiyun 	struct mscan_priv *priv = netdev_priv(dev);
392*4882a593Smuzhiyun 	struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	regs->canctl0 |= MSCAN_INITRQ;
395*4882a593Smuzhiyun 	while (!(regs->canctl1 & MSCAN_INITAK))
396*4882a593Smuzhiyun 		udelay(10);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	regs->canctl1 = saved_regs.canctl1;
399*4882a593Smuzhiyun 	regs->canbtr0 = saved_regs.canbtr0;
400*4882a593Smuzhiyun 	regs->canbtr1 = saved_regs.canbtr1;
401*4882a593Smuzhiyun 	regs->canidac = saved_regs.canidac;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	/* restore masks, buffers etc. */
404*4882a593Smuzhiyun 	_memcpy_toio(&regs->canidar1_0, (void *)&saved_regs.canidar1_0,
405*4882a593Smuzhiyun 		     sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	regs->canctl0 &= ~MSCAN_INITRQ;
408*4882a593Smuzhiyun 	regs->cantbsel = saved_regs.cantbsel;
409*4882a593Smuzhiyun 	regs->canrier = saved_regs.canrier;
410*4882a593Smuzhiyun 	regs->cantier = saved_regs.cantier;
411*4882a593Smuzhiyun 	regs->canctl0 = saved_regs.canctl0;
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 	return 0;
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun #endif
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun static const struct mpc5xxx_can_data mpc5200_can_data = {
418*4882a593Smuzhiyun 	.type = MSCAN_TYPE_MPC5200,
419*4882a593Smuzhiyun 	.get_clock = mpc52xx_can_get_clock,
420*4882a593Smuzhiyun 	/* .put_clock not applicable */
421*4882a593Smuzhiyun };
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun static const struct mpc5xxx_can_data mpc5121_can_data = {
424*4882a593Smuzhiyun 	.type = MSCAN_TYPE_MPC5121,
425*4882a593Smuzhiyun 	.get_clock = mpc512x_can_get_clock,
426*4882a593Smuzhiyun 	.put_clock = mpc512x_can_put_clock,
427*4882a593Smuzhiyun };
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun static const struct of_device_id mpc5xxx_can_table[] = {
430*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5200-mscan", .data = &mpc5200_can_data, },
431*4882a593Smuzhiyun 	/* Note that only MPC5121 Rev. 2 (and later) is supported */
432*4882a593Smuzhiyun 	{ .compatible = "fsl,mpc5121-mscan", .data = &mpc5121_can_data, },
433*4882a593Smuzhiyun 	{},
434*4882a593Smuzhiyun };
435*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mpc5xxx_can_table);
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun static struct platform_driver mpc5xxx_can_driver = {
438*4882a593Smuzhiyun 	.driver = {
439*4882a593Smuzhiyun 		.name = "mpc5xxx_can",
440*4882a593Smuzhiyun 		.of_match_table = mpc5xxx_can_table,
441*4882a593Smuzhiyun 	},
442*4882a593Smuzhiyun 	.probe = mpc5xxx_can_probe,
443*4882a593Smuzhiyun 	.remove = mpc5xxx_can_remove,
444*4882a593Smuzhiyun #ifdef CONFIG_PM
445*4882a593Smuzhiyun 	.suspend = mpc5xxx_can_suspend,
446*4882a593Smuzhiyun 	.resume = mpc5xxx_can_resume,
447*4882a593Smuzhiyun #endif
448*4882a593Smuzhiyun };
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun module_platform_driver(mpc5xxx_can_driver);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
453*4882a593Smuzhiyun MODULE_DESCRIPTION("Freescale MPC5xxx CAN driver");
454*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
455