c_can_platform.c 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Platform CAN bus driver for Bosch C_CAN controller
 *
 * Copyright (C) 2010 ST Microelectronics
 * Bhupesh Sharma <bhupesh.sharma@st.com>
 *
 * Borrowed heavily from the C_CAN driver originally written by:
 * Copyright (C) 2007
 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
 * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
 *
 * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
 * Bosch C_CAN user manual can be obtained from:
 * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
 * users_manual_c_can.pdf
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2. This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/if_ether.h>
#include <linux/list.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
33 34
#include <linux/of.h>
#include <linux/of_device.h>
35 36
#include <linux/mfd/syscon.h>
#include <linux/regmap.h>
37 38 39 40 41

#include <linux/can/dev.h>

#include "c_can.h"

42
#define DCAN_RAM_INIT_BIT		(1 << 3)
43
static DEFINE_SPINLOCK(raminit_lock);
44 45 46 47 48 49
/*
 * 16-bit c_can registers can be arranged differently in the memory
 * architecture of different implementations. For example: 16-bit
 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
 * Handle the same by providing a common read/write interface.
 */
50
static u16 c_can_plat_read_reg_aligned_to_16bit(const struct c_can_priv *priv,
51
						enum reg index)
52
{
53
	return readw(priv->base + priv->regs[index]);
54 55
}

56
static void c_can_plat_write_reg_aligned_to_16bit(const struct c_can_priv *priv,
57
						enum reg index, u16 val)
58
{
59
	writew(val, priv->base + priv->regs[index]);
60 61
}

62
static u16 c_can_plat_read_reg_aligned_to_32bit(const struct c_can_priv *priv,
63
						enum reg index)
64
{
65
	return readw(priv->base + 2 * priv->regs[index]);
66 67
}

68
static void c_can_plat_write_reg_aligned_to_32bit(const struct c_can_priv *priv,
69
						enum reg index, u16 val)
70
{
71
	writew(val, priv->base + 2 * priv->regs[index]);
72 73
}

74 75
static void c_can_hw_raminit_wait_syscon(const struct c_can_priv *priv,
					 u32 mask, u32 val)
76
{
77
	const struct c_can_raminit *raminit = &priv->raminit_sys;
78
	int timeout = 0;
79
	u32 ctrl = 0;
80

81 82
	/* We look only at the bits of our instance. */
	val &= mask;
83
	do {
84
		udelay(1);
85 86
		timeout++;

87
		regmap_read(raminit->syscon, raminit->reg, &ctrl);
88 89 90 91
		if (timeout == 1000) {
			dev_err(&priv->dev->dev, "%s: time out\n", __func__);
			break;
		}
92
	} while ((ctrl & mask) != val);
93 94
}

95
static void c_can_hw_raminit_syscon(const struct c_can_priv *priv, bool enable)
96
{
97 98 99
	const struct c_can_raminit *raminit = &priv->raminit_sys;
	u32 ctrl = 0;
	u32 mask;
100

101 102
	spin_lock(&raminit_lock);

103 104 105
	mask = 1 << raminit->bits.start | 1 << raminit->bits.done;
	regmap_read(raminit->syscon, raminit->reg, &ctrl);

106
	/* We clear the start bit first. The start bit is
107
	 * looking at the 0 -> transition, but is not self clearing;
108
	 * NOTE: DONE must be written with 1 to clear it.
109 110 111
	 * We can't clear the DONE bit here using regmap_update_bits()
	 * as it will bypass the write if initial condition is START:0 DONE:1
	 * e.g. on DRA7 which needs START pulse.
112
	 */
113 114
	ctrl &= ~mask;	/* START = 0, DONE = 0 */
	regmap_update_bits(raminit->syscon, raminit->reg, mask, ctrl);
115

116 117 118 119
	/* check if START bit is 0. Ignore DONE bit for now
	 * as it can be either 0 or 1.
	 */
	c_can_hw_raminit_wait_syscon(priv, 1 << raminit->bits.start, ctrl);
120 121

	if (enable) {
122
		/* Clear DONE bit & set START bit. */
123
		ctrl |= 1 << raminit->bits.start;
124 125 126 127 128
		/* DONE must be written with 1 to clear it */
		ctrl |= 1 << raminit->bits.done;
		regmap_update_bits(raminit->syscon, raminit->reg, mask, ctrl);
		/* prevent further clearing of DONE bit */
		ctrl &= ~(1 << raminit->bits.done);
129 130 131
		/* clear START bit if start pulse is needed */
		if (raminit->needs_pulse) {
			ctrl &= ~(1 << raminit->bits.start);
132 133
			regmap_update_bits(raminit->syscon, raminit->reg,
					   mask, ctrl);
134 135
		}

136 137
		ctrl |= 1 << raminit->bits.done;
		c_can_hw_raminit_wait_syscon(priv, mask, ctrl);
138 139
	}
	spin_unlock(&raminit_lock);
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
static u32 c_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
{
	u32 val;

	val = priv->read_reg(priv, index);
	val |= ((u32) priv->read_reg(priv, index + 1)) << 16;

	return val;
}

static void c_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
		u32 val)
{
	priv->write_reg(priv, index + 1, val >> 16);
	priv->write_reg(priv, index, val);
}

static u32 d_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
{
	return readl(priv->base + priv->regs[index]);
}

static void d_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
		u32 val)
{
	writel(val, priv->base + priv->regs[index]);
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
static void c_can_hw_raminit_wait(const struct c_can_priv *priv, u32 mask)
{
	while (priv->read_reg32(priv, C_CAN_FUNCTION_REG) & mask)
		udelay(1);
}

static void c_can_hw_raminit(const struct c_can_priv *priv, bool enable)
{
	u32 ctrl;

	ctrl = priv->read_reg32(priv, C_CAN_FUNCTION_REG);
	ctrl &= ~DCAN_RAM_INIT_BIT;
	priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
	c_can_hw_raminit_wait(priv, ctrl);

	if (enable) {
		ctrl |= DCAN_RAM_INIT_BIT;
		priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
		c_can_hw_raminit_wait(priv, ctrl);
	}
}

192 193 194 195 196 197 198 199
static const struct c_can_driver_data c_can_drvdata = {
	.id = BOSCH_C_CAN,
};

static const struct c_can_driver_data d_can_drvdata = {
	.id = BOSCH_D_CAN,
};

200 201 202 203 204 205 206 207 208 209 210 211
static const struct raminit_bits dra7_raminit_bits[] = {
	[0] = { .start = 3, .done = 1, },
	[1] = { .start = 5, .done = 2, },
};

static const struct c_can_driver_data dra7_dcan_drvdata = {
	.id = BOSCH_D_CAN,
	.raminit_num = ARRAY_SIZE(dra7_raminit_bits),
	.raminit_bits = dra7_raminit_bits,
	.raminit_pulse = true,
};

212 213 214 215 216 217 218 219 220 221 222
static const struct raminit_bits am3352_raminit_bits[] = {
	[0] = { .start = 0, .done = 8, },
	[1] = { .start = 1, .done = 9, },
};

static const struct c_can_driver_data am3352_dcan_drvdata = {
	.id = BOSCH_D_CAN,
	.raminit_num = ARRAY_SIZE(am3352_raminit_bits),
	.raminit_bits = am3352_raminit_bits,
};

A
Arvind Yadav 已提交
223
static const struct platform_device_id c_can_id_table[] = {
224
	{
225
		.name = KBUILD_MODNAME,
226
		.driver_data = (kernel_ulong_t)&c_can_drvdata,
227
	},
228
	{
229
		.name = "c_can",
230
		.driver_data = (kernel_ulong_t)&c_can_drvdata,
231
	},
232
	{
233
		.name = "d_can",
234 235 236
		.driver_data = (kernel_ulong_t)&d_can_drvdata,
	},
	{ /* sentinel */ },
237
};
238
MODULE_DEVICE_TABLE(platform, c_can_id_table);
239 240

static const struct of_device_id c_can_of_table[] = {
241 242
	{ .compatible = "bosch,c_can", .data = &c_can_drvdata },
	{ .compatible = "bosch,d_can", .data = &d_can_drvdata },
243
	{ .compatible = "ti,dra7-d_can", .data = &dra7_dcan_drvdata },
244
	{ .compatible = "ti,am3352-d_can", .data = &am3352_dcan_drvdata },
245
	{ .compatible = "ti,am4372-d_can", .data = &am3352_dcan_drvdata },
246 247
	{ /* sentinel */ },
};
248
MODULE_DEVICE_TABLE(of, c_can_of_table);
249

B
Bill Pemberton 已提交
250
static int c_can_plat_probe(struct platform_device *pdev)
251 252 253 254 255
{
	int ret;
	void __iomem *addr;
	struct net_device *dev;
	struct c_can_priv *priv;
256
	const struct of_device_id *match;
257
	struct resource *mem;
258
	int irq;
259
	struct clk *clk;
260
	const struct c_can_driver_data *drvdata;
261
	struct device_node *np = pdev->dev.of_node;
262 263 264 265 266 267 268

	match = of_match_device(c_can_of_table, &pdev->dev);
	if (match) {
		drvdata = match->data;
	} else if (pdev->id_entry->driver_data) {
		drvdata = (struct c_can_driver_data *)
			platform_get_device_id(pdev)->driver_data;
269
	} else {
270
		return -ENODEV;
271 272
	}

273
	/* get the appropriate clk */
274
	clk = devm_clk_get(&pdev->dev, NULL);
275
	if (IS_ERR(clk)) {
276
		ret = PTR_ERR(clk);
277 278 279 280
		goto exit;
	}

	/* get the platform data */
281
	irq = platform_get_irq(pdev, 0);
282
	if (irq <= 0) {
283
		ret = -ENODEV;
284
		goto exit;
285 286
	}

287 288 289 290 291
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	addr = devm_ioremap_resource(&pdev->dev, mem);
	if (IS_ERR(addr)) {
		ret =  PTR_ERR(addr);
		goto exit;
292 293 294 295 296 297
	}

	/* allocate the c_can device */
	dev = alloc_c_can_dev();
	if (!dev) {
		ret = -ENOMEM;
298
		goto exit;
299 300 301
	}

	priv = netdev_priv(dev);
302
	switch (drvdata->id) {
303
	case BOSCH_C_CAN:
304 305 306 307 308
		priv->regs = reg_map_c_can;
		switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
		case IORESOURCE_MEM_32BIT:
			priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
			priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
309 310
			priv->read_reg32 = c_can_plat_read_reg32;
			priv->write_reg32 = c_can_plat_write_reg32;
311 312 313 314 315
			break;
		case IORESOURCE_MEM_16BIT:
		default:
			priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
			priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
316 317
			priv->read_reg32 = c_can_plat_read_reg32;
			priv->write_reg32 = c_can_plat_write_reg32;
318 319 320
			break;
		}
		break;
321
	case BOSCH_D_CAN:
322 323 324
		priv->regs = reg_map_d_can;
		priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
		priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
325 326
		priv->read_reg32 = d_can_plat_read_reg32;
		priv->write_reg32 = d_can_plat_write_reg32;
327

328 329
		/* Check if we need custom RAMINIT via syscon. Mostly for TI
		 * platforms. Only supported with DT boot.
330
		 */
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
		if (np && of_property_read_bool(np, "syscon-raminit")) {
			u32 id;
			struct c_can_raminit *raminit = &priv->raminit_sys;

			ret = -EINVAL;
			raminit->syscon = syscon_regmap_lookup_by_phandle(np,
									  "syscon-raminit");
			if (IS_ERR(raminit->syscon)) {
				/* can fail with -EPROBE_DEFER */
				ret = PTR_ERR(raminit->syscon);
				free_c_can_dev(dev);
				return ret;
			}

			if (of_property_read_u32_index(np, "syscon-raminit", 1,
						       &raminit->reg)) {
				dev_err(&pdev->dev,
					"couldn't get the RAMINIT reg. offset!\n");
				goto exit_free_device;
			}

			if (of_property_read_u32_index(np, "syscon-raminit", 2,
						       &id)) {
				dev_err(&pdev->dev,
					"couldn't get the CAN instance ID\n");
				goto exit_free_device;
			}

			if (id >= drvdata->raminit_num) {
				dev_err(&pdev->dev,
					"Invalid CAN instance ID\n");
				goto exit_free_device;
			}

			raminit->bits = drvdata->raminit_bits[id];
366
			raminit->needs_pulse = drvdata->raminit_pulse;
367 368 369

			priv->raminit = c_can_hw_raminit_syscon;
		} else {
370 371
			priv->raminit = c_can_hw_raminit;
		}
372 373 374 375 376
		break;
	default:
		ret = -EINVAL;
		goto exit_free_device;
	}
377

378
	dev->irq = irq;
379
	priv->base = addr;
380
	priv->device = &pdev->dev;
381 382
	priv->can.clock.freq = clk_get_rate(clk);
	priv->priv = clk;
383
	priv->type = drvdata->id;
384 385 386 387 388 389 390 391 392 393 394 395

	platform_set_drvdata(pdev, dev);
	SET_NETDEV_DEV(dev, &pdev->dev);

	ret = register_c_can_dev(dev);
	if (ret) {
		dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
			KBUILD_MODNAME, ret);
		goto exit_free_device;
	}

	dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
