1 /*
2  * Platform CAN bus driver for Bosch C_CAN controller
3  *
4  * Copyright (C) 2010 ST Microelectronics
5  * Bhupesh Sharma <bhupesh.sharma@st.com>
6  *
7  * Borrowed heavily from the C_CAN driver originally written by:
8  * Copyright (C) 2007
9  * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
10  * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
11  *
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
16  *
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.
20  */
21 
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>
30 #include <linux/io.h>
31 #include <linux/platform_device.h>
32 #include <linux/clk.h>
33 #include <linux/of.h>
34 #include <linux/of_device.h>
35 #include <linux/pinctrl/consumer.h>
36 
37 #include <linux/can/dev.h>
38 
39 #include "c_can.h"
40 
41 #define CAN_RAMINIT_START_MASK(i)	(1 << (i))
42 
43 /*
44  * 16-bit c_can registers can be arranged differently in the memory
45  * architecture of different implementations. For example: 16-bit
46  * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
47  * Handle the same by providing a common read/write interface.
48  */
49 static u16 c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv *priv,
50 						enum reg index)
51 {
52 	return readw(priv->base + priv->regs[index]);
53 }
54 
55 static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv *priv,
56 						enum reg index, u16 val)
57 {
58 	writew(val, priv->base + priv->regs[index]);
59 }
60 
61 static u16 c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv *priv,
62 						enum reg index)
63 {
64 	return readw(priv->base + 2 * priv->regs[index]);
65 }
66 
67 static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv *priv,
68 						enum reg index, u16 val)
69 {
70 	writew(val, priv->base + 2 * priv->regs[index]);
71 }
72 
73 static void c_can_hw_raminit(const struct c_can_priv *priv, bool enable)
74 {
75 	u32 val;
76 
77 	val = readl(priv->raminit_ctrlreg);
78 	if (enable)
79 		val |= CAN_RAMINIT_START_MASK(priv->instance);
80 	else
81 		val &= ~CAN_RAMINIT_START_MASK(priv->instance);
82 	writel(val, priv->raminit_ctrlreg);
83 }
84 
85 static struct platform_device_id c_can_id_table[] = {
86 	[BOSCH_C_CAN_PLATFORM] = {
87 		.name = KBUILD_MODNAME,
88 		.driver_data = BOSCH_C_CAN,
89 	},
90 	[BOSCH_C_CAN] = {
91 		.name = "c_can",
92 		.driver_data = BOSCH_C_CAN,
93 	},
94 	[BOSCH_D_CAN] = {
95 		.name = "d_can",
96 		.driver_data = BOSCH_D_CAN,
97 	}, {
98 	}
99 };
100 MODULE_DEVICE_TABLE(platform, c_can_id_table);
101 
102 static const struct of_device_id c_can_of_table[] = {
103 	{ .compatible = "bosch,c_can", .data = &c_can_id_table[BOSCH_C_CAN] },
104 	{ .compatible = "bosch,d_can", .data = &c_can_id_table[BOSCH_D_CAN] },
105 	{ /* sentinel */ },
106 };
107 MODULE_DEVICE_TABLE(of, c_can_of_table);
108 
109 static int c_can_plat_probe(struct platform_device *pdev)
110 {
111 	int ret;
112 	void __iomem *addr;
113 	struct net_device *dev;
114 	struct c_can_priv *priv;
115 	const struct of_device_id *match;
116 	const struct platform_device_id *id;
117 	struct pinctrl *pinctrl;
118 	struct resource *mem, *res;
119 	int irq;
120 	struct clk *clk;
121 
122 	if (pdev->dev.of_node) {
123 		match = of_match_device(c_can_of_table, &pdev->dev);
124 		if (!match) {
125 			dev_err(&pdev->dev, "Failed to find matching dt id\n");
126 			ret = -EINVAL;
127 			goto exit;
128 		}
129 		id = match->data;
130 	} else {
131 		id = platform_get_device_id(pdev);
132 	}
133 
134 	pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
135 	if (IS_ERR(pinctrl))
136 		dev_warn(&pdev->dev,
137 			"failed to configure pins from driver\n");
138 
139 	/* get the appropriate clk */
140 	clk = clk_get(&pdev->dev, NULL);
141 	if (IS_ERR(clk)) {
142 		dev_err(&pdev->dev, "no clock defined\n");
143 		ret = -ENODEV;
144 		goto exit;
145 	}
146 
147 	/* get the platform data */
148 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 	irq = platform_get_irq(pdev, 0);
150 	if (!mem || irq <= 0) {
151 		ret = -ENODEV;
152 		goto exit_free_clk;
153 	}
154 
155 	if (!request_mem_region(mem->start, resource_size(mem),
156 				KBUILD_MODNAME)) {
157 		dev_err(&pdev->dev, "resource unavailable\n");
158 		ret = -ENODEV;
159 		goto exit_free_clk;
160 	}
161 
162 	addr = ioremap(mem->start, resource_size(mem));
163 	if (!addr) {
164 		dev_err(&pdev->dev, "failed to map can port\n");
165 		ret = -ENOMEM;
166 		goto exit_release_mem;
167 	}
168 
169 	/* allocate the c_can device */
170 	dev = alloc_c_can_dev();
171 	if (!dev) {
172 		ret = -ENOMEM;
173 		goto exit_iounmap;
174 	}
175 
176 	priv = netdev_priv(dev);
177 	switch (id->driver_data) {
178 	case BOSCH_C_CAN:
179 		priv->regs = reg_map_c_can;
180 		switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
181 		case IORESOURCE_MEM_32BIT:
182 			priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
183 			priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
184 			break;
185 		case IORESOURCE_MEM_16BIT:
186 		default:
187 			priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
188 			priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
189 			break;
190 		}
191 		break;
192 	case BOSCH_D_CAN:
193 		priv->regs = reg_map_d_can;
194 		priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
195 		priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
196 		priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
197 
198 		if (pdev->dev.of_node)
199 			priv->instance = of_alias_get_id(pdev->dev.of_node, "d_can");
200 		else
201 			priv->instance = pdev->id;
202 
203 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
204 		priv->raminit_ctrlreg =	devm_request_and_ioremap(&pdev->dev, res);
205 		if (!priv->raminit_ctrlreg || priv->instance < 0)
206 			dev_info(&pdev->dev, "control memory is not used for raminit\n");
207 		else
208 			priv->raminit = c_can_hw_raminit;
209 		break;
210 	default:
211 		ret = -EINVAL;
212 		goto exit_free_device;
213 	}
214 
215 	dev->irq = irq;
216 	priv->base = addr;
217 	priv->device = &pdev->dev;
218 	priv->can.clock.freq = clk_get_rate(clk);
219 	priv->priv = clk;
220 	priv->type = id->driver_data;
221 
222 	platform_set_drvdata(pdev, dev);
223 	SET_NETDEV_DEV(dev, &pdev->dev);
224 
225 	ret = register_c_can_dev(dev);
226 	if (ret) {
227 		dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
228 			KBUILD_MODNAME, ret);
229 		goto exit_free_device;
230 	}
231 
232 	dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
233 		 KBUILD_MODNAME, priv->base, dev->irq);
234 	return 0;
235 
236 exit_free_device:
237 	platform_set_drvdata(pdev, NULL);
238 	free_c_can_dev(dev);
239 exit_iounmap:
240 	iounmap(addr);
241 exit_release_mem:
242 	release_mem_region(mem->start, resource_size(mem));
243 exit_free_clk:
244 	clk_put(clk);
245 exit:
246 	dev_err(&pdev->dev, "probe failed\n");
247 
248 	return ret;
249 }
250 
251 static int c_can_plat_remove(struct platform_device *pdev)
252 {
253 	struct net_device *dev = platform_get_drvdata(pdev);
254 	struct c_can_priv *priv = netdev_priv(dev);
255 	struct resource *mem;
256 
257 	unregister_c_can_dev(dev);
258 	platform_set_drvdata(pdev, NULL);
259 
260 	free_c_can_dev(dev);
261 	iounmap(priv->base);
262 
263 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
264 	release_mem_region(mem->start, resource_size(mem));
265 
266 	clk_put(priv->priv);
267 
268 	return 0;
269 }
270 
271 #ifdef CONFIG_PM
272 static int c_can_suspend(struct platform_device *pdev, pm_message_t state)
273 {
274 	int ret;
275 	struct net_device *ndev = platform_get_drvdata(pdev);
276 	struct c_can_priv *priv = netdev_priv(ndev);
277 
278 	if (priv->type != BOSCH_D_CAN) {
279 		dev_warn(&pdev->dev, "Not supported\n");
280 		return 0;
281 	}
282 
283 	if (netif_running(ndev)) {
284 		netif_stop_queue(ndev);
285 		netif_device_detach(ndev);
286 	}
287 
288 	ret = c_can_power_down(ndev);
289 	if (ret) {
290 		netdev_err(ndev, "failed to enter power down mode\n");
291 		return ret;
292 	}
293 
294 	priv->can.state = CAN_STATE_SLEEPING;
295 
296 	return 0;
297 }
298 
299 static int c_can_resume(struct platform_device *pdev)
300 {
301 	int ret;
302 	struct net_device *ndev = platform_get_drvdata(pdev);
303 	struct c_can_priv *priv = netdev_priv(ndev);
304 
305 	if (priv->type != BOSCH_D_CAN) {
306 		dev_warn(&pdev->dev, "Not supported\n");
307 		return 0;
308 	}
309 
310 	ret = c_can_power_up(ndev);
311 	if (ret) {
312 		netdev_err(ndev, "Still in power down mode\n");
313 		return ret;
314 	}
315 
316 	priv->can.state = CAN_STATE_ERROR_ACTIVE;
317 
318 	if (netif_running(ndev)) {
319 		netif_device_attach(ndev);
320 		netif_start_queue(ndev);
321 	}
322 
323 	return 0;
324 }
325 #else
326 #define c_can_suspend NULL
327 #define c_can_resume NULL
328 #endif
329 
330 static struct platform_driver c_can_plat_driver = {
331 	.driver = {
332 		.name = KBUILD_MODNAME,
333 		.owner = THIS_MODULE,
334 		.of_match_table = of_match_ptr(c_can_of_table),
335 	},
336 	.probe = c_can_plat_probe,
337 	.remove = c_can_plat_remove,
338 	.suspend = c_can_suspend,
339 	.resume = c_can_resume,
340 	.id_table = c_can_id_table,
341 };
342 
343 module_platform_driver(c_can_plat_driver);
344 
345 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
346 MODULE_LICENSE("GPL v2");
347 MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");
348