2 * Driver for CC770 and AN82527 CAN controllers on the platform bus
4 * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the version 2 of the GNU General Public License
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 * If platform data are used you should have similar definitions
18 * in your board-specific code:
20 * static struct cc770_platform_data myboard_cc770_pdata = {
21 * .osc_freq = 16000000,
27 * Please see include/linux/can/platform/cc770.h for description of
30 * If the device tree is used, you need a CAN node definition in your
31 * DTS file similar to:
34 * compatible = "bosch,cc770";
35 * reg = <3 0x100 0x80>;
37 * interrupt-parent = <&mpic>;
38 * bosch,external-clock-frequency = <16000000>;
41 * See "Documentation/devicetree/bindings/net/can/cc770.txt" for further
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/interrupt.h>
48 #include <linux/netdevice.h>
49 #include <linux/delay.h>
50 #include <linux/platform_device.h>
52 #include <linux/can.h>
53 #include <linux/can/dev.h>
54 #include <linux/can/platform/cc770.h>
58 #define DRV_NAME "cc770_platform"
60 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
61 MODULE_DESCRIPTION("Socket-CAN driver for CC770 on the platform bus");
62 MODULE_LICENSE("GPL v2");
63 MODULE_ALIAS("platform:" DRV_NAME
);
65 #define CC770_PLATFORM_CAN_CLOCK 16000000
67 static u8
cc770_platform_read_reg(const struct cc770_priv
*priv
, int reg
)
69 return ioread8(priv
->reg_base
+ reg
);
72 static void cc770_platform_write_reg(const struct cc770_priv
*priv
, int reg
,
75 iowrite8(val
, priv
->reg_base
+ reg
);
78 static int cc770_get_of_node_data(struct platform_device
*pdev
,
79 struct cc770_priv
*priv
)
81 struct device_node
*np
= pdev
->dev
.of_node
;
86 prop
= of_get_property(np
, "bosch,external-clock-frequency",
88 if (prop
&& (prop_size
== sizeof(u32
)))
91 clkext
= CC770_PLATFORM_CAN_CLOCK
; /* default */
92 priv
->can
.clock
.freq
= clkext
;
94 /* The system clock may not exceed 10 MHz */
95 if (priv
->can
.clock
.freq
> 10000000) {
96 priv
->cpu_interface
|= CPUIF_DSC
;
97 priv
->can
.clock
.freq
/= 2;
100 /* The memory clock may not exceed 8 MHz */
101 if (priv
->can
.clock
.freq
> 8000000)
102 priv
->cpu_interface
|= CPUIF_DMC
;
104 if (of_get_property(np
, "bosch,divide-memory-clock", NULL
))
105 priv
->cpu_interface
|= CPUIF_DMC
;
106 if (of_get_property(np
, "bosch,iso-low-speed-mux", NULL
))
107 priv
->cpu_interface
|= CPUIF_MUX
;
109 if (!of_get_property(np
, "bosch,no-comperator-bypass", NULL
))
110 priv
->bus_config
|= BUSCFG_CBY
;
111 if (of_get_property(np
, "bosch,disconnect-rx0-input", NULL
))
112 priv
->bus_config
|= BUSCFG_DR0
;
113 if (of_get_property(np
, "bosch,disconnect-rx1-input", NULL
))
114 priv
->bus_config
|= BUSCFG_DR1
;
115 if (of_get_property(np
, "bosch,disconnect-tx1-output", NULL
))
116 priv
->bus_config
|= BUSCFG_DT1
;
117 if (of_get_property(np
, "bosch,polarity-dominant", NULL
))
118 priv
->bus_config
|= BUSCFG_POL
;
120 prop
= of_get_property(np
, "bosch,clock-out-frequency", &prop_size
);
121 if (prop
&& (prop_size
== sizeof(u32
)) && *prop
> 0) {
122 u32 cdv
= clkext
/ *prop
;
125 if (cdv
> 0 && cdv
< 16) {
126 priv
->cpu_interface
|= CPUIF_CEN
;
127 priv
->clkout
|= (cdv
- 1) & CLKOUT_CD_MASK
;
129 prop
= of_get_property(np
, "bosch,slew-rate",
131 if (prop
&& (prop_size
== sizeof(u32
))) {
134 /* Determine default slew rate */
135 slew
= (CLKOUT_SL_MASK
>>
137 ((cdv
* clkext
- 1) / 8000000);
141 priv
->clkout
|= (slew
<< CLKOUT_SL_SHIFT
) &
144 dev_dbg(&pdev
->dev
, "invalid clock-out-frequency\n");
151 static int cc770_get_platform_data(struct platform_device
*pdev
,
152 struct cc770_priv
*priv
)
155 struct cc770_platform_data
*pdata
= pdev
->dev
.platform_data
;
157 priv
->can
.clock
.freq
= pdata
->osc_freq
;
158 if (priv
->cpu_interface
& CPUIF_DSC
)
159 priv
->can
.clock
.freq
/= 2;
160 priv
->clkout
= pdata
->cor
;
161 priv
->bus_config
= pdata
->bcr
;
162 priv
->cpu_interface
= pdata
->cir
;
167 static int cc770_platform_probe(struct platform_device
*pdev
)
169 struct net_device
*dev
;
170 struct cc770_priv
*priv
;
171 struct resource
*mem
;
172 resource_size_t mem_size
;
176 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
177 irq
= platform_get_irq(pdev
, 0);
178 if (!mem
|| irq
<= 0)
181 mem_size
= resource_size(mem
);
182 if (!request_mem_region(mem
->start
, mem_size
, pdev
->name
))
185 base
= ioremap(mem
->start
, mem_size
);
188 goto exit_release_mem
;
191 dev
= alloc_cc770dev(0);
198 priv
= netdev_priv(dev
);
199 priv
->read_reg
= cc770_platform_read_reg
;
200 priv
->write_reg
= cc770_platform_write_reg
;
201 priv
->irq_flags
= IRQF_SHARED
;
202 priv
->reg_base
= base
;
204 if (pdev
->dev
.of_node
)
205 err
= cc770_get_of_node_data(pdev
, priv
);
206 else if (pdev
->dev
.platform_data
)
207 err
= cc770_get_platform_data(pdev
, priv
);
211 goto exit_free_cc770
;
214 "reg_base=0x%p irq=%d clock=%d cpu_interface=0x%02x "
215 "bus_config=0x%02x clkout=0x%02x\n",
216 priv
->reg_base
, dev
->irq
, priv
->can
.clock
.freq
,
217 priv
->cpu_interface
, priv
->bus_config
, priv
->clkout
);
219 platform_set_drvdata(pdev
, dev
);
220 SET_NETDEV_DEV(dev
, &pdev
->dev
);
222 err
= register_cc770dev(dev
);
225 "couldn't register CC700 device (err=%d)\n", err
);
226 goto exit_free_cc770
;
236 release_mem_region(mem
->start
, mem_size
);
241 static int cc770_platform_remove(struct platform_device
*pdev
)
243 struct net_device
*dev
= platform_get_drvdata(pdev
);
244 struct cc770_priv
*priv
= netdev_priv(dev
);
245 struct resource
*mem
;
247 unregister_cc770dev(dev
);
248 iounmap(priv
->reg_base
);
251 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
252 release_mem_region(mem
->start
, resource_size(mem
));
257 static struct of_device_id cc770_platform_table
[] = {
258 {.compatible
= "bosch,cc770"}, /* CC770 from Bosch */
259 {.compatible
= "intc,82527"}, /* AN82527 from Intel CP */
262 MODULE_DEVICE_TABLE(of
, cc770_platform_table
);
264 static struct platform_driver cc770_platform_driver
= {
267 .owner
= THIS_MODULE
,
268 .of_match_table
= cc770_platform_table
,
270 .probe
= cc770_platform_probe
,
271 .remove
= cc770_platform_remove
,
274 module_platform_driver(cc770_platform_driver
);