396
		 KBUILD_MODNAME, priv->base, dev->irq);
397 398 399 400 401 402 403 404 405 406
	return 0;

exit_free_device:
	free_c_can_dev(dev);
exit:
	dev_err(&pdev->dev, "probe failed\n");

	return ret;
}

B
Bill Pemberton 已提交
407
static int c_can_plat_remove(struct platform_device *pdev)
408 409 410 411 412 413 414 415 416 417
{
	struct net_device *dev = platform_get_drvdata(pdev);

	unregister_c_can_dev(dev);

	free_c_can_dev(dev);

	return 0;
}

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
#ifdef CONFIG_PM
static int c_can_suspend(struct platform_device *pdev, pm_message_t state)
{
	int ret;
	struct net_device *ndev = platform_get_drvdata(pdev);
	struct c_can_priv *priv = netdev_priv(ndev);

	if (priv->type != BOSCH_D_CAN) {
		dev_warn(&pdev->dev, "Not supported\n");
		return 0;
	}

	if (netif_running(ndev)) {
		netif_stop_queue(ndev);
		netif_device_detach(ndev);
	}

	ret = c_can_power_down(ndev);
	if (ret) {
		netdev_err(ndev, "failed to enter power down mode\n");
		return ret;
	}

	priv->can.state = CAN_STATE_SLEEPING;

	return 0;
}

static int c_can_resume(struct platform_device *pdev)
{
	int ret;
	struct net_device *ndev = platform_get_drvdata(pdev);
	struct c_can_priv *priv = netdev_priv(ndev);

	if (priv->type != BOSCH_D_CAN) {
		dev_warn(&pdev->dev, "Not supported\n");
		return 0;
	}

	ret = c_can_power_up(ndev);
	if (ret) {
		netdev_err(ndev, "Still in power down mode\n");
		return ret;
	}

	priv->can.state = CAN_STATE_ERROR_ACTIVE;

	if (netif_running(ndev)) {
		netif_device_attach(ndev);
		netif_start_queue(ndev);
	}

	return 0;
}
#else
#define c_can_suspend NULL
#define c_can_resume NULL
#endif

477 478 479
static struct platform_driver c_can_plat_driver = {
	.driver = {
		.name = KBUILD_MODNAME,
480
		.of_match_table = c_can_of_table,
481 482
	},
	.probe = c_can_plat_probe,
B
Bill Pemberton 已提交
483
	.remove = c_can_plat_remove,
484 485
	.suspend = c_can_suspend,
	.resume = c_can_resume,
486
	.id_table = c_can_id_table,
487 488
};

489
module_platform_driver(c_can_plat_driver);
490 491 492 493

MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");