1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for CC770 and AN82527 CAN controllers on the platform bus
5 * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com>
9 * If platform data are used you should have similar definitions
10 * in your board-specific code:
12 * static struct cc770_platform_data myboard_cc770_pdata = {
13 * .osc_freq = 16000000,
19 * Please see include/linux/can/platform/cc770.h for description of
22 * If the device tree is used, you need a CAN node definition in your
23 * DTS file similar to:
26 * compatible = "bosch,cc770";
27 * reg = <3 0x100 0x80>;
29 * interrupt-parent = <&mpic>;
30 * bosch,external-clock-frequency = <16000000>;
33 * See "Documentation/devicetree/bindings/net/can/cc770.txt" for further
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/interrupt.h>
40 #include <linux/netdevice.h>
41 #include <linux/delay.h>
42 #include <linux/platform_device.h>
44 #include <linux/can.h>
45 #include <linux/can/dev.h>
46 #include <linux/can/platform/cc770.h>
50 #define DRV_NAME "cc770_platform"
52 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
53 MODULE_DESCRIPTION("Socket-CAN driver for CC770 on the platform bus");
54 MODULE_LICENSE("GPL v2");
55 MODULE_ALIAS("platform:" DRV_NAME
);
57 #define CC770_PLATFORM_CAN_CLOCK 16000000
59 static u8
cc770_platform_read_reg(const struct cc770_priv
*priv
, int reg
)
61 return ioread8(priv
->reg_base
+ reg
);
64 static void cc770_platform_write_reg(const struct cc770_priv
*priv
, int reg
,
67 iowrite8(val
, priv
->reg_base
+ reg
);
70 static int cc770_get_of_node_data(struct platform_device
*pdev
,
71 struct cc770_priv
*priv
)
73 struct device_node
*np
= pdev
->dev
.of_node
;
78 prop
= of_get_property(np
, "bosch,external-clock-frequency",
80 if (prop
&& (prop_size
== sizeof(u32
)))
83 clkext
= CC770_PLATFORM_CAN_CLOCK
; /* default */
84 priv
->can
.clock
.freq
= clkext
;
86 /* The system clock may not exceed 10 MHz */
87 if (priv
->can
.clock
.freq
> 10000000) {
88 priv
->cpu_interface
|= CPUIF_DSC
;
89 priv
->can
.clock
.freq
/= 2;
92 /* The memory clock may not exceed 8 MHz */
93 if (priv
->can
.clock
.freq
> 8000000)
94 priv
->cpu_interface
|= CPUIF_DMC
;
96 if (of_get_property(np
, "bosch,divide-memory-clock", NULL
))
97 priv
->cpu_interface
|= CPUIF_DMC
;
98 if (of_get_property(np
, "bosch,iso-low-speed-mux", NULL
))
99 priv
->cpu_interface
|= CPUIF_MUX
;
101 if (!of_get_property(np
, "bosch,no-comperator-bypass", NULL
))
102 priv
->bus_config
|= BUSCFG_CBY
;
103 if (of_get_property(np
, "bosch,disconnect-rx0-input", NULL
))
104 priv
->bus_config
|= BUSCFG_DR0
;
105 if (of_get_property(np
, "bosch,disconnect-rx1-input", NULL
))
106 priv
->bus_config
|= BUSCFG_DR1
;
107 if (of_get_property(np
, "bosch,disconnect-tx1-output", NULL
))
108 priv
->bus_config
|= BUSCFG_DT1
;
109 if (of_get_property(np
, "bosch,polarity-dominant", NULL
))
110 priv
->bus_config
|= BUSCFG_POL
;
112 prop
= of_get_property(np
, "bosch,clock-out-frequency", &prop_size
);
113 if (prop
&& (prop_size
== sizeof(u32
)) && *prop
> 0) {
114 u32 cdv
= clkext
/ *prop
;
117 if (cdv
> 0 && cdv
< 16) {
118 priv
->cpu_interface
|= CPUIF_CEN
;
119 priv
->clkout
|= (cdv
- 1) & CLKOUT_CD_MASK
;
121 prop
= of_get_property(np
, "bosch,slew-rate",
123 if (prop
&& (prop_size
== sizeof(u32
))) {
126 /* Determine default slew rate */
127 slew
= (CLKOUT_SL_MASK
>>
129 ((cdv
* clkext
- 1) / 8000000);
133 priv
->clkout
|= (slew
<< CLKOUT_SL_SHIFT
) &
136 dev_dbg(&pdev
->dev
, "invalid clock-out-frequency\n");
143 static int cc770_get_platform_data(struct platform_device
*pdev
,
144 struct cc770_priv
*priv
)
147 struct cc770_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
149 priv
->can
.clock
.freq
= pdata
->osc_freq
;
150 if (priv
->cpu_interface
& CPUIF_DSC
)
151 priv
->can
.clock
.freq
/= 2;
152 priv
->clkout
= pdata
->cor
;
153 priv
->bus_config
= pdata
->bcr
;
154 priv
->cpu_interface
= pdata
->cir
;
159 static int cc770_platform_probe(struct platform_device
*pdev
)
161 struct net_device
*dev
;
162 struct cc770_priv
*priv
;
163 struct resource
*mem
;
164 resource_size_t mem_size
;
168 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
169 irq
= platform_get_irq(pdev
, 0);
170 if (!mem
|| irq
<= 0)
173 mem_size
= resource_size(mem
);
174 if (!request_mem_region(mem
->start
, mem_size
, pdev
->name
))
177 base
= ioremap(mem
->start
, mem_size
);
180 goto exit_release_mem
;
183 dev
= alloc_cc770dev(0);
190 priv
= netdev_priv(dev
);
191 priv
->read_reg
= cc770_platform_read_reg
;
192 priv
->write_reg
= cc770_platform_write_reg
;
193 priv
->irq_flags
= IRQF_SHARED
;
194 priv
->reg_base
= base
;
196 if (pdev
->dev
.of_node
)
197 err
= cc770_get_of_node_data(pdev
, priv
);
198 else if (dev_get_platdata(&pdev
->dev
))
199 err
= cc770_get_platform_data(pdev
, priv
);
203 goto exit_free_cc770
;
206 "reg_base=0x%p irq=%d clock=%d cpu_interface=0x%02x "
207 "bus_config=0x%02x clkout=0x%02x\n",
208 priv
->reg_base
, dev
->irq
, priv
->can
.clock
.freq
,
209 priv
->cpu_interface
, priv
->bus_config
, priv
->clkout
);
211 platform_set_drvdata(pdev
, dev
);
212 SET_NETDEV_DEV(dev
, &pdev
->dev
);
214 err
= register_cc770dev(dev
);
217 "couldn't register CC700 device (err=%d)\n", err
);
218 goto exit_free_cc770
;
228 release_mem_region(mem
->start
, mem_size
);
233 static int cc770_platform_remove(struct platform_device
*pdev
)
235 struct net_device
*dev
= platform_get_drvdata(pdev
);
236 struct cc770_priv
*priv
= netdev_priv(dev
);
237 struct resource
*mem
;
239 unregister_cc770dev(dev
);
240 iounmap(priv
->reg_base
);
243 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
244 release_mem_region(mem
->start
, resource_size(mem
));
249 static const struct of_device_id cc770_platform_table
[] = {
250 {.compatible
= "bosch,cc770"}, /* CC770 from Bosch */
251 {.compatible
= "intc,82527"}, /* AN82527 from Intel CP */
254 MODULE_DEVICE_TABLE(of
, cc770_platform_table
);
256 static struct platform_driver cc770_platform_driver
= {
259 .of_match_table
= cc770_platform_table
,
261 .probe
= cc770_platform_probe
,
262 .remove
= cc770_platform_remove
,
265 module_platform_driver(cc770_platform_driver
);