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 BIT(3)
44 static DEFINE_SPINLOCK(raminit_lock
);
46 /* 16-bit c_can registers can be arranged differently in the memory
47 * architecture of different implementations. For example: 16-bit
48 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
49 * Handle the same by providing a common read/write interface.
51 static u16
c_can_plat_read_reg_aligned_to_16bit(const struct c_can_priv
*priv
,
54 return readw(priv
->base
+ priv
->regs
[index
]);
57 static void c_can_plat_write_reg_aligned_to_16bit(const struct c_can_priv
*priv
,
58 enum reg index
, u16 val
)
60 writew(val
, priv
->base
+ priv
->regs
[index
]);
63 static u16
c_can_plat_read_reg_aligned_to_32bit(const struct c_can_priv
*priv
,
66 return readw(priv
->base
+ 2 * priv
->regs
[index
]);
69 static void c_can_plat_write_reg_aligned_to_32bit(const struct c_can_priv
*priv
,
70 enum reg index
, u16 val
)
72 writew(val
, priv
->base
+ 2 * priv
->regs
[index
]);
75 static void c_can_hw_raminit_wait_syscon(const struct c_can_priv
*priv
,
78 const struct c_can_raminit
*raminit
= &priv
->raminit_sys
;
82 /* We look only at the bits of our instance. */
88 regmap_read(raminit
->syscon
, raminit
->reg
, &ctrl
);
89 if (timeout
== 1000) {
90 dev_err(&priv
->dev
->dev
, "%s: time out\n", __func__
);
93 } while ((ctrl
& mask
) != val
);
96 static void c_can_hw_raminit_syscon(const struct c_can_priv
*priv
, bool enable
)
98 const struct c_can_raminit
*raminit
= &priv
->raminit_sys
;
102 spin_lock(&raminit_lock
);
104 mask
= 1 << raminit
->bits
.start
| 1 << raminit
->bits
.done
;
105 regmap_read(raminit
->syscon
, raminit
->reg
, &ctrl
);
107 /* We clear the start bit first. The start bit is
108 * looking at the 0 -> transition, but is not self clearing;
109 * NOTE: DONE must be written with 1 to clear it.
110 * We can't clear the DONE bit here using regmap_update_bits()
111 * as it will bypass the write if initial condition is START:0 DONE:1
112 * e.g. on DRA7 which needs START pulse.
114 ctrl
&= ~mask
; /* START = 0, DONE = 0 */
115 regmap_update_bits(raminit
->syscon
, raminit
->reg
, mask
, ctrl
);
117 /* check if START bit is 0. Ignore DONE bit for now
118 * as it can be either 0 or 1.
120 c_can_hw_raminit_wait_syscon(priv
, 1 << raminit
->bits
.start
, ctrl
);
123 /* Clear DONE bit & set START bit. */
124 ctrl
|= 1 << raminit
->bits
.start
;
125 /* DONE must be written with 1 to clear it */
126 ctrl
|= 1 << raminit
->bits
.done
;
127 regmap_update_bits(raminit
->syscon
, raminit
->reg
, mask
, ctrl
);
128 /* prevent further clearing of DONE bit */
129 ctrl
&= ~(1 << raminit
->bits
.done
);
130 /* clear START bit if start pulse is needed */
131 if (raminit
->needs_pulse
) {
132 ctrl
&= ~(1 << raminit
->bits
.start
);
133 regmap_update_bits(raminit
->syscon
, raminit
->reg
,
137 ctrl
|= 1 << raminit
->bits
.done
;
138 c_can_hw_raminit_wait_syscon(priv
, mask
, ctrl
);
140 spin_unlock(&raminit_lock
);
143 static u32
c_can_plat_read_reg32(const struct c_can_priv
*priv
, enum reg index
)
147 val
= priv
->read_reg(priv
, index
);
148 val
|= ((u32
)priv
->read_reg(priv
, index
+ 1)) << 16;
153 static void c_can_plat_write_reg32(const struct c_can_priv
*priv
,
154 enum reg index
, u32 val
)
156 priv
->write_reg(priv
, index
+ 1, val
>> 16);
157 priv
->write_reg(priv
, index
, val
);
160 static u32
d_can_plat_read_reg32(const struct c_can_priv
*priv
, enum reg index
)
162 return readl(priv
->base
+ priv
->regs
[index
]);
165 static void d_can_plat_write_reg32(const struct c_can_priv
*priv
,
166 enum reg index
, u32 val
)
168 writel(val
, priv
->base
+ priv
->regs
[index
]);
171 static void c_can_hw_raminit_wait(const struct c_can_priv
*priv
, u32 mask
)
173 while (priv
->read_reg32(priv
, C_CAN_FUNCTION_REG
) & mask
)
177 static void c_can_hw_raminit(const struct c_can_priv
*priv
, bool enable
)
181 ctrl
= priv
->read_reg32(priv
, C_CAN_FUNCTION_REG
);
182 ctrl
&= ~DCAN_RAM_INIT_BIT
;
183 priv
->write_reg32(priv
, C_CAN_FUNCTION_REG
, ctrl
);
184 c_can_hw_raminit_wait(priv
, ctrl
);
187 ctrl
|= DCAN_RAM_INIT_BIT
;
188 priv
->write_reg32(priv
, C_CAN_FUNCTION_REG
, ctrl
);
189 c_can_hw_raminit_wait(priv
, ctrl
);
193 static const struct c_can_driver_data c_can_drvdata
= {
197 static const struct c_can_driver_data d_can_drvdata
= {
201 static const struct raminit_bits dra7_raminit_bits
[] = {
202 [0] = { .start
= 3, .done
= 1, },
203 [1] = { .start
= 5, .done
= 2, },
206 static const struct c_can_driver_data dra7_dcan_drvdata
= {
208 .raminit_num
= ARRAY_SIZE(dra7_raminit_bits
),
209 .raminit_bits
= dra7_raminit_bits
,
210 .raminit_pulse
= true,
213 static const struct raminit_bits am3352_raminit_bits
[] = {
214 [0] = { .start
= 0, .done
= 8, },
215 [1] = { .start
= 1, .done
= 9, },
218 static const struct c_can_driver_data am3352_dcan_drvdata
= {
220 .raminit_num
= ARRAY_SIZE(am3352_raminit_bits
),
221 .raminit_bits
= am3352_raminit_bits
,
224 static const struct platform_device_id c_can_id_table
[] = {
226 .name
= KBUILD_MODNAME
,
227 .driver_data
= (kernel_ulong_t
)&c_can_drvdata
,
231 .driver_data
= (kernel_ulong_t
)&c_can_drvdata
,
235 .driver_data
= (kernel_ulong_t
)&d_can_drvdata
,
239 MODULE_DEVICE_TABLE(platform
, c_can_id_table
);
241 static const struct of_device_id c_can_of_table
[] = {
242 { .compatible
= "bosch,c_can", .data
= &c_can_drvdata
},
243 { .compatible
= "bosch,d_can", .data
= &d_can_drvdata
},
244 { .compatible
= "ti,dra7-d_can", .data
= &dra7_dcan_drvdata
},
245 { .compatible
= "ti,am3352-d_can", .data
= &am3352_dcan_drvdata
},
246 { .compatible
= "ti,am4372-d_can", .data
= &am3352_dcan_drvdata
},
249 MODULE_DEVICE_TABLE(of
, c_can_of_table
);
251 static int c_can_plat_probe(struct platform_device
*pdev
)
255 struct net_device
*dev
;
256 struct c_can_priv
*priv
;
257 const struct of_device_id
*match
;
258 struct resource
*mem
;
261 const struct c_can_driver_data
*drvdata
;
262 struct device_node
*np
= pdev
->dev
.of_node
;
264 match
= of_match_device(c_can_of_table
, &pdev
->dev
);
266 drvdata
= match
->data
;
267 } else if (pdev
->id_entry
->driver_data
) {
268 drvdata
= (struct c_can_driver_data
*)
269 platform_get_device_id(pdev
)->driver_data
;
274 /* get the appropriate clk */
275 clk
= devm_clk_get(&pdev
->dev
, NULL
);
281 /* get the platform data */
282 irq
= platform_get_irq(pdev
, 0);
288 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
289 addr
= devm_ioremap_resource(&pdev
->dev
, mem
);
295 /* allocate the c_can device */
296 dev
= alloc_c_can_dev();
302 priv
= netdev_priv(dev
);
303 switch (drvdata
->id
) {
305 priv
->regs
= reg_map_c_can
;
306 switch (mem
->flags
& IORESOURCE_MEM_TYPE_MASK
) {
307 case IORESOURCE_MEM_32BIT
:
308 priv
->read_reg
= c_can_plat_read_reg_aligned_to_32bit
;
309 priv
->write_reg
= c_can_plat_write_reg_aligned_to_32bit
;
310 priv
->read_reg32
= c_can_plat_read_reg32
;
311 priv
->write_reg32
= c_can_plat_write_reg32
;
313 case IORESOURCE_MEM_16BIT
:
315 priv
->read_reg
= c_can_plat_read_reg_aligned_to_16bit
;
316 priv
->write_reg
= c_can_plat_write_reg_aligned_to_16bit
;
317 priv
->read_reg32
= c_can_plat_read_reg32
;
318 priv
->write_reg32
= c_can_plat_write_reg32
;
323 priv
->regs
= reg_map_d_can
;
324 priv
->read_reg
= c_can_plat_read_reg_aligned_to_16bit
;
325 priv
->write_reg
= c_can_plat_write_reg_aligned_to_16bit
;
326 priv
->read_reg32
= d_can_plat_read_reg32
;
327 priv
->write_reg32
= d_can_plat_write_reg32
;
329 /* Check if we need custom RAMINIT via syscon. Mostly for TI
330 * platforms. Only supported with DT boot.
332 if (np
&& of_property_read_bool(np
, "syscon-raminit")) {
334 struct c_can_raminit
*raminit
= &priv
->raminit_sys
;
337 raminit
->syscon
= syscon_regmap_lookup_by_phandle(np
,
339 if (IS_ERR(raminit
->syscon
)) {
340 /* can fail with -EPROBE_DEFER */
341 ret
= PTR_ERR(raminit
->syscon
);
346 if (of_property_read_u32_index(np
, "syscon-raminit", 1,
349 "couldn't get the RAMINIT reg. offset!\n");
350 goto exit_free_device
;
353 if (of_property_read_u32_index(np
, "syscon-raminit", 2,
356 "couldn't get the CAN instance ID\n");
357 goto exit_free_device
;
360 if (id
>= drvdata
->raminit_num
) {
362 "Invalid CAN instance ID\n");
363 goto exit_free_device
;
366 raminit
->bits
= drvdata
->raminit_bits
[id
];
367 raminit
->needs_pulse
= drvdata
->raminit_pulse
;
369 priv
->raminit
= c_can_hw_raminit_syscon
;
371 priv
->raminit
= c_can_hw_raminit
;
376 goto exit_free_device
;
381 priv
->device
= &pdev
->dev
;
382 priv
->can
.clock
.freq
= clk_get_rate(clk
);
384 priv
->type
= drvdata
->id
;
386 platform_set_drvdata(pdev
, dev
);
387 SET_NETDEV_DEV(dev
, &pdev
->dev
);
389 ret
= register_c_can_dev(dev
);
391 dev_err(&pdev
->dev
, "registering %s failed (err=%d)\n",
392 KBUILD_MODNAME
, ret
);
393 goto exit_free_device
;
396 dev_info(&pdev
->dev
, "%s device registered (regs=%p, irq=%d)\n",
397 KBUILD_MODNAME
, priv
->base
, dev
->irq
);
403 dev_err(&pdev
->dev
, "probe failed\n");
408 static int c_can_plat_remove(struct platform_device
*pdev
)
410 struct net_device
*dev
= platform_get_drvdata(pdev
);
412 unregister_c_can_dev(dev
);
420 static int c_can_suspend(struct platform_device
*pdev
, pm_message_t state
)
423 struct net_device
*ndev
= platform_get_drvdata(pdev
);
424 struct c_can_priv
*priv
= netdev_priv(ndev
);
426 if (priv
->type
!= BOSCH_D_CAN
) {
427 dev_warn(&pdev
->dev
, "Not supported\n");
431 if (netif_running(ndev
)) {
432 netif_stop_queue(ndev
);
433 netif_device_detach(ndev
);
436 ret
= c_can_power_down(ndev
);
438 netdev_err(ndev
, "failed to enter power down mode\n");
442 priv
->can
.state
= CAN_STATE_SLEEPING
;
447 static int c_can_resume(struct platform_device
*pdev
)
450 struct net_device
*ndev
= platform_get_drvdata(pdev
);
451 struct c_can_priv
*priv
= netdev_priv(ndev
);
453 if (priv
->type
!= BOSCH_D_CAN
) {
454 dev_warn(&pdev
->dev
, "Not supported\n");
458 ret
= c_can_power_up(ndev
);
460 netdev_err(ndev
, "Still in power down mode\n");
464 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
466 if (netif_running(ndev
)) {
467 netif_device_attach(ndev
);
468 netif_start_queue(ndev
);
474 #define c_can_suspend NULL
475 #define c_can_resume NULL
478 static struct platform_driver c_can_plat_driver
= {
480 .name
= KBUILD_MODNAME
,
481 .of_match_table
= c_can_of_table
,
483 .probe
= c_can_plat_probe
,
484 .remove
= c_can_plat_remove
,
485 .suspend
= c_can_suspend
,
486 .resume
= c_can_resume
,
487 .id_table
= c_can_id_table
,
490 module_platform_driver(c_can_plat_driver
);
492 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
493 MODULE_LICENSE("GPL v2");
494 MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");