1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2005 Sascha Hauer, Pengutronix
4 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/netdevice.h>
11 #include <linux/delay.h>
12 #include <linux/pci.h>
13 #include <linux/platform_device.h>
14 #include <linux/irq.h>
15 #include <linux/can/dev.h>
16 #include <linux/can/platform/sja1000.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
24 #define DRV_NAME "sja1000_platform"
25 #define SP_CAN_CLOCK (16000000 / 2)
27 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
28 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
29 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
30 MODULE_ALIAS("platform:" DRV_NAME
);
31 MODULE_LICENSE("GPL v2");
33 struct sja1000_of_data
{
35 int (*init
)(struct sja1000_priv
*priv
, struct device_node
*of
);
38 struct technologic_priv
{
42 static u8
sp_read_reg8(const struct sja1000_priv
*priv
, int reg
)
44 return ioread8(priv
->reg_base
+ reg
);
47 static void sp_write_reg8(const struct sja1000_priv
*priv
, int reg
, u8 val
)
49 iowrite8(val
, priv
->reg_base
+ reg
);
52 static u8
sp_read_reg16(const struct sja1000_priv
*priv
, int reg
)
54 return ioread8(priv
->reg_base
+ reg
* 2);
57 static void sp_write_reg16(const struct sja1000_priv
*priv
, int reg
, u8 val
)
59 iowrite8(val
, priv
->reg_base
+ reg
* 2);
62 static u8
sp_read_reg32(const struct sja1000_priv
*priv
, int reg
)
64 return ioread8(priv
->reg_base
+ reg
* 4);
67 static void sp_write_reg32(const struct sja1000_priv
*priv
, int reg
, u8 val
)
69 iowrite8(val
, priv
->reg_base
+ reg
* 4);
72 static u8
sp_technologic_read_reg16(const struct sja1000_priv
*priv
, int reg
)
74 struct technologic_priv
*tp
= priv
->priv
;
78 spin_lock_irqsave(&tp
->io_lock
, flags
);
79 iowrite16(reg
, priv
->reg_base
+ 0);
80 val
= ioread16(priv
->reg_base
+ 2);
81 spin_unlock_irqrestore(&tp
->io_lock
, flags
);
86 static void sp_technologic_write_reg16(const struct sja1000_priv
*priv
,
89 struct technologic_priv
*tp
= priv
->priv
;
92 spin_lock_irqsave(&tp
->io_lock
, flags
);
93 iowrite16(reg
, priv
->reg_base
+ 0);
94 iowrite16(val
, priv
->reg_base
+ 2);
95 spin_unlock_irqrestore(&tp
->io_lock
, flags
);
98 static int sp_technologic_init(struct sja1000_priv
*priv
, struct device_node
*of
)
100 struct technologic_priv
*tp
= priv
->priv
;
102 priv
->read_reg
= sp_technologic_read_reg16
;
103 priv
->write_reg
= sp_technologic_write_reg16
;
104 spin_lock_init(&tp
->io_lock
);
109 static void sp_populate(struct sja1000_priv
*priv
,
110 struct sja1000_platform_data
*pdata
,
111 unsigned long resource_mem_flags
)
113 /* The CAN clock frequency is half the oscillator clock frequency */
114 priv
->can
.clock
.freq
= pdata
->osc_freq
/ 2;
115 priv
->ocr
= pdata
->ocr
;
116 priv
->cdr
= pdata
->cdr
;
118 switch (resource_mem_flags
& IORESOURCE_MEM_TYPE_MASK
) {
119 case IORESOURCE_MEM_32BIT
:
120 priv
->read_reg
= sp_read_reg32
;
121 priv
->write_reg
= sp_write_reg32
;
123 case IORESOURCE_MEM_16BIT
:
124 priv
->read_reg
= sp_read_reg16
;
125 priv
->write_reg
= sp_write_reg16
;
127 case IORESOURCE_MEM_8BIT
:
129 priv
->read_reg
= sp_read_reg8
;
130 priv
->write_reg
= sp_write_reg8
;
135 static void sp_populate_of(struct sja1000_priv
*priv
, struct device_node
*of
)
140 err
= of_property_read_u32(of
, "reg-io-width", &prop
);
142 prop
= 1; /* 8 bit is default */
146 priv
->read_reg
= sp_read_reg32
;
147 priv
->write_reg
= sp_write_reg32
;
150 priv
->read_reg
= sp_read_reg16
;
151 priv
->write_reg
= sp_write_reg16
;
155 priv
->read_reg
= sp_read_reg8
;
156 priv
->write_reg
= sp_write_reg8
;
159 err
= of_property_read_u32(of
, "nxp,external-clock-frequency", &prop
);
161 priv
->can
.clock
.freq
= prop
/ 2;
163 priv
->can
.clock
.freq
= SP_CAN_CLOCK
; /* default */
165 err
= of_property_read_u32(of
, "nxp,tx-output-mode", &prop
);
167 priv
->ocr
|= prop
& OCR_MODE_MASK
;
169 priv
->ocr
|= OCR_MODE_NORMAL
; /* default */
171 err
= of_property_read_u32(of
, "nxp,tx-output-config", &prop
);
173 priv
->ocr
|= (prop
<< OCR_TX_SHIFT
) & OCR_TX_MASK
;
175 priv
->ocr
|= OCR_TX0_PULLDOWN
; /* default */
177 err
= of_property_read_u32(of
, "nxp,clock-out-frequency", &prop
);
179 u32 divider
= priv
->can
.clock
.freq
* 2 / prop
;
182 priv
->cdr
|= divider
/ 2 - 1;
184 priv
->cdr
|= CDR_CLKOUT_MASK
;
186 priv
->cdr
|= CDR_CLK_OFF
; /* default */
189 if (!of_property_read_bool(of
, "nxp,no-comparator-bypass"))
190 priv
->cdr
|= CDR_CBP
; /* default */
193 static struct sja1000_of_data technologic_data
= {
194 .priv_sz
= sizeof(struct technologic_priv
),
195 .init
= sp_technologic_init
,
198 static const struct of_device_id sp_of_table
[] = {
199 { .compatible
= "nxp,sja1000", .data
= NULL
, },
200 { .compatible
= "technologic,sja1000", .data
= &technologic_data
, },
203 MODULE_DEVICE_TABLE(of
, sp_of_table
);
205 static int sp_probe(struct platform_device
*pdev
)
209 struct net_device
*dev
;
210 struct sja1000_priv
*priv
;
211 struct resource
*res_mem
, *res_irq
= NULL
;
212 struct sja1000_platform_data
*pdata
;
213 struct device_node
*of
= pdev
->dev
.of_node
;
214 const struct of_device_id
*of_id
;
215 const struct sja1000_of_data
*of_data
= NULL
;
218 pdata
= dev_get_platdata(&pdev
->dev
);
220 dev_err(&pdev
->dev
, "No platform data provided!\n");
224 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
228 if (!devm_request_mem_region(&pdev
->dev
, res_mem
->start
,
229 resource_size(res_mem
), DRV_NAME
))
232 addr
= devm_ioremap(&pdev
->dev
, res_mem
->start
,
233 resource_size(res_mem
));
238 irq
= irq_of_parse_and_map(of
, 0);
240 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
242 if (!irq
&& !res_irq
)
245 of_id
= of_match_device(sp_of_table
, &pdev
->dev
);
246 if (of_id
&& of_id
->data
) {
247 of_data
= of_id
->data
;
248 priv_sz
= of_data
->priv_sz
;
251 dev
= alloc_sja1000dev(priv_sz
);
254 priv
= netdev_priv(dev
);
257 irq
= res_irq
->start
;
258 priv
->irq_flags
= res_irq
->flags
& IRQF_TRIGGER_MASK
;
259 if (res_irq
->flags
& IORESOURCE_IRQ_SHAREABLE
)
260 priv
->irq_flags
|= IRQF_SHARED
;
262 priv
->irq_flags
= IRQF_SHARED
;
266 priv
->reg_base
= addr
;
269 sp_populate_of(priv
, of
);
271 if (of_data
&& of_data
->init
) {
272 err
= of_data
->init(priv
, of
);
277 sp_populate(priv
, pdata
, res_mem
->flags
);
280 platform_set_drvdata(pdev
, dev
);
281 SET_NETDEV_DEV(dev
, &pdev
->dev
);
283 err
= register_sja1000dev(dev
);
285 dev_err(&pdev
->dev
, "registering %s failed (err=%d)\n",
290 dev_info(&pdev
->dev
, "%s device registered (reg_base=%p, irq=%d)\n",
291 DRV_NAME
, priv
->reg_base
, dev
->irq
);
295 free_sja1000dev(dev
);
299 static int sp_remove(struct platform_device
*pdev
)
301 struct net_device
*dev
= platform_get_drvdata(pdev
);
303 unregister_sja1000dev(dev
);
304 free_sja1000dev(dev
);
309 static struct platform_driver sp_driver
= {
314 .of_match_table
= sp_of_table
,
318 module_platform_driver(sp_driver
);