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>
31 #include <linux/platform_device.h>
32 #include <linux/clk.h>
34 #include <linux/can/dev.h>
39 * 16-bit c_can registers can be arranged differently in the memory
40 * architecture of different implementations. For example: 16-bit
41 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc.
42 * Handle the same by providing a common read/write interface.
44 static u16 c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv *priv,
50 static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv *priv,
56 static u16 c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv *priv,
59 return readw(reg + (long)reg - (long)priv->regs);
62 static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv *priv,
65 writew(val, reg + (long)reg - (long)priv->regs);
68 static int __devinit c_can_plat_probe(struct platform_device *pdev)
72 struct net_device *dev;
73 struct c_can_priv *priv;
76 #ifdef CONFIG_HAVE_CLK
79 /* get the appropriate clk */
80 clk = clk_get(&pdev->dev, NULL);
82 dev_err(&pdev->dev, "no clock defined\n");
88 /* get the platform data */
89 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
90 irq = platform_get_irq(pdev, 0);
91 if (!mem || irq <= 0) {
96 if (!request_mem_region(mem->start, resource_size(mem),
98 dev_err(&pdev->dev, "resource unavailable\n");
103 addr = ioremap(mem->start, resource_size(mem));
105 dev_err(&pdev->dev, "failed to map can port\n");
107 goto exit_release_mem;
110 /* allocate the c_can device */
111 dev = alloc_c_can_dev();
117 priv = netdev_priv(dev);
121 #ifdef CONFIG_HAVE_CLK
122 priv->can.clock.freq = clk_get_rate(clk);
126 switch (mem->flags & IORESOURCE_MEM_TYPE_MASK) {
127 case IORESOURCE_MEM_32BIT:
128 priv->read_reg = c_can_plat_read_reg_aligned_to_32bit;
129 priv->write_reg = c_can_plat_write_reg_aligned_to_32bit;
131 case IORESOURCE_MEM_16BIT:
133 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit;
134 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit;
138 platform_set_drvdata(pdev, dev);
139 SET_NETDEV_DEV(dev, &pdev->dev);
141 ret = register_c_can_dev(dev);
143 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
144 KBUILD_MODNAME, ret);
145 goto exit_free_device;
148 dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n",
149 KBUILD_MODNAME, priv->regs, dev->irq);
153 platform_set_drvdata(pdev, NULL);
158 release_mem_region(mem->start, resource_size(mem));
160 #ifdef CONFIG_HAVE_CLK
164 dev_err(&pdev->dev, "probe failed\n");
169 static int __devexit c_can_plat_remove(struct platform_device *pdev)
171 struct net_device *dev = platform_get_drvdata(pdev);
172 struct c_can_priv *priv = netdev_priv(dev);
173 struct resource *mem;
175 unregister_c_can_dev(dev);
176 platform_set_drvdata(pdev, NULL);
181 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
182 release_mem_region(mem->start, resource_size(mem));
184 #ifdef CONFIG_HAVE_CLK
191 static struct platform_driver c_can_plat_driver = {
193 .name = KBUILD_MODNAME,
194 .owner = THIS_MODULE,
196 .probe = c_can_plat_probe,
197 .remove = __devexit_p(c_can_plat_remove),
200 static int __init c_can_plat_init(void)
202 return platform_driver_register(&c_can_plat_driver);
204 module_init(c_can_plat_init);
206 static void __exit c_can_plat_exit(void)
208 platform_driver_unregister(&c_can_plat_driver);
210 module_exit(c_can_plat_exit);
212 MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
213 MODULE_LICENSE("GPL v2");
214 MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller");