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>
36 #include <linux/can/dev.h>
40 #define CAN_RAMINIT_START_MASK(i) (1 << (i))
43 * 16-bit c_can registers can be arranged differently in the memory
44 * architecture of different implementations. For example: 16-bit
45 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
46 * Handle the same by providing a common read/write interface.
48 static u16
c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv
*priv
,
51 return readw(priv
->base
+ priv
->regs
[index
]);
54 static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv
*priv
,
55 enum reg index
, u16 val
)
57 writew(val
, priv
->base
+ priv
->regs
[index
]);
60 static u16
c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv
*priv
,
63 return readw(priv
->base
+ 2 * priv
->regs
[index
]);
66 static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv
*priv
,
67 enum reg index
, u16 val
)
69 writew(val
, priv
->base
+ 2 * priv
->regs
[index
]);
72 static void c_can_hw_raminit(const struct c_can_priv
*priv
, bool enable
)
76 val
= readl(priv
->raminit_ctrlreg
);
78 val
|= CAN_RAMINIT_START_MASK(priv
->instance
);
80 val
&= ~CAN_RAMINIT_START_MASK(priv
->instance
);
81 writel(val
, priv
->raminit_ctrlreg
);
84 static struct platform_device_id c_can_id_table
[] = {
85 [BOSCH_C_CAN_PLATFORM
] = {
86 .name
= KBUILD_MODNAME
,
87 .driver_data
= BOSCH_C_CAN
,
91 .driver_data
= BOSCH_C_CAN
,
95 .driver_data
= BOSCH_D_CAN
,
99 MODULE_DEVICE_TABLE(platform
, c_can_id_table
);
101 static const struct of_device_id c_can_of_table
[] = {
102 { .compatible
= "bosch,c_can", .data
= &c_can_id_table
[BOSCH_C_CAN
] },
103 { .compatible
= "bosch,d_can", .data
= &c_can_id_table
[BOSCH_D_CAN
] },
106 MODULE_DEVICE_TABLE(of
, c_can_of_table
);
108 static int c_can_plat_probe(struct platform_device
*pdev
)
112 struct net_device
*dev
;
113 struct c_can_priv
*priv
;
114 const struct of_device_id
*match
;
115 const struct platform_device_id
*id
;
116 struct resource
*mem
, *res
;
120 if (pdev
->dev
.of_node
) {
121 match
= of_match_device(c_can_of_table
, &pdev
->dev
);
123 dev_err(&pdev
->dev
, "Failed to find matching dt id\n");
129 id
= platform_get_device_id(pdev
);
132 /* get the appropriate clk */
133 clk
= clk_get(&pdev
->dev
, NULL
);
135 dev_err(&pdev
->dev
, "no clock defined\n");
140 /* get the platform data */
141 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
142 irq
= platform_get_irq(pdev
, 0);
143 if (!mem
|| irq
<= 0) {
148 if (!request_mem_region(mem
->start
, resource_size(mem
),
150 dev_err(&pdev
->dev
, "resource unavailable\n");
155 addr
= ioremap(mem
->start
, resource_size(mem
));
157 dev_err(&pdev
->dev
, "failed to map can port\n");
159 goto exit_release_mem
;
162 /* allocate the c_can device */
163 dev
= alloc_c_can_dev();
169 priv
= netdev_priv(dev
);
170 switch (id
->driver_data
) {
172 priv
->regs
= reg_map_c_can
;
173 switch (mem
->flags
& IORESOURCE_MEM_TYPE_MASK
) {
174 case IORESOURCE_MEM_32BIT
:
175 priv
->read_reg
= c_can_plat_read_reg_aligned_to_32bit
;
176 priv
->write_reg
= c_can_plat_write_reg_aligned_to_32bit
;
178 case IORESOURCE_MEM_16BIT
:
180 priv
->read_reg
= c_can_plat_read_reg_aligned_to_16bit
;
181 priv
->write_reg
= c_can_plat_write_reg_aligned_to_16bit
;
186 priv
->regs
= reg_map_d_can
;
187 priv
->can
.ctrlmode_supported
|= CAN_CTRLMODE_3_SAMPLES
;
188 priv
->read_reg
= c_can_plat_read_reg_aligned_to_16bit
;
189 priv
->write_reg
= c_can_plat_write_reg_aligned_to_16bit
;
191 if (pdev
->dev
.of_node
)
192 priv
->instance
= of_alias_get_id(pdev
->dev
.of_node
, "d_can");
194 priv
->instance
= pdev
->id
;
196 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
197 priv
->raminit_ctrlreg
= devm_ioremap_resource(&pdev
->dev
, res
);
198 if (IS_ERR(priv
->raminit_ctrlreg
) || (int)priv
->instance
< 0)
199 dev_info(&pdev
->dev
, "control memory is not used for raminit\n");
201 priv
->raminit
= c_can_hw_raminit
;
205 goto exit_free_device
;
210 priv
->device
= &pdev
->dev
;
211 priv
->can
.clock
.freq
= clk_get_rate(clk
);
213 priv
->type
= id
->driver_data
;
215 platform_set_drvdata(pdev
, dev
);
216 SET_NETDEV_DEV(dev
, &pdev
->dev
);
218 ret
= register_c_can_dev(dev
);
220 dev_err(&pdev
->dev
, "registering %s failed (err=%d)\n",
221 KBUILD_MODNAME
, ret
);
222 goto exit_free_device
;
225 dev_info(&pdev
->dev
, "%s device registered (regs=%p, irq=%d)\n",
226 KBUILD_MODNAME
, priv
->base
, dev
->irq
);
234 release_mem_region(mem
->start
, resource_size(mem
));
238 dev_err(&pdev
->dev
, "probe failed\n");
243 static int c_can_plat_remove(struct platform_device
*pdev
)
245 struct net_device
*dev
= platform_get_drvdata(pdev
);
246 struct c_can_priv
*priv
= netdev_priv(dev
);
247 struct resource
*mem
;
249 unregister_c_can_dev(dev
);
254 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
255 release_mem_region(mem
->start
, resource_size(mem
));
263 static int c_can_suspend(struct platform_device
*pdev
, pm_message_t state
)
266 struct net_device
*ndev
= platform_get_drvdata(pdev
);
267 struct c_can_priv
*priv
= netdev_priv(ndev
);
269 if (priv
->type
!= BOSCH_D_CAN
) {
270 dev_warn(&pdev
->dev
, "Not supported\n");
274 if (netif_running(ndev
)) {
275 netif_stop_queue(ndev
);
276 netif_device_detach(ndev
);
279 ret
= c_can_power_down(ndev
);
281 netdev_err(ndev
, "failed to enter power down mode\n");
285 priv
->can
.state
= CAN_STATE_SLEEPING
;
290 static int c_can_resume(struct platform_device
*pdev
)
293 struct net_device
*ndev
= platform_get_drvdata(pdev
);
294 struct c_can_priv
*priv
= netdev_priv(ndev
);
296 if (priv
->type
!= BOSCH_D_CAN
) {
297 dev_warn(&pdev
->dev
, "Not supported\n");
301 ret
= c_can_power_up(ndev
);
303 netdev_err(ndev
, "Still in power down mode\n");
307 priv
->can
.state
= CAN_STATE_ERROR_ACTIVE
;
309 if (netif_running(ndev
)) {
310 netif_device_attach(ndev
);
311 netif_start_queue(ndev
);
317 #define c_can_suspend NULL
318 #define c_can_resume NULL
321 static struct platform_driver c_can_plat_driver
= {
323 .name
= KBUILD_MODNAME
,
324 .owner
= THIS_MODULE
,
325 .of_match_table
= of_match_ptr(c_can_of_table
),
327 .probe
= c_can_plat_probe
,
328 .remove
= c_can_plat_remove
,
329 .suspend
= c_can_suspend
,
330 .resume
= c_can_resume
,
331 .id_table
= c_can_id_table
,
334 module_platform_driver(c_can_plat_driver
);
336 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
337 MODULE_LICENSE("GPL v2");
338 MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");