mpc5xxx_can.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * CAN bus driver for the Freescale MPC5xxx embedded CPU.
 *
 * Copyright (C) 2004-2005 Andrey Volkov <avolkov@varma-el.com>,
 *                         Varma Electronics Oy
 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
 * Copyright (C) 2009 Wolfram Sang, Pengutronix <w.sang@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the version 2 of the GNU General Public License
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/netdevice.h>
#include <linux/can/dev.h>
#include <linux/of_platform.h>
#include <sysdev/fsl_soc.h>
30
#include <linux/clk.h>
31 32 33 34 35 36 37
#include <linux/io.h>
#include <asm/mpc52xx.h>

#include "mscan.h"

#define DRV_NAME "mpc5xxx_can"

38 39
struct mpc5xxx_can_data {
	unsigned int type;
40
	u32 (*get_clock)(struct platform_device *ofdev, const char *clock_name,
41
			 int *mscan_clksrc);
42
	void (*put_clock)(struct platform_device *ofdev);
43 44
};

45
#ifdef CONFIG_PPC_MPC52xx
B
Bill Pemberton 已提交
46
static struct of_device_id mpc52xx_cdm_ids[] = {
47 48 49 50
	{ .compatible = "fsl,mpc5200-cdm", },
	{}
};

B
Bill Pemberton 已提交
51
static u32 mpc52xx_can_get_clock(struct platform_device *ofdev,
52
				 const char *clock_name, int *mscan_clksrc)
53
{
54
	unsigned int pvr;
55 56 57 58 59
	struct mpc52xx_cdm  __iomem *cdm;
	struct device_node *np_cdm;
	unsigned int freq;
	u32 val;

60 61
	pvr = mfspr(SPRN_PVR);

62 63 64 65 66 67 68 69 70 71 72 73 74
	/*
	 * Either the oscillator clock (SYS_XTAL_IN) or the IP bus clock
	 * (IP_CLK) can be selected as MSCAN clock source. According to
	 * the MPC5200 user's manual, the oscillator clock is the better
	 * choice as it has less jitter. For this reason, it is selected
	 * by default. Unfortunately, it can not be selected for the old
	 * MPC5200 Rev. A chips due to a hardware bug (check errata).
	 */
	if (clock_name && strcmp(clock_name, "ip") == 0)
		*mscan_clksrc = MSCAN_CLKSRC_BUS;
	else
		*mscan_clksrc = MSCAN_CLKSRC_XTAL;

75
	freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
76 77 78
	if (!freq)
		return 0;

79
	if (*mscan_clksrc == MSCAN_CLKSRC_BUS || pvr == 0x80822011)
80 81 82
		return freq;

	/* Determine SYS_XTAL_IN frequency from the clock domain settings */
83 84
	np_cdm = of_find_matching_node(NULL, mpc52xx_cdm_ids);
	if (!np_cdm) {
85
		dev_err(&ofdev->dev, "can't get clock node!\n");
86 87 88 89 90 91
		return 0;
	}
	cdm = of_iomap(np_cdm, 0);

	if (in_8(&cdm->ipb_clk_sel) & 0x1)
		freq *= 2;
W
Wolfram Sang 已提交
92 93 94 95
	val = in_be32(&cdm->rstcfg);

	freq *= (val & (1 << 5)) ? 8 : 4;
	freq /= (val & (1 << 6)) ? 12 : 16;
96

97
	of_node_put(np_cdm);
98 99 100 101
	iounmap(cdm);

	return freq;
}
102
#else /* !CONFIG_PPC_MPC52xx */
B
Bill Pemberton 已提交
103
static u32 mpc52xx_can_get_clock(struct platform_device *ofdev,
104
				 const char *clock_name, int *mscan_clksrc)
105 106 107
{
	return 0;
}
108
#endif /* CONFIG_PPC_MPC52xx */
109 110

#ifdef CONFIG_PPC_MPC512x
B
Bill Pemberton 已提交
111
static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
112
				 const char *clock_source, int *mscan_clksrc)
