Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Sascha Hauer, Pengutronix |
| 3 | * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the version 2 of the GNU General Public License |
| 7 | * as published by the Free Software Foundation |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/pci.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/irq.h> |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 27 | #include <linux/can/dev.h> |
| 28 | #include <linux/can/platform/sja1000.h> |
| 29 | #include <linux/io.h> |
| 30 | |
| 31 | #include "sja1000.h" |
| 32 | |
| 33 | #define DRV_NAME "sja1000_platform" |
| 34 | |
| 35 | MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>"); |
| 36 | MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus"); |
| 37 | MODULE_LICENSE("GPL v2"); |
| 38 | |
Yegor Yefremov | 986917b | 2010-03-19 00:50:44 +0000 | [diff] [blame] | 39 | static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg) |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 40 | { |
Wolfgang Grandegger | 255a915 | 2009-05-30 07:55:49 +0000 | [diff] [blame] | 41 | return ioread8(priv->reg_base + reg); |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Yegor Yefremov | 986917b | 2010-03-19 00:50:44 +0000 | [diff] [blame] | 44 | static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val) |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 45 | { |
Wolfgang Grandegger | 255a915 | 2009-05-30 07:55:49 +0000 | [diff] [blame] | 46 | iowrite8(val, priv->reg_base + reg); |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Yegor Yefremov | 986917b | 2010-03-19 00:50:44 +0000 | [diff] [blame] | 49 | static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg) |
| 50 | { |
| 51 | return ioread8(priv->reg_base + reg * 2); |
| 52 | } |
| 53 | |
| 54 | static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val) |
| 55 | { |
| 56 | iowrite8(val, priv->reg_base + reg * 2); |
| 57 | } |
| 58 | |
| 59 | static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg) |
| 60 | { |
| 61 | return ioread8(priv->reg_base + reg * 4); |
| 62 | } |
| 63 | |
| 64 | static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val) |
| 65 | { |
| 66 | iowrite8(val, priv->reg_base + reg * 4); |
| 67 | } |
| 68 | |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 69 | static int sp_probe(struct platform_device *pdev) |
| 70 | { |
| 71 | int err; |
| 72 | void __iomem *addr; |
| 73 | struct net_device *dev; |
| 74 | struct sja1000_priv *priv; |
| 75 | struct resource *res_mem, *res_irq; |
| 76 | struct sja1000_platform_data *pdata; |
| 77 | |
| 78 | pdata = pdev->dev.platform_data; |
| 79 | if (!pdata) { |
| 80 | dev_err(&pdev->dev, "No platform data provided!\n"); |
| 81 | err = -ENODEV; |
| 82 | goto exit; |
| 83 | } |
| 84 | |
| 85 | res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 86 | res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 87 | if (!res_mem || !res_irq) { |
| 88 | err = -ENODEV; |
| 89 | goto exit; |
| 90 | } |
| 91 | |
| 92 | if (!request_mem_region(res_mem->start, resource_size(res_mem), |
| 93 | DRV_NAME)) { |
| 94 | err = -EBUSY; |
| 95 | goto exit; |
| 96 | } |
| 97 | |
| 98 | addr = ioremap_nocache(res_mem->start, resource_size(res_mem)); |
| 99 | if (!addr) { |
| 100 | err = -ENOMEM; |
| 101 | goto exit_release; |
| 102 | } |
| 103 | |
| 104 | dev = alloc_sja1000dev(0); |
| 105 | if (!dev) { |
| 106 | err = -ENOMEM; |
| 107 | goto exit_iounmap; |
| 108 | } |
| 109 | priv = netdev_priv(dev); |
| 110 | |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 111 | dev->irq = res_irq->start; |
Yegor Yefremov | abde89d | 2010-03-19 00:49:08 +0000 | [diff] [blame] | 112 | priv->irq_flags = res_irq->flags & (IRQF_TRIGGER_MASK | IRQF_SHARED); |
Wolfgang Grandegger | 255a915 | 2009-05-30 07:55:49 +0000 | [diff] [blame] | 113 | priv->reg_base = addr; |
Wolfgang Grandegger | 56e6943b9 | 2010-05-17 22:39:48 -0700 | [diff] [blame] | 114 | /* The CAN clock frequency is half the oscillator clock frequency */ |
| 115 | priv->can.clock.freq = pdata->osc_freq / 2; |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 116 | priv->ocr = pdata->ocr; |
| 117 | priv->cdr = pdata->cdr; |
| 118 | |
Yegor Yefremov | 986917b | 2010-03-19 00:50:44 +0000 | [diff] [blame] | 119 | switch (res_mem->flags & IORESOURCE_MEM_TYPE_MASK) { |
| 120 | case IORESOURCE_MEM_32BIT: |
| 121 | priv->read_reg = sp_read_reg32; |
| 122 | priv->write_reg = sp_write_reg32; |
| 123 | break; |
| 124 | case IORESOURCE_MEM_16BIT: |
| 125 | priv->read_reg = sp_read_reg16; |
| 126 | priv->write_reg = sp_write_reg16; |
| 127 | break; |
| 128 | case IORESOURCE_MEM_8BIT: |
| 129 | default: |
| 130 | priv->read_reg = sp_read_reg8; |
| 131 | priv->write_reg = sp_write_reg8; |
| 132 | break; |
| 133 | } |
| 134 | |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 135 | dev_set_drvdata(&pdev->dev, dev); |
| 136 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 137 | |
| 138 | err = register_sja1000dev(dev); |
| 139 | if (err) { |
| 140 | dev_err(&pdev->dev, "registering %s failed (err=%d)\n", |
| 141 | DRV_NAME, err); |
| 142 | goto exit_free; |
| 143 | } |
| 144 | |
Wolfgang Grandegger | 255a915 | 2009-05-30 07:55:49 +0000 | [diff] [blame] | 145 | dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n", |
| 146 | DRV_NAME, priv->reg_base, dev->irq); |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 147 | return 0; |
| 148 | |
| 149 | exit_free: |
| 150 | free_sja1000dev(dev); |
| 151 | exit_iounmap: |
| 152 | iounmap(addr); |
| 153 | exit_release: |
| 154 | release_mem_region(res_mem->start, resource_size(res_mem)); |
| 155 | exit: |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | static int sp_remove(struct platform_device *pdev) |
| 160 | { |
| 161 | struct net_device *dev = dev_get_drvdata(&pdev->dev); |
Wolfgang Grandegger | 255a915 | 2009-05-30 07:55:49 +0000 | [diff] [blame] | 162 | struct sja1000_priv *priv = netdev_priv(dev); |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 163 | struct resource *res; |
| 164 | |
| 165 | unregister_sja1000dev(dev); |
| 166 | dev_set_drvdata(&pdev->dev, NULL); |
| 167 | |
Wolfgang Grandegger | 255a915 | 2009-05-30 07:55:49 +0000 | [diff] [blame] | 168 | if (priv->reg_base) |
| 169 | iounmap(priv->reg_base); |
Wolfgang Grandegger | f534e52 | 2009-05-15 23:39:31 +0000 | [diff] [blame] | 170 | |
| 171 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 172 | release_mem_region(res->start, resource_size(res)); |
| 173 | |
| 174 | free_sja1000dev(dev); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static struct platform_driver sp_driver = { |
| 180 | .probe = sp_probe, |
| 181 | .remove = sp_remove, |
| 182 | .driver = { |
| 183 | .name = DRV_NAME, |
| 184 | .owner = THIS_MODULE, |
| 185 | }, |
| 186 | }; |
| 187 | |
Axel Lin | 871d337 | 2011-11-27 15:42:31 +0000 | [diff] [blame] | 188 | module_platform_driver(sp_driver); |