2 * Platform CAN bus driver for Bosch C_CAN controller
4 * Copyright (C) 2010 ST Microelectronics
5 * Bhupesh Sharma <bhupesh.sharma@st.com>
7 * Borrowed heavily from the C_CAN driver originally written by:
9 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
10 * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
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
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.
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/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/clk.h>
35 #include <linux/can/dev.h>
40 * 16-bit c_can registers can be arranged differently in the memory
41 * architecture of different implementations. For example: 16-bit
42 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
43 * Handle the same by providing a common read/write interface.
45 static u16 c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv *priv,
51 static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv *priv,
57 static u16 c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv *priv,
60 return readw(reg + (long)reg - (long)priv->regs);
63 static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv *priv,
66 writew(val, reg + (long)reg - (long)priv->regs);
69 static int __devinit c_can_plat_probe(struct platform_device *pdev)
73 struct net_device *dev;
74 struct c_can_priv *priv;
77 #ifdef CONFIG_HAVE_CLK
80 /* get the appropriate clk */
81 clk = clk_get(&pdev->dev, NULL);
83 dev_err(&pdev->dev, "no clock defined\n");
89 /* get the platform data */
90 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
91 irq = platform_get_irq(pdev, 0);
92 if (!mem || irq <= 0) {
97 if (!request_mem_region(mem->start, resource_size(mem),
99 dev_err(&pdev->dev, "resource unavailable\n");
104 addr = ioremap(mem->start, resource_size(mem));
106 dev_err(&pdev->dev, "failed to map can port\n");
108 goto exit_release_mem;
111 /* allocate the c_can device */
112 dev = alloc_c_can_dev();
118 priv = netdev_priv(dev);
122 #ifdef CONFIG_HAVE_CLK
123 priv->can.clock.freq = clk_get_rate(clk);
127 switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
128 case IORESOURCE_MEM_32BIT:
129 priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
130 priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
132 case IORESOURCE_MEM_16BIT:
134 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
135 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
139 platform_set_drvdata(pdev, dev);
140 SET_NETDEV_DEV(dev, &pdev->dev);
142 ret = register_c_can_dev(dev);
144 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
145 KBUILD_MODNAME, ret);
146 goto exit_free_device;
149 dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
150 KBUILD_MODNAME, priv->regs, dev->irq);
154 platform_set_drvdata(pdev, NULL);
159 release_mem_region(mem->start, resource_size(mem));
161 #ifdef CONFIG_HAVE_CLK
165 dev_err(&pdev->dev, "probe failed\n");
170 static int __devexit c_can_plat_remove(struct platform_device *pdev)
172 struct net_device *dev = platform_get_drvdata(pdev);
173 struct c_can_priv *priv = netdev_priv(dev);
174 struct resource *mem;
176 unregister_c_can_dev(dev);
177 platform_set_drvdata(pdev, NULL);
182 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
183 release_mem_region(mem->start, resource_size(mem));
185 #ifdef CONFIG_HAVE_CLK
192 static struct platform_driver c_can_plat_driver = {
194 .name = KBUILD_MODNAME,
195 .owner = THIS_MODULE,
197 .probe = c_can_plat_probe,
198 .remove = __devexit_p(c_can_plat_remove),
201 static int __init c_can_plat_init(void)
203 return platform_driver_register(&c_can_plat_driver);
205 module_init(c_can_plat_init);
207 static void __exit c_can_plat_exit(void)
209 platform_driver_unregister(&c_can_plat_driver);
211 module_exit(c_can_plat_exit);
213 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
214 MODULE_LICENSE("GPL v2");
215 MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");