113
{
114 115 116 117 118 119 120 121 122 123 124 125
	struct device_node *np;
	u32 clockdiv;
	enum {
		CLK_FROM_AUTO,
		CLK_FROM_IPS,
		CLK_FROM_SYS,
		CLK_FROM_REF,
	} clk_from;
	struct clk *clk_in, *clk_can;
	unsigned long freq_calc;
	struct mscan_priv *priv;
	struct clk *clk_ipg;
126

127 128
	/* the caller passed in the clock source spec that was read from
	 * the device tree, get the optional clock divider as well
129
	 */
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	np = ofdev->dev.of_node;
	clockdiv = 1;
	of_property_read_u32(np, "fsl,mscan-clock-divider", &clockdiv);
	dev_dbg(&ofdev->dev, "device tree specs: clk src[%s] div[%d]\n",
		clock_source ? clock_source : "<NULL>", clockdiv);

	/* when clock-source is 'ip', the CANCTL1[CLKSRC] bit needs to
	 * get set, and the 'ips' clock is the input to the MSCAN
	 * component
	 *
	 * for clock-source values of 'ref' or 'sys' the CANCTL1[CLKSRC]
	 * bit needs to get cleared, an optional clock-divider may have
	 * been specified (the default value is 1), the appropriate
	 * MSCAN related MCLK is the input to the MSCAN component
	 *
	 * in the absence of a clock-source spec, first an optimal clock
	 * gets determined based on the 'sys' clock, if that fails the
	 * 'ref' clock is used
	 */
	clk_from = CLK_FROM_AUTO;
	if (clock_source) {
		/* interpret the device tree's spec for the clock source */
		if (!strcmp(clock_source, "ip"))
			clk_from = CLK_FROM_IPS;
		else if (!strcmp(clock_source, "sys"))
			clk_from = CLK_FROM_SYS;
		else if (!strcmp(clock_source, "ref"))
			clk_from = CLK_FROM_REF;
		else
			goto err_invalid;
		dev_dbg(&ofdev->dev, "got a clk source spec[%d]\n", clk_from);
	}
	if (clk_from == CLK_FROM_AUTO) {
		/* no spec so far, try the 'sys' clock; round to the
		 * next MHz and see if we can get a multiple of 16MHz
		 */
		dev_dbg(&ofdev->dev, "no clk source spec, trying SYS\n");
		clk_in = devm_clk_get(&ofdev->dev, "sys");
		if (IS_ERR(clk_in))
			goto err_notavail;
		freq_calc = clk_get_rate(clk_in);
		freq_calc +=  499999;
		freq_calc /= 1000000;
		freq_calc *= 1000000;
		if ((freq_calc % 16000000) == 0) {
			clk_from = CLK_FROM_SYS;
			clockdiv = freq_calc / 16000000;
			dev_dbg(&ofdev->dev,
				"clk fit, sys[%lu] div[%d] freq[%lu]\n",
				freq_calc, clockdiv, freq_calc / clockdiv);
		}
	}
	if (clk_from == CLK_FROM_AUTO) {
		/* no spec so far, use the 'ref' clock */
		dev_dbg(&ofdev->dev, "no clk source spec, trying REF\n");
		clk_in = devm_clk_get(&ofdev->dev, "ref");
		if (IS_ERR(clk_in))
			goto err_notavail;
		clk_from = CLK_FROM_REF;
		freq_calc = clk_get_rate(clk_in);
		dev_dbg(&ofdev->dev,
			"clk fit, ref[%lu] (no div) freq[%lu]\n",
			freq_calc, freq_calc);
	}
194

195 196 197 198
	/* select IPS or MCLK as the MSCAN input (returned to the caller),
	 * setup the MCLK mux source and rate if applicable, apply the
	 * optionally specified or derived above divider, and determine
	 * the actual resulting clock rate to return to the caller
199
	 */
200 201 202 203 204 205 206 207
	switch (clk_from) {
	case CLK_FROM_IPS:
		clk_can = devm_clk_get(&ofdev->dev, "ips");
		if (IS_ERR(clk_can))
			goto err_notavail;
		priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
		priv->clk_can = clk_can;
		freq_calc = clk_get_rate(clk_can);
208
		*mscan_clksrc = MSCAN_CLKSRC_IPS;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		dev_dbg(&ofdev->dev, "clk from IPS, clksrc[%d] freq[%lu]\n",
			*mscan_clksrc, freq_calc);
		break;
	case CLK_FROM_SYS:
	case CLK_FROM_REF:
		clk_can = devm_clk_get(&ofdev->dev, "mclk");
		if (IS_ERR(clk_can))
			goto err_notavail;
		priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
		priv->clk_can = clk_can;
		if (clk_from == CLK_FROM_SYS)
			clk_in = devm_clk_get(&ofdev->dev, "sys");
		if (clk_from == CLK_FROM_REF)
			clk_in = devm_clk_get(&ofdev->dev, "ref");
		if (IS_ERR(clk_in))
			goto err_notavail;
		clk_set_parent(clk_can, clk_in);
		freq_calc = clk_get_rate(clk_in);
		freq_calc /= clockdiv;
		clk_set_rate(clk_can, freq_calc);
		freq_calc = clk_get_rate(clk_can);
230
		*mscan_clksrc = MSCAN_CLKSRC_BUS;
231 232 233 234 235
		dev_dbg(&ofdev->dev, "clk from MCLK, clksrc[%d] freq[%lu]\n",
			*mscan_clksrc, freq_calc);
		break;
	default:
		goto err_invalid;
236 237
	}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	/* the above clk_can item is used for the bitrate, access to
	 * the peripheral's register set needs the clk_ipg item
	 */
	clk_ipg = devm_clk_get(&ofdev->dev, "ipg");
	if (IS_ERR(clk_ipg))
		goto err_notavail_ipg;
	if (clk_prepare_enable(clk_ipg))
		goto err_notavail_ipg;
	priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
	priv->clk_ipg = clk_ipg;

	/* return the determined clock source rate */
	return freq_calc;

err_invalid:
	dev_err(&ofdev->dev, "invalid clock source specification\n");
	/* clock source rate could not get determined */
	return 0;
256

257 258 259 260
err_notavail:
	dev_err(&ofdev->dev, "cannot acquire or setup bitrate clock source\n");
	/* clock source rate could not get determined */
	return 0;
261

262 263 264 265 266
err_notavail_ipg:
	dev_err(&ofdev->dev, "cannot acquire or setup register clock\n");
	/* clock source rate could not get determined */
	return 0;
}
267

268 269 270 271 272 273 274
static void mpc512x_can_put_clock(struct platform_device *ofdev)
{
	struct mscan_priv *priv;

	priv = netdev_priv(dev_get_drvdata(&ofdev->dev));
	if (priv->clk_ipg)
		clk_disable_unprepare(priv->clk_ipg);
275 276
}
#else /* !CONFIG_PPC_MPC512x */
B
Bill Pemberton 已提交
277
static u32 mpc512x_can_get_clock(struct platform_device *ofdev,
278
				 const char *clock_name, int *mscan_clksrc)
279 280 281
{
	return 0;
}
282
#define mpc512x_can_put_clock NULL
283
#endif /* CONFIG_PPC_MPC512x */
284

285
static const struct of_device_id mpc5xxx_can_table[];
B
Bill Pemberton 已提交
286
static int mpc5xxx_can_probe(struct platform_device *ofdev)
287
{
288
	const struct of_device_id *match;
289
	const struct mpc5xxx_can_data *data;
290
	struct device_node *np = ofdev->dev.of_node;
291 292 293
	struct net_device *dev;
	struct mscan_priv *priv;
	void __iomem *base;
294 295 296
	const char *clock_name = NULL;
	int irq, mscan_clksrc = 0;
	int err = -ENOMEM;
297

298 299
	match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
	if (!match)
300
		return -EINVAL;
301
	data = match->data;
302

303
	base = of_iomap(np, 0);
304 305
	if (!base) {
		dev_err(&ofdev->dev, "couldn't ioremap\n");
306
		return err;
307 308 309 310 311 312 313 314 315 316
	}

	irq = irq_of_parse_and_map(np, 0);
	if (!irq) {
		dev_err(&ofdev->dev, "no irq found\n");
		err = -ENODEV;
		goto exit_unmap_mem;
	}

	dev = alloc_mscandev();
317
	if (!dev)
318
		goto exit_dispose_irq;
319 320
	platform_set_drvdata(ofdev, dev);
	SET_NETDEV_DEV(dev, &ofdev->dev);
321 322 323 324 325

	priv = netdev_priv(dev);
	priv->reg_base = base;
	dev->irq = irq;

326 327 328 329 330 331
	clock_name = of_get_property(np, "fsl,mscan-clock-source", NULL);

	BUG_ON(!data);
	priv->type = data->type;
	priv->can.clock.freq = data->get_clock(ofdev, clock_name,
					       &mscan_clksrc);
332
	if (!priv->can.clock.freq) {
333
		dev_err(&ofdev->dev, "couldn't get MSCAN clock properties\n");
334 335 336
		goto exit_free_mscan;
	}

337
	err = register_mscandev(dev, mscan_clksrc);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	if (err) {
		dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
			DRV_NAME, err);
		goto exit_free_mscan;
	}

	dev_info(&ofdev->dev, "MSCAN at 0x%p, irq %d, clock %d Hz\n",
		 priv->reg_base, dev->irq, priv->can.clock.freq);

	return 0;

