1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2005 Sascha Hauer, Pengutronix
4  * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
5  */
6 
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>
17 #include <linux/io.h>
18 #include <linux/of.h>
19 #include <linux/of_device.h>
20 
21 #include "sja1000.h"
22 
23 #define DRV_NAME "sja1000_platform"
24 #define SP_CAN_CLOCK  (16000000 / 2)
25 
26 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
27 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
28 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
29 MODULE_ALIAS("platform:" DRV_NAME);
30 MODULE_LICENSE("GPL v2");
31 
32 struct sja1000_of_data {
33 	size_t  priv_sz;
34 	void    (*init)(struct sja1000_priv *priv, struct device_node *of);
35 };
36 
37 struct technologic_priv {
38 	spinlock_t      io_lock;
39 };
40 
41 static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
42 {
43 	return ioread8(priv->reg_base + reg);
44 }
45 
46 static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
47 {
48 	iowrite8(val, priv->reg_base + reg);
49 }
50 
51 static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
52 {
53 	return ioread8(priv->reg_base + reg * 2);
54 }
55 
56 static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
57 {
58 	iowrite8(val, priv->reg_base + reg * 2);
59 }
60 
61 static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
62 {
63 	return ioread8(priv->reg_base + reg * 4);
64 }
65 
66 static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
67 {
68 	iowrite8(val, priv->reg_base + reg * 4);
69 }
70 
71 static u8 sp_technologic_read_reg16(const struct sja1000_priv *priv, int reg)
72 {
73 	struct technologic_priv *tp = priv->priv;
74 	unsigned long flags;
75 	u8 val;
76 
77 	spin_lock_irqsave(&tp->io_lock, flags);
78 	iowrite16(reg, priv->reg_base + 0);
79 	val = ioread16(priv->reg_base + 2);
80 	spin_unlock_irqrestore(&tp->io_lock, flags);
81 
82 	return val;
83 }
84 
85 static void sp_technologic_write_reg16(const struct sja1000_priv *priv,
86 				       int reg, u8 val)
87 {
88 	struct technologic_priv *tp = priv->priv;
89 	unsigned long flags;
90 
91 	spin_lock_irqsave(&tp->io_lock, flags);
92 	iowrite16(reg, priv->reg_base + 0);
93 	iowrite16(val, priv->reg_base + 2);
94 	spin_unlock_irqrestore(&tp->io_lock, flags);
95 }
96 
97 static void sp_technologic_init(struct sja1000_priv *priv, struct device_node *of)
98 {
99 	struct technologic_priv *tp = priv->priv;
100 
101 	priv->read_reg = sp_technologic_read_reg16;
102 	priv->write_reg = sp_technologic_write_reg16;
103 	spin_lock_init(&tp->io_lock);
104 }
105 
106 static void sp_populate(struct sja1000_priv *priv,
107 			struct sja1000_platform_data *pdata,
108 			unsigned long resource_mem_flags)
109 {
110 	/* The CAN clock frequency is half the oscillator clock frequency */
111 	priv->can.clock.freq = pdata->osc_freq / 2;
112 	priv->ocr = pdata->ocr;
113 	priv->cdr = pdata->cdr;
114 
115 	switch (resource_mem_flags & IORESOURCE_MEM_TYPE_MASK) {
116 	case IORESOURCE_MEM_32BIT:
117 		priv->read_reg = sp_read_reg32;
118 		priv->write_reg = sp_write_reg32;
119 		break;
120 	case IORESOURCE_MEM_16BIT:
121 		priv->read_reg = sp_read_reg16;
122 		priv->write_reg = sp_write_reg16;
123 		break;
124 	case IORESOURCE_MEM_8BIT:
125 	default:
126 		priv->read_reg = sp_read_reg8;
127 		priv->write_reg = sp_write_reg8;
128 		break;
129 	}
130 }
131 
132 static void sp_populate_of(struct sja1000_priv *priv, struct device_node *of)
133 {
134 	int err;
135 	u32 prop;
136 
137 	err = of_property_read_u32(of, "reg-io-width", &prop);
138 	if (err)
139 		prop = 1; /* 8 bit is default */
140 
141 	switch (prop) {
142 	case 4:
143 		priv->read_reg = sp_read_reg32;
144 		priv->write_reg = sp_write_reg32;
145 		break;
146 	case 2:
147 		priv->read_reg = sp_read_reg16;
148 		priv->write_reg = sp_write_reg16;
149 		break;
150 	case 1:
151 	default:
152 		priv->read_reg = sp_read_reg8;
153 		priv->write_reg = sp_write_reg8;
154 	}
155 
156 	err = of_property_read_u32(of, "nxp,external-clock-frequency", &prop);
157 	if (!err)
158 		priv->can.clock.freq = prop / 2;
159 	else
160 		priv->can.clock.freq = SP_CAN_CLOCK; /* default */
161 
162 	err = of_property_read_u32(of, "nxp,tx-output-mode", &prop);
163 	if (!err)
164 		priv->ocr |= prop & OCR_MODE_MASK;
165 	else
166 		priv->ocr |= OCR_MODE_NORMAL; /* default */
167 
168 	err = of_property_read_u32(of, "nxp,tx-output-config", &prop);
169 	if (!err)
170 		priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
171 	else
172 		priv->ocr |= OCR_TX0_PULLDOWN; /* default */
173 
174 	err = of_property_read_u32(of, "nxp,clock-out-frequency", &prop);
175 	if (!err && prop) {
176 		u32 divider = priv->can.clock.freq * 2 / prop;
177 
178 		if (divider > 1)
179 			priv->cdr |= divider / 2 - 1;
180 		else
181 			priv->cdr |= CDR_CLKOUT_MASK;
182 	} else {
183 		priv->cdr |= CDR_CLK_OFF; /* default */
184 	}
185 
186 	if (!of_property_read_bool(of, "nxp,no-comparator-bypass"))
187 		priv->cdr |= CDR_CBP; /* default */
188 }
189 
190 static struct sja1000_of_data technologic_data = {
191 	.priv_sz = sizeof(struct technologic_priv),
192 	.init = sp_technologic_init,
193 };
194 
195 static const struct of_device_id sp_of_table[] = {
196 	{ .compatible = "nxp,sja1000", .data = NULL, },
197 	{ .compatible = "technologic,sja1000", .data = &technologic_data, },
198 	{ /* sentinel */ },
199 };
200 MODULE_DEVICE_TABLE(of, sp_of_table);
201 
202 static int sp_probe(struct platform_device *pdev)
203 {
204 	int err, irq = 0;
205 	void __iomem *addr;
206 	struct net_device *dev;
207 	struct sja1000_priv *priv;
208 	struct resource *res_mem, *res_irq = NULL;
209 	struct sja1000_platform_data *pdata;
210 	struct device_node *of = pdev->dev.of_node;
211 	const struct sja1000_of_data *of_data = NULL;
212 	size_t priv_sz = 0;
213 
214 	pdata = dev_get_platdata(&pdev->dev);
215 	if (!pdata && !of) {
216 		dev_err(&pdev->dev, "No platform data provided!\n");
217 		return -ENODEV;
218 	}
219 
220 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
221 	if (!res_mem)
222 		return -ENODEV;
223 
224 	if (!devm_request_mem_region(&pdev->dev, res_mem->start,
225 				     resource_size(res_mem), DRV_NAME))
226 		return -EBUSY;
227 
228 	addr = devm_ioremap(&pdev->dev, res_mem->start,
229 				    resource_size(res_mem));
230 	if (!addr)
231 		return -ENOMEM;
232 
233 	if (of) {
234 		irq = platform_get_irq(pdev, 0);
235 		if (irq < 0)
236 			return irq;
237 	} else {
238 		res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
239 		if (!res_irq)
240 			return -ENODEV;
241 	}
242 
243 	of_data = device_get_match_data(&pdev->dev);
244 	if (of_data)
245 		priv_sz = of_data->priv_sz;
246 
247 	dev = alloc_sja1000dev(priv_sz);
248 	if (!dev)
249 		return -ENOMEM;
250 	priv = netdev_priv(dev);
251 
252 	if (res_irq) {
253 		irq = res_irq->start;
254 		priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
255 		if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
256 			priv->irq_flags |= IRQF_SHARED;
257 	} else {
258 		priv->irq_flags = IRQF_SHARED;
259 	}
260 
261 	dev->irq = irq;
262 	priv->reg_base = addr;
263 
264 	if (of) {
265 		sp_populate_of(priv, of);
266 
267 		if (of_data && of_data->init)
268 			of_data->init(priv, of);
269 	} else {
270 		sp_populate(priv, pdata, res_mem->flags);
271 	}
272 
273 	platform_set_drvdata(pdev, dev);
274 	SET_NETDEV_DEV(dev, &pdev->dev);
275 
276 	err = register_sja1000dev(dev);
277 	if (err) {
278 		dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
279 			DRV_NAME, err);
280 		goto exit_free;
281 	}
282 
283 	dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
284 		 DRV_NAME, priv->reg_base, dev->irq);
285 	return 0;
286 
287  exit_free:
288 	free_sja1000dev(dev);
289 	return err;
290 }
291 
292 static int sp_remove(struct platform_device *pdev)
293 {
294 	struct net_device *dev = platform_get_drvdata(pdev);
295 
296 	unregister_sja1000dev(dev);
297 	free_sja1000dev(dev);
298 
299 	return 0;
300 }
301 
302 static struct platform_driver sp_driver = {
303 	.probe = sp_probe,
304 	.remove = sp_remove,
305 	.driver = {
306 		.name = DRV_NAME,
307 		.of_match_table = sp_of_table,
308 	},
309 };
310 
311 module_platform_driver(sp_driver);
312