2 * Platform CAN bus driver for Bosch C_CAN controller
4 * Copyright (C) 2010 ST Microelectronics
5 * Bhupesh Sharma <bhupesh.sharma@st.com>
7 * Borrowed heavily from the C_CAN driver originally written by:
9 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
10 * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
12 * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
13 * Bosch C_CAN user manual can be obtained from:
14 * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
15 * users_manual_c_can.pdf
17 * This file is licensed under the terms of the GNU General Public
18 * License version 2. This program is licensed "as is" without any
19 * warranty of any kind, whether express or implied.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/if_ether.h>
29 #include <linux/list.h>
31 #include <linux/platform_device.h>
32 #include <linux/clk.h>
34 #include <linux/of_device.h>
35 #include <linux/mfd/syscon.h>
36 #include <linux/regmap.h>
38 #include <linux/can/dev.h>
42 #define DCAN_RAM_INIT_BIT (1 << 3)
43 static DEFINE_SPINLOCK(raminit_lock);
45 * 16-bit c_can registers can be arranged differently in the memory
46 * architecture of different implementations. For example: 16-bit
47 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
48 * 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,
53 return readw(priv->base + priv->regs[index]);
56 static void c_can_plat_write_reg_aligned_to_16bit(const struct c_can_priv *priv,
57 enum reg index, u16 val)
59 writew(val, priv->base + priv->regs[index]);
62 static u16 c_can_plat_read_reg_aligned_to_32bit(const struct c_can_priv *priv,
65 return readw(priv->base + 2 * priv->regs[index]);
68 static void c_can_plat_write_reg_aligned_to_32bit(const struct c_can_priv *priv,
69 enum reg index, u16 val)
71 writew(val, priv->base + 2 * priv->regs[index]);
74 static void c_can_hw_raminit_wait_syscon(const struct c_can_priv *priv,
77 const struct c_can_raminit *raminit = &priv->raminit_sys;
81 /* We look only at the bits of our instance. */
87 regmap_read(raminit->syscon, raminit->reg, &ctrl);
88 if (timeout == 1000) {
89 dev_err(&priv->dev->dev, "%s: time out\n", __func__);
92 } while ((ctrl & mask) != val);
95 static void c_can_hw_raminit_syscon(const struct c_can_priv *priv, bool enable)
97 const struct c_can_raminit *raminit = &priv->raminit_sys;
101 spin_lock(&raminit_lock);
103 mask = 1 << raminit->bits.start | 1 << raminit->bits.done;
104 regmap_read(raminit->syscon, raminit->reg, &ctrl);
106 /* We clear the done and start bit first. The start bit is
107 * looking at the 0 -> transition, but is not self clearing;
108 * And we clear the init done bit as well.
109 * NOTE: DONE must be written with 1 to clear it.
111 ctrl &= ~(1 << raminit->bits.start);
112 ctrl |= 1 << raminit->bits.done;
113 regmap_write(raminit->syscon, raminit->reg, ctrl);
115 ctrl &= ~(1 << raminit->bits.done);
116 c_can_hw_raminit_wait_syscon(priv, mask, ctrl);
119 /* Set start bit and wait for the done bit. */
120 ctrl |= 1 << raminit->bits.start;
121 regmap_write(raminit->syscon, raminit->reg, ctrl);
123 /* clear START bit if start pulse is needed */
124 if (raminit->needs_pulse) {
125 ctrl &= ~(1 << raminit->bits.start);
126 regmap_write(raminit->syscon, raminit->reg, ctrl);
129 ctrl |= 1 << raminit->bits.done;
130 c_can_hw_raminit_wait_syscon(priv, mask, ctrl);
132 spin_unlock(&raminit_lock);
135 static u32 c_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
139 val = priv->read_reg(priv, index);
140 val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
145 static void c_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
148 priv->write_reg(priv, index + 1, val >> 16);
149 priv->write_reg(priv, index, val);
152 static u32 d_can_plat_read_reg32(const struct c_can_priv *priv, enum reg index)
154 return readl(priv->base + priv->regs[index]);
157 static void d_can_plat_write_reg32(const struct c_can_priv *priv, enum reg index,
160 writel(val, priv->base + priv->regs[index]);
163 static void c_can_hw_raminit_wait(const struct c_can_priv *priv, u32 mask)
165 while (priv->read_reg32(priv, C_CAN_FUNCTION_REG) & mask)
169 static void c_can_hw_raminit(const struct c_can_priv *priv, bool enable)
173 ctrl = priv->read_reg32(priv, C_CAN_FUNCTION_REG);
174 ctrl &= ~DCAN_RAM_INIT_BIT;
175 priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
176 c_can_hw_raminit_wait(priv, ctrl);
179 ctrl |= DCAN_RAM_INIT_BIT;
180 priv->write_reg32(priv, C_CAN_FUNCTION_REG, ctrl);
181 c_can_hw_raminit_wait(priv, ctrl);
185 static const struct c_can_driver_data c_can_drvdata = {
189 static const struct c_can_driver_data d_can_drvdata = {
193 static const struct raminit_bits dra7_raminit_bits[] = {
194 [0] = { .start = 3, .done = 1, },
195 [1] = { .start = 5, .done = 2, },
198 static const struct c_can_driver_data dra7_dcan_drvdata = {
200 .raminit_num = ARRAY_SIZE(dra7_raminit_bits),
201 .raminit_bits = dra7_raminit_bits,
202 .raminit_pulse = true,
205 static struct platform_device_id c_can_id_table[] = {
207 .name = KBUILD_MODNAME,
208 .driver_data = (kernel_ulong_t)&c_can_drvdata,
212 .driver_data = (kernel_ulong_t)&c_can_drvdata,
216 .driver_data = (kernel_ulong_t)&d_can_drvdata,
220 MODULE_DEVICE_TABLE(platform, c_can_id_table);
222 static const struct of_device_id c_can_of_table[] = {
223 { .compatible = "bosch,c_can", .data = &c_can_drvdata },
224 { .compatible = "bosch,d_can", .data = &d_can_drvdata },
225 { .compatible = "ti,dra7-d_can", .data = &dra7_dcan_drvdata },
228 MODULE_DEVICE_TABLE(of, c_can_of_table);
230 static int c_can_plat_probe(struct platform_device *pdev)
234 struct net_device *dev;
235 struct c_can_priv *priv;
236 const struct of_device_id *match;
237 struct resource *mem;
240 const struct c_can_driver_data *drvdata;
241 struct device_node *np = pdev->dev.of_node;
243 match = of_match_device(c_can_of_table, &pdev->dev);
245 drvdata = match->data;
246 } else if (pdev->id_entry->driver_data) {
247 drvdata = (struct c_can_driver_data *)
248 platform_get_device_id(pdev)->driver_data;
253 /* get the appropriate clk */
254 clk = devm_clk_get(&pdev->dev, NULL);
260 /* get the platform data */
261 irq = platform_get_irq(pdev, 0);
267 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
268 addr = devm_ioremap_resource(&pdev->dev, mem);
274 /* allocate the c_can device */
275 dev = alloc_c_can_dev();
281 priv = netdev_priv(dev);
282 switch (drvdata->id) {
284 priv->regs = reg_map_c_can;
285 switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
286 case IORESOURCE_MEM_32BIT:
287 priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
288 priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
289 priv->read_reg32 = c_can_plat_read_reg32;
290 priv->write_reg32 = c_can_plat_write_reg32;
292 case IORESOURCE_MEM_16BIT:
294 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
295 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
296 priv->read_reg32 = c_can_plat_read_reg32;
297 priv->write_reg32 = c_can_plat_write_reg32;
302 priv->regs = reg_map_d_can;
303 priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
304 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
305 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
306 priv->read_reg32 = d_can_plat_read_reg32;
307 priv->write_reg32 = d_can_plat_write_reg32;
309 /* Check if we need custom RAMINIT via syscon. Mostly for TI
310 * platforms. Only supported with DT boot.
312 if (np && of_property_read_bool(np, "syscon-raminit")) {
314 struct c_can_raminit *raminit = &priv->raminit_sys;
317 raminit->syscon = syscon_regmap_lookup_by_phandle(np,
319 if (IS_ERR(raminit->syscon)) {
320 /* can fail with -EPROBE_DEFER */
321 ret = PTR_ERR(raminit->syscon);
326 if (of_property_read_u32_index(np, "syscon-raminit", 1,
329 "couldn't get the RAMINIT reg. offset!\n");
330 goto exit_free_device;
333 if (of_property_read_u32_index(np, "syscon-raminit", 2,
336 "couldn't get the CAN instance ID\n");
337 goto exit_free_device;
340 if (id >= drvdata->raminit_num) {
342 "Invalid CAN instance ID\n");
343 goto exit_free_device;
346 raminit->bits = drvdata->raminit_bits[id];
347 raminit->needs_pulse = drvdata->raminit_pulse;
349 priv->raminit = c_can_hw_raminit_syscon;
351 priv->raminit = c_can_hw_raminit;
356 goto exit_free_device;
361 priv->device = &pdev->dev;
362 priv->can.clock.freq = clk_get_rate(clk);
364 priv->type = drvdata->id;
366 platform_set_drvdata(pdev, dev);
367 SET_NETDEV_DEV(dev, &pdev->dev);
369 ret = register_c_can_dev(dev);
371 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
372 KBUILD_MODNAME, ret);
373 goto exit_free_device;
376 dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
377 KBUILD_MODNAME, priv->base, dev->irq);
383 dev_err(&pdev->dev, "probe failed\n");
388 static int c_can_plat_remove(struct platform_device *pdev)
390 struct net_device *dev = platform_get_drvdata(pdev);
392 unregister_c_can_dev(dev);
400 static int c_can_suspend(struct platform_device *pdev, pm_message_t state)
403 struct net_device *ndev = platform_get_drvdata(pdev);
404 struct c_can_priv *priv = netdev_priv(ndev);
406 if (priv->type != BOSCH_D_CAN) {
407 dev_warn(&pdev->dev, "Not supported\n");
411 if (netif_running(ndev)) {
412 netif_stop_queue(ndev);
413 netif_device_detach(ndev);
416 ret = c_can_power_down(ndev);
418 netdev_err(ndev, "failed to enter power down mode\n");
422 priv->can.state = CAN_STATE_SLEEPING;
427 static int c_can_resume(struct platform_device *pdev)
430 struct net_device *ndev = platform_get_drvdata(pdev);
431 struct c_can_priv *priv = netdev_priv(ndev);
433 if (priv->type != BOSCH_D_CAN) {
434 dev_warn(&pdev->dev, "Not supported\n");
438 ret = c_can_power_up(ndev);
440 netdev_err(ndev, "Still in power down mode\n");
444 priv->can.state = CAN_STATE_ERROR_ACTIVE;
446 if (netif_running(ndev)) {
447 netif_device_attach(ndev);
448 netif_start_queue(ndev);
454 #define c_can_suspend NULL
455 #define c_can_resume NULL
458 static struct platform_driver c_can_plat_driver = {
460 .name = KBUILD_MODNAME,
461 .owner = THIS_MODULE,
462 .of_match_table = c_can_of_table,
464 .probe = c_can_plat_probe,
465 .remove = c_can_plat_remove,
466 .suspend = c_can_suspend,
467 .resume = c_can_resume,
468 .id_table = c_can_id_table,
471 module_platform_driver(c_can_plat_driver);
473 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
474 MODULE_LICENSE("GPL v2");
475 MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");