exit_free_mscan:
	free_candev(dev);
exit_dispose_irq:
	irq_dispose_mapping(irq);
exit_unmap_mem:
	iounmap(base);
355

356 357 358
	return err;
}

B
Bill Pemberton 已提交
359
static int mpc5xxx_can_remove(struct platform_device *ofdev)
360
{
361 362
	const struct of_device_id *match;
	const struct mpc5xxx_can_data *data;
363
	struct net_device *dev = platform_get_drvdata(ofdev);
364 365
	struct mscan_priv *priv = netdev_priv(dev);

366 367 368
	match = of_match_device(mpc5xxx_can_table, &ofdev->dev);
	data = match ? match->data : NULL;

369
	unregister_mscandev(dev);
370 371
	if (data && data->put_clock)
		data->put_clock(ofdev);
372 373 374 375 376 377 378 379 380
	iounmap(priv->reg_base);
	irq_dispose_mapping(dev->irq);
	free_candev(dev);

	return 0;
}

#ifdef CONFIG_PM
static struct mscan_regs saved_regs;
381
static int mpc5xxx_can_suspend(struct platform_device *ofdev, pm_message_t state)
382
{
383
	struct net_device *dev = platform_get_drvdata(ofdev);
384 385 386 387 388 389 390 391
	struct mscan_priv *priv = netdev_priv(dev);
	struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;

	_memcpy_fromio(&saved_regs, regs, sizeof(*regs));

	return 0;
}

392
static int mpc5xxx_can_resume(struct platform_device *ofdev)
393
{
394
	struct net_device *dev = platform_get_drvdata(ofdev);
395 396 397 398
	struct mscan_priv *priv = netdev_priv(dev);
	struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;

	regs->canctl0 |= MSCAN_INITRQ;
W
Wolfram Sang 已提交
399
	while (!(regs->canctl1 & MSCAN_INITAK))
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
		udelay(10);

	regs->canctl1 = saved_regs.canctl1;
	regs->canbtr0 = saved_regs.canbtr0;
	regs->canbtr1 = saved_regs.canbtr1;
	regs->canidac = saved_regs.canidac;

	/* restore masks, buffers etc. */
	_memcpy_toio(&regs->canidar1_0, (void *)&saved_regs.canidar1_0,
		     sizeof(*regs) - offsetof(struct mscan_regs, canidar1_0));

	regs->canctl0 &= ~MSCAN_INITRQ;
	regs->cantbsel = saved_regs.cantbsel;
	regs->canrier = saved_regs.canrier;
	regs->cantier = saved_regs.cantier;
	regs->canctl0 = saved_regs.canctl0;

	return 0;
}
#endif

B
Bill Pemberton 已提交
421
static const struct mpc5xxx_can_data mpc5200_can_data = {
422 423
	.type = MSCAN_TYPE_MPC5200,
	.get_clock = mpc52xx_can_get_clock,
424
	/* .put_clock not applicable */
425 426
};

B
Bill Pemberton 已提交
427
static const struct mpc5xxx_can_data mpc5121_can_data = {
428 429
	.type = MSCAN_TYPE_MPC5121,
	.get_clock = mpc512x_can_get_clock,
430
	.put_clock = mpc512x_can_put_clock,
431 432
};

B
Bill Pemberton 已提交
433
static const struct of_device_id mpc5xxx_can_table[] = {
434 435 436
	{ .compatible = "fsl,mpc5200-mscan", .data = &mpc5200_can_data, },
	/* Note that only MPC5121 Rev. 2 (and later) is supported */
	{ .compatible = "fsl,mpc5121-mscan", .data = &mpc5121_can_data, },
437 438
	{},
};
439
MODULE_DEVICE_TABLE(of, mpc5xxx_can_table);
440

441
static struct platform_driver mpc5xxx_can_driver = {
442 443 444 445 446
	.driver = {
		.name = "mpc5xxx_can",
		.owner = THIS_MODULE,
		.of_match_table = mpc5xxx_can_table,
	},
447
	.probe = mpc5xxx_can_probe,
B
Bill Pemberton 已提交
448
	.remove = mpc5xxx_can_remove,
449 450 451 452 453 454
#ifdef CONFIG_PM
	.suspend = mpc5xxx_can_suspend,
	.resume = mpc5xxx_can_resume,
#endif
};

455
module_platform_driver(mpc5xxx_can_driver);
456 457

MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
458
MODULE_DESCRIPTION("Freescale MPC5xxx CAN driver");
459
MODULE_LICENSE("GPL v2");