c_can_platform.c revision 881ff67ad45041f6ff08441aa17302aea77bd054
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/version.h> 24#include <linux/module.h> 25#include <linux/interrupt.h> 26#include <linux/delay.h> 27#include <linux/netdevice.h> 28#include <linux/if_arp.h> 29#include <linux/if_ether.h> 30#include <linux/list.h> 31#include <linux/delay.h> 32#include <linux/io.h> 33#include <linux/platform_device.h> 34#include <linux/clk.h> 35 36#include <linux/can/dev.h> 37 38#include "c_can.h" 39 40/* 41 * 16-bit c_can registers can be arranged differently in the memory 42 * architecture of different implementations. For example: 16-bit 43 * registers can be aligned to a 16-bit boundary or 32-bit boundary etc. 44 * Handle the same by providing a common read/write interface. 45 */ 46static u16 c_can_plat_read_reg_aligned_to_16bit(struct c_can_priv *priv, 47 void *reg) 48{ 49 return readw(reg); 50} 51 52static void c_can_plat_write_reg_aligned_to_16bit(struct c_can_priv *priv, 53 void *reg, u16 val) 54{ 55 writew(val, reg); 56} 57 58static u16 c_can_plat_read_reg_aligned_to_32bit(struct c_can_priv *priv, 59 void *reg) 60{ 61 return readw(reg + (long)reg - (long)priv->regs); 62} 63 64static void c_can_plat_write_reg_aligned_to_32bit(struct c_can_priv *priv, 65 void *reg, u16 val) 66{ 67 writew(val, reg + (long)reg - (long)priv->regs); 68} 69 70static int __devinit c_can_plat_probe(struct platform_device *pdev) 71{ 72 int ret; 73 void __iomem *addr; 74 struct net_device *dev; 75 struct c_can_priv *priv; 76 struct resource *mem, *irq; 77#ifdef CONFIG_HAVE_CLK 78 struct clk *clk; 79 80 /* get the appropriate clk */ 81 clk = clk_get(&pdev->dev, NULL); 82 if (IS_ERR(clk)) { 83 dev_err(&pdev->dev, "no clock defined\n"); 84 ret = -ENODEV; 85 goto exit; 86 } 87#endif 88 89 /* get the platform data */ 90 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 91 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 92 if (!mem || (irq <= 0)) { 93 ret = -ENODEV; 94 goto exit_free_clk; 95 } 96 97 if (!request_mem_region(mem->start, resource_size(mem), 98 KBUILD_MODNAME)) { 99 dev_err(&pdev->dev, "resource unavailable\n"); 100 ret = -ENODEV; 101 goto exit_free_clk; 102 } 103 104 addr = ioremap(mem->start, resource_size(mem)); 105 if (!addr) { 106 dev_err(&pdev->dev, "failed to map can port\n"); 107 ret = -ENOMEM; 108 goto exit_release_mem; 109 } 110 111 /* allocate the c_can device */ 112 dev = alloc_c_can_dev(); 113 if (!dev) { 114 ret = -ENOMEM; 115 goto exit_iounmap; 116 } 117 118 priv = netdev_priv(dev); 119 120 dev->irq = irq->start; 121 priv->regs = addr; 122#ifdef CONFIG_HAVE_CLK 123 priv->can.clock.freq = clk_get_rate(clk); 124 priv->priv = clk; 125#endif 126 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; 131 break; 132 case IORESOURCE_MEM_16BIT: 133 default: 134 priv->read_reg = c_can_plat_read_reg_aligned_to_16bit; 135 priv->write_reg = c_can_plat_write_reg_aligned_to_16bit; 136 break; 137 } 138 139 platform_set_drvdata(pdev, dev); 140 SET_NETDEV_DEV(dev, &pdev->dev); 141 142 ret = register_c_can_dev(dev); 143 if (ret) { 144 dev_err(&pdev->dev, "registering %s failed (err=%d)\n", 145 KBUILD_MODNAME, ret); 146 goto exit_free_device; 147 } 148 149 dev_info(&pdev->dev, "%s device registered (regs=%p, irq=%d)\n", 150 KBUILD_MODNAME, priv->regs, dev->irq); 151 return 0; 152 153exit_free_device: 154 platform_set_drvdata(pdev, NULL); 155 free_c_can_dev(dev); 156exit_iounmap: 157 iounmap(addr); 158exit_release_mem: 159 release_mem_region(mem->start, resource_size(mem)); 160exit_free_clk: 161#ifdef CONFIG_HAVE_CLK 162 clk_put(clk); 163exit: 164#endif 165 dev_err(&pdev->dev, "probe failed\n"); 166 167 return ret; 168} 169 170static int __devexit c_can_plat_remove(struct platform_device *pdev) 171{ 172 struct net_device *dev = platform_get_drvdata(pdev); 173 struct c_can_priv *priv = netdev_priv(dev); 174 struct resource *mem; 175 176 unregister_c_can_dev(dev); 177 platform_set_drvdata(pdev, NULL); 178 179 free_c_can_dev(dev); 180 iounmap(priv->regs); 181 182 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 183 release_mem_region(mem->start, resource_size(mem)); 184 185#ifdef CONFIG_HAVE_CLK 186 clk_put(priv->priv); 187#endif 188 189 return 0; 190} 191 192static struct platform_driver c_can_plat_driver = { 193 .driver = { 194 .name = KBUILD_MODNAME, 195 .owner = THIS_MODULE, 196 }, 197 .probe = c_can_plat_probe, 198 .remove = __devexit_p(c_can_plat_remove), 199}; 200 201static int __init c_can_plat_init(void) 202{ 203 return platform_driver_register(&c_can_plat_driver); 204} 205module_init(c_can_plat_init); 206 207static void __exit c_can_plat_exit(void) 208{ 209 platform_driver_unregister(&c_can_plat_driver); 210} 211module_exit(c_can_plat_exit); 212 213MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>"); 214MODULE_LICENSE("GPL v2"); 215MODULE_DESCRIPTION("Platform CAN bus driver for Bosch C_CAN controller"); 216